To retrieve the accessor methods (i.e., the "getters") of a Java record, you should not rely on conventional naming patterns such as "get" or "is", because records automatically generate accessor methods with the same name as their component fields.
Instead, Java provides a reflection API that allows you to programmatically access these generated methods via the Class object of the record. Here's how you can do it:
✅ Key Concepts
-
Records in Java are a special kind of class that automatically generates accessor methods for each of its components.
-
These accessor methods are public, have no parameters, and are named after the component field.
-
For example, if a record has a component String name, the accessor method is name().
-
Java 16+ introduced the getRecordComponents() method on the Class class, which allows you to retrieve the components of a record.
-
Each RecordComponent object contains the name and type of the component, which can be used to find the corresponding accessor method.
🔍 How to Retrieve Accessor Methods of a Record
Here's a complete and safe way to retrieve all public accessor methods of a record:
import java.lang.reflect.Method;
import java.lang.reflect.RecordComponent;
import java.util.ArrayList;
import java.util.List;
public class RecordAccessorFinder {
public static List<Method> getRecordAccessors(Class<?> recordClass) {
List<Method> accessors = new ArrayList<>();
// Check if the class is a record
if (recordClass.isRecord()) {
for (RecordComponent component : recordClass.getRecordComponents()) {
try {
// Retrieve the method by name (same as component name)
Method method = recordClass.getMethod(component.getName());
accessors.add(method);
} catch (NoSuchMethodException e) {
// Should not happen for valid records
e.printStackTrace();
}
}
}
return accessors;
}
public static void main(String[] args) {
// Example usage
List<Method> accessors = getRecordAccessors(Person.class);
for (Method method : accessors) {
System.out.println("Accessor method: " + method.getName() + " (" + method.getReturnType().getName() + ")");
}
}
}
📌 Example Record Class
public record Person(String name, int age) {}
When you run the above code with Person.class, it will output:
Accessor method: name (java.lang.String)
Accessor method: age (int)
⚠️ Notes
- Java Version:
getRecordComponents() is available in Java 16 and later. If you're using an older version, this method is not available.
- Accessibility: The generated methods are public, so
getMethod() will find them.
- Non-public Components: If a component is not public (e.g., package-private), the corresponding method is also not public. In that case, use
getDeclaredMethod() and set it accessible if needed.
🧠 Summary
| Feature | Description |
|---|
| Method Naming | Accessor methods are named after the component fields (e.g., name(), age()) |
| Reflection API | Use Class.getRecordComponents() to get the list of components |
| Method Lookup | Use Class.getMethod(name) to get the corresponding method |
| Java Version | Requires Java 16 or newer |
✅ Conclusion
For regular classes, you can use naming conventions like "get" or "is" to find getters. But for Java records, you must use the reflection API with getRecordComponents() and look up the methods by the component names. This approach is both accurate and idiomatic for records.