Quick revision
The short answer
The framework calls getDeclaredMethods, which returns both the real handler method and the synthetic bridge method javac generated for the generic interface, and because recent javac versions copy annotations onto bridges, both look like annotated handlers. The fix is to skip methods where Method.isBridge() is true, or to resolve each bridge to the method it forwards to, as Spring's BridgeMethodResolver does, and then read annotations only from that method. We should also add tests with generic interfaces and covariant returns to the framework's own suite.
What to remember
In this answer, we saw that a framework registers handlers twice because getDeclaredMethods returns both the real method and the synthetic bridge that javac generates for generic interfaces, and recent javac copies annotations onto bridges, so both pass an annotation check. The fix is to skip methods with isBridge(), or to resolve each bridge to its bridged method, as Spring's BridgeMethodResolver does, before reading annotations and parameter types. Tests with generic interfaces, overridden generic methods and covariant returns keep the problem from coming back.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The framework calls getDeclaredMethods, which returns both the real handler method and the synthetic bridge method javac generated for the generic interface, and because recent javac versions copy annotations onto bridges, both look like annotated handlers. The fix is to skip methods where Method.isBridge() is true, or to resolve each bridge to the method it forwards to, as Spring's BridgeMethodResolver does, and then read annotations only from that method. We should also add tests with generic interfaces and covariant returns to the framework's own suite.
Why this question is asked
Internal frameworks often start as a few lines of reflection: find every method with our annotation and register it. That works until someone implements a generic interface, and suddenly every event is handled twice, or an annotation works on one class and not another. Interviewers use this scenario to see whether we can connect a symptom in a framework to how javac compiles generics. In this answer, we will reproduce the double registration, explain where the second method comes from, see why annotations make it worse, apply two fixes, and then protect the framework with tests.
1Reproducing the Double Registration
Here is a small framework that registers any method annotated with @Handler, and a handler class that implements a generic interface.
Notice the second line. We wrote one method, but the scanner found two, and the second one takes Object. Registered under Object, it now receives every event type, and for an OrderCreated event both entries fire, so the handler runs twice.
2Where the Second Method Comes From
EventHandler<E> erases to an interface whose method is handle(Object). Our class declares handle(OrderCreated), which has a different descriptor, so at the bytecode level it does not override the interface method. To keep overriding working, javac generates a bridge method in OrderCreatedHandler: public void handle(Object event), marked synthetic and bridge, which casts the argument to OrderCreated and calls our method.
getDeclaredMethods returns every method the class file declares, and that includes the bridge. There is no flag in the Java source that hides it, because it really is a public method of the class. So any framework that scans methods by reflection must decide what to do with bridges.
| Method | Written by | isBridge | Annotations seen |
|---|---|---|---|
handle(OrderCreated) | Us | false | @Handler |
handle(Object) | javac | true | @Handler (copied by recent javac) |
3Why the Annotations Look Inconsistent
Older javac versions did not copy annotations to bridge methods, so older frameworks were accidentally protected: the bridge had no @Handler and was skipped. Since around JDK 8 javac copies method annotations, and parameter annotations, onto the bridge, so the bridge now passes the annotation check too. Check your version for exact behaviour, but on any current JDK we should assume bridges carry the annotations.
That history explains the inconsistent reports. A class compiled with an older compiler, or one where the bridge is not needed because the class implements a raw or non-generic interface, behaves differently from a class compiled today with a generic interface. Covariant returns add more variety: if the interface method returns Object and the class returns Order, there is a bridge even without generics. If the framework reads annotations from whichever method it happened to find first, the bridge and the real method can disagree, for example when an annotation was added on a subclass override but the bridge belongs to a parent.
4Fix 1: Skip Bridge Methods
The simplest fix is to ignore bridges entirely. The real method always exists alongside the bridge, so we lose nothing.
Notice that we register with the parameter type of the real method, OrderCreated, so routing is also correct now. isSynthetic also skips other generated methods such as lambda bodies, which a handler scanner never wants.
5Fix 2: Resolve Bridges to the Real Method
Sometimes the framework does not choose which method it gets. A proxy or an interceptor may receive the Method object that was invoked, and if the call came through the generic interface, that is the bridge. In that case we resolve it to the bridged method, the real one, before reading annotations or parameter types. Spring does exactly this with BridgeMethodResolver, and if our framework runs inside Spring we can use it directly, together with AnnotatedElementUtils, which also merges annotations from interfaces and superclasses.
- Get declared methods
- isBridge() or isSynthetic()?YesSkip, or resolve to the bridged methodNoKeep the method
- Read annotations from the real methodmerged from interfaces if needed
- Has @Handler?YesRegister with the real parameter typeNoIgnore
- Fail start-up on duplicate registrationcatches future regressions
5 steps in order. Play them, or step through them one at a time.
Without Spring, the same idea is a search: among the class's non-bridge methods, find one with the same name and parameter count whose parameter types are assignable to the bridge's erased types, and check that its generic signature matches. That is harder to get right than it looks, which is why reusing a tested resolver is worth it.
6Protecting the Framework With Tests
The bug went unnoticed because the framework's tests used only simple handler classes. We add test fixtures that cover the cases that create bridges: a handler for a generic interface, a subclass that overrides a generic method, a method with a covariant return type, and an abstract generic base class with a concrete subclass. Each test asserts the exact number of registrations and the parameter type of each one.
It also helps to make the framework strict: if two registrations target the same bean and the same logical method, fail at start-up with a clear message. A duplicate is never intended, and failing loudly turns a silent double execution in production into an error the author sees on the first run.
7Follow-up questions
Is it safe to always skip bridge methods?
For scanning, yes, because every bridge forwards to a real method that the class also declares or inherits, and that real method carries the user's intent. The only care needed is when we start from a bridge we were handed, such as in an interceptor; then we resolve it rather than skip it.
Does getMethods behave differently from getDeclaredMethods here?
getMethods returns public methods including inherited ones, and it also includes bridges, so the same problem appears. Whichever we use, the isBridge filter is needed.
Why not compare method names to remove duplicates?
Because overloads legitimately share names. A handler class may have handle(OrderCreated) and handle(OrderCancelled), and a name-based filter would drop one of them. isBridge is precise; names are not.
8Conclusion
In this answer, we saw that a framework registers handlers twice because getDeclaredMethods returns both the real method and the synthetic bridge that javac generates for generic interfaces, and recent javac copies annotations onto bridges, so both pass an annotation check. The fix is to skip methods with isBridge(), or to resolve each bridge to its bridged method, as Spring's BridgeMethodResolver does, before reading annotations and parameter types. Tests with generic interfaces, overridden generic methods and covariant returns keep the problem from coming back.
Open with
The cause is bridge methods. When a class implements a generic interface like EventHandler<OrderCreated>, the interface method erases to handle(Object), so javac generates a synthetic bridge handle(Object) in the class that casts and calls the real handle(OrderCreated). getDeclaredMethods returns both, and since about JDK 8 javac copies the method annotations onto the bridge, so a scanner that checks for the annotation registers both methods.
Then, if they want more
The bridge is registered under Object, so it can receive every event, and the handler runs twice. The inconsistency people see comes from which methods get bridges: generic interfaces, overridden generic methods and covariant return types do, and older compilers did not copy annotations.
To fix the framework, I filter with Method.isBridge, and isSynthetic, when scanning, and I always read the parameter types and annotations from the real method. If the framework is handed a Method, for example in an interceptor, I resolve it with Spring's BridgeMethodResolver, or an equivalent search, before looking at it.
Then I add test fixtures for generic interfaces, generic base classes and covariant returns that assert exactly one registration each, and I make duplicate registrations fail at start-up.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A framework that scans methods by reflection registers each handler twice, or applies...": https://scrivio-interview-library.netlify.app/java/generics/5e7aa7f2a1/