Reflection
Reflection
Definition
Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods, etc.
It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.
What can you find out from an Object using reflection
- Class: object.class
- Fields: Class.getFields
- Methods: Class.getMethods
- Constructors and its parameters. Class.getConstructors
- Private Fields and Methods
- Annotations (some of them)
- Array introspection (determine the type of class)
- Generic type introspection
- Java Modules
- Dynamic proxies (class to implement a Java Interface dynamically at runtime)
Examples of reflection
- Can be used to map properties in JSON files to getter/setter methods in Java objects, like Jackson or GSON.
- Can be used to map the column names of a JDBC ResultSet to getter/setter methods in a Java object.
Reflection Examples
Class object
Class myObjectClass = MyObject.class;
Object.class.getMethods()
Method[] methods = MyObject.class.getMethods();
for(Method method : methods){
System.out.println("method = " + method.getName());
}
Fields
Class myObjectClass = MyObject.class;
Field[] fields = myObjectClass.getFields();