Sub topic 7
Private members, setAccessible, and Java modules
This chapter assumes you can already get a Class object and read its fields and methods with getDeclaredField and getDeclaredMethod. It covers what happens when reflection touches a private member, and how the Java 9+ module system changes the answer.
Why private members fail by default
Reflection does not skip Java's access rules. The getDeclared... methods return private members, but using them still goes through an access check. If your code could not write account.balance at that spot, Field.get throws IllegalAccessException.
Calling setAccessible(true) on the Field, Method, or Constructor turns off that check for that one object.
import java.lang.reflect.Field;
class Account {
private int balance = 100;
}
public class AccessDemo {
public static void main(String[] args) throws Exception {
Field balance = Account.class.getDeclaredField("balance");
try {
balance.get(new Account());
} catch (IllegalAccessException e) {
System.out.println("Blocked: " + e.getMessage());
}
balance.setAccessible(true);
System.out.println(balance.get(new Account()));
Field value = String.class.getDeclaredField("value");
System.out.println("String.value accessible: " + value.trySetAccessible());
}
}The first read is blocked, and the read after setAccessible(true) prints 100. The last line depends on your Java version, as the next section explains.
What modules change
Since Java 9, code can be packaged in modules, and each module decides which of its packages other code may reach. setAccessible succeeds only if the target package is open to the caller's module.
| Directive | Compile-time use of public types | Runtime reflection on private members |
|---|---|---|
| `exports pkg` | Yes | No |
| `opens pkg` | No | Yes |
The JDK's own modules keep their internals closed. Code on the classpath runs in the unnamed module, which can still reach anything that is open.
When the package is not open, setAccessible throws InaccessibleObjectException, which is unchecked. trySetAccessible() returns false instead of throwing.
Opening your own packages
A framework that reflects on your classes needs your package opened to it. In module-info.java:
module com.example.app {
opens com.example.model;
}You can restrict the opening with opens com.example.model to some.framework;.
For JDK internals, or for libraries you cannot change, use a launcher flag:
java --add-opens java.base/java.lang=ALL-UNNAMED -cp app.jar MainCommon mistakes
- Calling
getFieldorgetMethodfor a private member. These find only public members, so you getNoSuchFieldExceptionorNoSuchMethodException. Use thegetDeclared...versions. - Forgetting that
getDeclared...does not search superclasses. Walk up withgetSuperclass(). - Calling
setAccessibleon a shared object and keeping it around. The flag lives on thatFieldinstance.getDeclaredFieldreturns a fresh copy each time, so the change does not leak to other callers. - Writing to a
finalfield. An instancefinalfield can be changed aftersetAccessible(true), but the JIT may have inlined the old value, so readers may not see the update.
Exercise
A framework calls setAccessible(true) on a private field of your class, which is in a named module, and gets InaccessibleObjectException. Name two ways to fix it without changing the framework.
Show answer
Add "opens your.package;" (optionally "to framework.module") to your module-info.java, or start the JVM with --add-opens your.module/your.package=framework.module (or ALL-UNNAMED if the framework is on the classpath).