Java Materials
← Core Java · Chapter 10

Reflection, modules and release coverage

Reflection inspects Class, constructors, methods and fields. Runtime discovery does not override language or module access rules. Metadata-driven frameworks should document what packages need exposure and why. Prefer stable public extension points when a migration would otherwise rely on internal JDK types.

Reflection, proxies and method handles

Definition:

A dynamic proxy redirects interface method calls to an invocation handler.

Explanation:

A JDK dynamic proxy implements interfaces and invokes InvocationHandler for calls.

Illustration image:

Reflection, proxies and method handles. Caller invokes interface · Generated proxy · InvocationHandler · Optional target callReflection, proxies and method handlesSource or actorCaller invokes interfaceFirst boundaryGenerated proxyInspected stateInvocationHandlerObservable effectOptional target call
Caller invokes interface · Generated proxy · InvocationHandler · Optional target call

Example:

Worked Java example · see surrounding context in notes
var proxy = (Runnable) java.lang.reflect.Proxy.newProxyInstance(
    Runnable.class.getClassLoader(), new Class<?>[]{Runnable.class},
    (receiver, method, args) -> { System.out.println(method.getName()); return null; });
proxy.run();

Expected behavior. Prints run. A real handler should define behavior for equals, hashCode and toString too.

Internal working:

A handler can inspect Method and arguments, but must explicitly call the target to continue. Method handles can represent typed invocations with a lookup-derived access right. Reflection access and performance depend on release; measure actual paths.

Interview questions:

Does a proxy automatically call the implementation?

No. The handler must explicitly delegate or implement the operation itself.

Modules, services and reflection boundaries

Definition:

JPMS defines module readability and controlled access to packages and services.

Explanation:

JPMS distinguishes readability, exported public surfaces and opened reflective packages.

Illustration image:

Modules, services and reflection boundaries. Client module · Exported package · Open DTO package · Closed internal packageModules, services and reflection boundariesSource or actorClient moduleFirst boundaryExported packageInspected stateOpen DTO packageObservable effectClosed internal package
Client module · Exported package · Open DTO package · Closed internal package

Example:

Worked Java example · see surrounding context in notes
module policy.engine {
    requires policy.api;
    uses policy.api.Rule;
    opens policy.engine.dto to policy.serializer;
}

Expected behavior. The DTO package is selectively open for reflection. It is not automatically exported as a public module API.

Internal working:

Use requires for dependencies, uses for service consumption and provides with for provider registration. Keep reflective openings scoped to the framework that requires them. A deep-reflection failure often indicates an obsolete library or incorrect module declaration.

Interview questions:

How is opens different from exports?

Exports exposes public API for normal access; opens permits deep reflection for the allowed targets.

Compile targets and preview boundaries

Definition:

A release target defines supported language and platform APIs; preview is release-specific.

Explanation:

Using a Java 21 compiler does not make Java 21 classes run on Java 17.

Illustration image:

Compile targets and preview boundaries. JDK 21 compiler · --release 17 · Java 17 class/API target · Test on JDK 17Compile targets and preview boundariesSource or actorJDK 21 compilerFirst boundary--release 17Inspected stateJava 17 class/API targetObservable effectTest on JDK 17
JDK 21 compiler · --release 17 · Java 17 class/API target · Test on JDK 17

Example:

Shell / diagnostic commands
javac --release 17 Example.java
# For code using a Java 21 preview feature:
javac --release 21 --enable-preview PreviewDemo.java
java --enable-preview PreviewDemo

Expected behavior. A stable Java 17 build cannot accidentally call an API introduced only in Java 21.

Internal working:

Use javac --release to restrict source language and available platform APIs, and run tests on the actual target. Preview is release-specific and may change. In Java 21, virtual threads are final, but structured concurrency and scoped values require preview flags.

Interview questions:

Can a JDK 17 runtime execute a class compiled with default Java 21 settings?

Generally no. Compile with --release 17 for Java 17 and test the actual target runtime.

Reference subtopics

Annotations, proxies and handles

Retention controls whether metadata survives source, class-file or runtime boundaries; targets restrict location. Annotation processors run at compile time. Dynamic proxies dispatch interface calls through InvocationHandler. Method handles are typed callable references with explicit lookup permissions. ServiceLoader discovers declared service providers without hard-coded implementation names.

JPMS

module-info.java declares requires for dependencies, exports for public API packages and opens for deep reflection when allowed. Opening a package is broader than exporting it for normal calls. Diagnose compatibility with jdeps, runtime tests and a minimal reproducer before adding any scoped migration flag.

Java 8 to 17

Java 8 brought lambdas, references, default methods, Streams, Optional, java.time and CompletableFuture. Java 9–17 brought modules, collection factories, var, standardized HTTP Client, switch expressions, text blocks, records, instanceof patterns and sealed types. Some features became final before 17; know the actual release when answering.

Java 18 to 21

Java 21 finalizes virtual threads, record patterns, switch pattern matching and sequenced collections. Structured concurrency and scoped values are preview in Java 21. Build and run release-specific preview code with preview flags and distinguish these APIs from their status in later JDKs.

Run the complete Java labs ↗ · Java reference ↗