Reflection API and Dynamic Proxies

How Java inspects and changes behaviour at runtime: Class objects, fields, methods, constructors and annotations via reflection, MethodHandles, JDK dynamic proxies vs CGLIB and ByteBuddy, how frameworks like Spring and Hibernate use them, performance, module encapsulation and GraalVM native-image constraints.

On this page

Simple definition#

  • Reflection lets a program inspect and use classes, methods, fields, constructors and annotations at runtime, without knowing them at compile time.
  • A dynamic proxy is an object created at runtime that implements interfaces (or subclasses a class) and routes every method call through your handler code.

Together they are the engine behind dependency injection, AOP, ORMs, serialisation and test mocks.

The problem#

Frameworks must work with your classes, which they have never seen. Spring must find @Autowired constructors, Hibernate must read @Entity fields, Jackson must call getters and constructors, and @Transactional must wrap your method calls. Reflection and proxies make that possible.

Reflection basics#

Class<?> type = Class.forName("com.shop.Order");          // or Order.class, or obj.getClass()

for (Field f : type.getDeclaredFields()) {                 // includes private fields
    if (f.isAnnotationPresent(Column.class)) {
        System.out.println(f.getName() + " -> " + f.getAnnotation(Column.class).name());
    }
}

Constructor<?> ctor = type.getDeclaredConstructor(String.class, BigDecimal.class);
Object order = ctor.newInstance("ORD-1", new BigDecimal("12.50"));

Method total = type.getMethod("total");
Object value = total.invoke(order);                        // dynamic call

Field status = type.getDeclaredField("status");
status.setAccessible(true);                                // bypass private (subject to module rules)
status.set(order, "PAID");
APIGives you
ClassName, modifiers, superclass, interfaces, record components (getRecordComponents()), sealed permitted subclasses
Field / Method / ConstructorGet and set, invoke, generic signatures (getGenericType()), annotations
AnnotationRuntime-retained annotations (@Retention(RUNTIME))
ParameterParameter names (when compiled with -parameters, which Spring Boot builds use)

MethodHandles: the faster, modern alternative#

java.lang.invoke.MethodHandle is a typed, directly executable reference to a method or field. The JIT can inline it, and access is checked once at lookup time. Since JDK 18 (JEP 416), core reflection itself is reimplemented on top of method handles.

MethodHandle total = MethodHandles.lookup()
        .findVirtual(Order.class, "total", MethodType.methodType(BigDecimal.class));
BigDecimal value = (BigDecimal) total.invoke(order);

Dynamic proxies#

JDK dynamic proxy (interfaces only)#

interface PaymentClient { Receipt charge(Payment p); }

PaymentClient timed = (PaymentClient) Proxy.newProxyInstance(
        PaymentClient.class.getClassLoader(),
        new Class<?>[] { PaymentClient.class },
        (proxy, method, args) -> {                          // InvocationHandler
            long start = System.nanoTime();
            try {
                return method.invoke(realClient, args);     // delegate to the real object
            } finally {
                log.info("{} took {} µs", method.getName(), (System.nanoTime() - start) / 1_000);
            }
        });
Calling through a proxy
sequenceDiagram
    participant C as Caller
    participant P as Proxy ($Proxy42)
    participant H as InvocationHandler
    participant T as Real object
    C->>P: charge(payment)
    P->>H: invoke(proxy, Method charge, args)
    H->>H: before: start timer / begin tx / check auth
    H->>T: method.invoke(target, args)
    T-->>H: receipt
    H->>H: after: log / commit
    H-->>C: receipt

Class-based proxies: CGLIB and ByteBuddy#

JDK proxies need an interface. To proxy a class, libraries generate a subclass at runtime that overrides the methods:

JDK dynamic proxySubclass proxy (CGLIB in Spring, ByteBuddy in Hibernate and Mockito)
RequiresAn interfaceA non-final class with non-final methods
Proxy typeImplements the interfacesExtends the class
Can’t interceptMethods not on the interfacefinal or private methods, constructors
Spring Boot defaultOnly if configuredYes (proxyTargetClass=true by default)

Who uses what:

  • Spring AOP (@Transactional, @Cacheable, @Async, method security): JDK or CGLIB proxies. See AOP and proxies.
  • Spring Data repositories: JDK proxies of your repository interfaces.
  • Hibernate lazy loading: ByteBuddy subclass proxies, or bytecode enhancement.
  • Mockito: ByteBuddy subclasses, or the inline mock maker (the default since Mockito 5), which can mock final classes.
  • HTTP interface clients (@HttpExchange in Spring 6+): JDK proxies that turn method calls into HTTP requests.

Performance and limits#

  • Reflection is slower than direct calls, mainly because of lookup and access checks. Cache Method and MethodHandle objects; never look them up per call.
  • Strong encapsulation (JDK 16+, JEP 396/403): setAccessible(true) on JDK internals fails unless the package is opened (--add-opens). Your own modules must opens packages to frameworks that reflect over them. See JPMS.
  • GraalVM native image needs reflection known at build time. Spring Boot’s AOT engine generates reflection hints (RuntimeHints) for this.
  • Integrity by default (JEP draft) continues to restrict deep reflection, and JDK 26 started warning when final fields are mutated through reflection (JEP 500). Prefer constructors and public APIs.

Common mistakes#

Quick check#

What can reflection do? Inspect classes, fields, methods, constructors and annotations at runtime; create instances; invoke methods; and read or write fields, including private ones if access is allowed.

JDK dynamic proxy vs CGLIB? JDK proxies implement interfaces and route calls to an InvocationHandler. CGLIB (and ByteBuddy) generate subclasses, so they can proxy classes but not final methods.

Why doesn’t a proxy intercept self-invocation? The internal call goes through this, which is the target object itself, not the proxy wrapping it.

What is a MethodHandle? A typed, directly invocable reference to a method or field, checked at lookup and optimisable by the JIT. Modern reflection is built on it.

Why do native images need reflection hints? GraalVM analyses reachability ahead of time, so classes and members used reflectively must be declared at build time.

Summary#

  • Reflection inspects and invokes code at runtime; frameworks depend on it.
  • Proxies wrap objects to add behaviour: JDK proxies for interfaces, CGLIB and ByteBuddy for classes.
  • Cache reflective lookups, respect module encapsulation, and remember self-invocation and final limits.

References#