Scrivio Interview library

Reflection, method handles and proxies

22 questions in 7 subtopics.

Reflection API

ConceptFoundations6 min read
What can you do with the Reflection API? Name the core classes and explain the difference between getMethods() and getDeclaredMethods().
CodingFoundations7 min read
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?
DebuggingIntermediate6 min read
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?
DesignAdvanced6 min read
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

CodingFoundations6 min read
Rewrite this reflective call using a MethodHandle:
Method m = String.class.getMethod("toUpperCase");
Object r = m.invoke("abc");
Explain what type the handle has.
InternalsIntermediate7 min read
Explain how invokedynamic works and how the Java compiler uses it for lambdas and for string concatenation.
InternalsIntermediate7 min read
What is a MethodHandle and how does it differ from java.lang.reflect.Method? Explain Lookup, MethodType and when access is checked.
ConceptPrincipal6 min read
Why did the JVM add invokedynamic in Java 7, and how has it shaped the evolution of the Java language since then?

VarHandles

ConceptIntermediate7 min read
What is a VarHandle 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.
ScenarioAdvanced7 min read
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

ConceptFoundations6 min read
How does java.lang.reflect.Proxy create a dynamic proxy? What are its limits compared with CGLIB or ByteBuddy subclass proxies?
CodingFoundations6 min read
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.
DebuggingIntermediate7 min read
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.
LeadershipPrincipal7 min read
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

ConceptIntermediate7 min read
Explain the difference between exports and opens in module-info for reflection. Why does a JPA or Jackson module often need opens?
DebuggingIntermediate7 min read
After upgrading from Java 8 to 17, a library fails with InaccessibleObjectException: Unable to make field private final ... accessible: module java.base does not "opens java.lang". What changed and what are the correct fixes?
LeadershipAdvanced7 min read
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

ConceptFoundations8 min read
Why is reflection usually slower than a direct method call? What changed in recent JDKs in how core reflection is implemented?
ScenarioIntermediate8 min read
A profiler shows 15 percent of CPU in a hand-written mapper that uses Class.getDeclaredField and Field.get for every object it maps. How would you improve it, and what options exist beyond caching?
DesignAdvanced8 min read
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

ConceptAdvanced7 min read
What is the java.lang.classfile Class-File API, finalised in Java 24, and why did the JDK add it when ASM already exists?
LeadershipPrincipal7 min read
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?