Reflection, method handles and proxies
22 questions in 7 subtopics.
Reflection API
What can you do with the Reflection API? Name the core classes and explain the difference betweengetMethods() and getDeclaredMethods().Write code that, given any object, prints the names and values of all its fields including private fields declared in superclasses. What exceptions and edge cases must you handle?A framework calls method.invoke(target, args) and the logs show java.lang.reflect.InvocationTargetException with no useful message. The team keeps adding catch blocks. What is happening and how should the code handle it?You are designing a plugin system where third-party jars register handlers. One proposal scans all classes with reflection at startup; another uses ServiceLoader. Compare them for a platform used by many teams.MethodHandles and invokedynamic
Rewrite this reflective call using aMethodHandle:Method m = String.class.getMethod("toUpperCase");
Object r = m.invoke("abc");
MethodHandle and how does it differ from java.lang.reflect.Method? Explain Lookup, MethodType and when access is checked.Why did the JVM add invokedynamic in Java 7, and how has it shaped the evolution of the Java language since then?VarHandles
What is aVarHandle and why was it added in Java 9? Show how you would perform a compare-and-set on a volatile int field without AtomicIntegerFieldUpdater or Unsafe.Your platform's internal high-performance libraries still use sun.misc.Unsafe for field CAS and off-heap memory. Recent JDKs warn about Unsafe memory access methods and plan to remove them. How do you plan the migration?Dynamic proxies
How doesjava.lang.reflect.Proxy create a dynamic proxy? What are its limits compared with CGLIB or ByteBuddy subclass proxies?Write a JDK dynamic proxy that logs the time taken by every method of any interface implementation. Mention one bug people often make in the handler.In a Spring service, @Transactional on a method has no effect when the method is called from another method in the same class. Explain using how proxies work and give fixes.Spring, Hibernate, Mockito and many internal tools depend on run-time proxies and generated subclasses. As the JDK tightens integrity by default and AOT becomes common, what long-term risks does this dependence carry, and how would you steer your platform?Reflection and the module system
Explain the difference between exports and opens in module-info for reflection. Why does a JPA or Jackson module often need opens?After upgrading from Java 8 to 17, a library fails withInaccessibleObjectException: Unable to make field private final ... accessible: module java.base does not "opens java.lang". What changed and what are the correct fixes?Your services carry many --add-opens flags in their launch scripts. How do you decide which are acceptable, and what policy do you set for the organisation?Reflection cost
Why is reflection usually slower than a direct method call? What changed in recent JDKs in how core reflection is implemented?A profiler shows 15 percent of CPU in a hand-written mapper that usesClass.getDeclaredField and Field.get for every object it maps. How would you improve it, and what options exist beyond caching?Your company is considering native images (GraalVM) and AOT for faster startup. Many internal libraries rely on run-time reflection and proxies. What changes do you require and how do you assess cost?Class file API
What is thejava.lang.classfile Class-File API, finalised in Java 24, and why did the JDK add it when ASM already exists?Bytecode manipulation libraries such as ASM, CGLIB and ByteBuddy underpin frameworks, mocking and agents. With the JDK shipping its own Class-File API, how should a large organisation think about its dependence on bytecode generation over the next decade?No question matches these choices.