Working with Reflection
1. Getting Class Objects
| Form | Detail |
obj.getClass() | Runtime type |
String.class | Class literal |
Class.forName("com.x.Foo") | Dynamic load |
| Method | Returns |
getName / getSimpleName / getCanonicalName | Names |
getSuperclass / getInterfaces | Type hierarchy |
getModifiers() | int (use Modifier.isPublic(m)) |
getPackage / getModule | Container info |
3. Working with Fields
Example: Field access
Field f = User.class.getDeclaredField("email");
f.setAccessible(true);
String email = (String) f.get(user);
f.set(user, "new@x.com");
| Method | Returns |
getFields() | Public fields (incl. inherited) |
getDeclaredFields() | All fields of this class |
getDeclaredField(name) | Single field |
4. Working with Methods
| Method | Use |
getMethod(name, paramTypes...) | Public method |
getDeclaredMethod(...) | Including non-public |
m.invoke(target, args...) | Call |
| Static call | m.invoke(null, args) |
5. Working with Constructors
| Method | Use |
getConstructor(types) | Get |
ctor.newInstance(args) | Instantiate |
cls.getDeclaredConstructors() | All |
6. Creating Instances Dynamically
| Form | Detail |
cls.getDeclaredConstructor().newInstance() | Modern way (Java 9+) |
cls.newInstance() | Deprecated |
7. Invoking Methods Dynamically
| Aspect | Detail |
| Returns | Object (cast required) |
| Throws | InvocationTargetException wraps the actual exception |
| Performance | Slower than direct call; cache Method objects |
8. Accessing Private Members
| Method | Detail |
setAccessible(true) | Bypass access check |
| Module-protected | Requires --add-opens for JDK internals |
| Records | Components accessible via getRecordComponents() |
Warning: Reflection breaks encapsulation and module boundaries — use sparingly.
9. Working with Generic Types
| Method | Returns |
getGenericType() | Field's generic type (Type) |
getGenericReturnType() | Method's generic return |
ParameterizedType | Cast to extract type args |
| Note | Erasure removes type info from instances; only declarations remain |
10. Using Class.forName()
| Overload | Detail |
forName(name) | Initialize, default classloader |
forName(name, init, cl) | Control init + classloader |
11. Using ClassLoader
| Type | Loads |
| Bootstrap | Core JDK classes |
| Platform | Java SE platform modules |
| App (system) | Classpath |
| Custom | Extend ClassLoader |
| Optimization | Detail |
| Cache lookups | Reuse Field/Method instances |
| MethodHandles | Faster alternative (java.lang.invoke) |
| VarHandles | For field access |
| JIT | Optimizes hot reflective calls |
13. Using MethodHandles API
Example: MethodHandle
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(String.class, int.class, int.class);
MethodHandle mh = lookup.findVirtual(String.class, "substring", mt);
String s = (String) mh.invoke("hello world", 0, 5); // "hello"
| Class | Use |
MethodHandles.Lookup | Access enforcement context |
MethodType | Signature description |
MethodHandle | Typed function reference (faster than Method.invoke) |
VarHandle | Atomic field operations |