Sub topic 11
Reflection and the `opens` Directive
Prerequisites
You should know Java packages, access modifiers, interfaces, and basic reflection. You should also understand that a named module has a module-info.java descriptor and that modules can export packages.
Why `exports` Is Not Enough
An exports directive makes a package available to ordinary Java code in other modules. It allows code to import public classes and call their public methods.
Reflection has a different access path. A framework such as a serializer, dependency-injection container, or test library may need to inspect private fields or call private constructors. In a named module, this deep reflective access is blocked unless the package is opened.
The opens directive grants reflective access without making the package part of the ordinary compile-time API.
flowchart TD
A[Named module] --> B{Access request}
B -->|Import public type| C[requires plus exports]
B -->|Reflect on private member| D[requires plus opens]
C --> E[Compile-time access]
D --> F[Runtime deep reflection]Opening a Package
This descriptor opens the model package only to the application module:
// src/com.example.model/module-info.java
module com.example.model {
opens com.example.model to com.example.app;
}
// src/com.example.model/com/example/model/Person.java
package com.example.model;
public class Person {
private String name = "Ada";
public Person() {
}
}The application uses reflection. It does not import Person, so the model package does not need to be exported:
// src/com.example.app/module-info.java
module com.example.app {
requires com.example.model;
}
// src/com.example.app/com/example/app/Main.java
package com.example.app;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws Exception {
Class<?> type = Class.forName("com.example.model.Person");
Object person = type.getConstructor().newInstance();
Field name = type.getDeclaredField("name");
name.setAccessible(true);
System.out.println(name.get(person));
}
}Compile and run the modules:
javac -d out --module-source-path src -m com.example.app
java --module-path out -m com.example.app/com.example.app.MainThe output is:
AdaWithout the opens com.example.model to com.example.app; directive, setAccessible(true) fails with an InaccessibleObjectException.
Choosing an Opening
An unqualified opening permits deep reflection from every module:
opens com.example.model;A qualified opening names the specific module that receives access:
opens com.example.model to com.example.app;Prefer a qualified opening when you know which framework or application needs access. It limits exposure. An open module declaration opens every package in the module and is convenient for frameworks, but it gives up more encapsulation:
open module com.example.model {
}Use exports for types that other modules should compile against. Use opens for implementation details that trusted code must inspect at runtime.
Exercise
A framework must set private fields in com.example.model, but application code must not import those classes. Which directive should you use?
Show answer
Use opens com.example.model to the framework's named module. Do not use exports unless ordinary compile-time access is also required.