A compiler may generate a synthetic bridge method when erasure would otherwise break an overriding method's dispatch signature.
Java bridge methods: preserve overrides after generic erasure
Operational contract
The base source declares a generic fee method and the concrete source returns Long. At the source level, calling through FeeSource<Long> invokes the concrete override. The compiler can emit an Object-returning bridge alongside the Long-returning method so erased callers still reach that override. Reflection may show both; method scanners should inspect isBridge and isSynthetic before treating every declared method as a separate business API. Do not call the synthetic method directly. An unchecked raw-type call can still move a type error to runtime.
Failure case
An audit tool lists methods on a fee provider and reports two rate methods. Only one was written by the developer. The second is a compiler bridge that preserves polymorphism; registering both as separate handlers would double-count one operation.
Java code
import java.lang.reflect.Method;
public class FeeBridgeInspection {
static abstract class FeeSource<T> { abstract T rate(); }
static final class FixedFee extends FeeSource<Long> {
@Override Long rate() { return 4700L; }
}
public static long rateThroughBase() {
FeeSource<Long> source = new FixedFee();
return source.rate();
}
public static int bridgeCount() {
int bridges = 0;
for (Method method : FixedFee.class.getDeclaredMethods()) {
if (method.isBridge()) bridges++;
}
return bridges;
}
}Performance and ownership cost
The virtual call is ordinary dispatch after compilation. Reflecting over M declared methods costs O(M) time and O(1) application space here, aside from reflection's method objects. Bridge generation is a compatibility detail, not a reason to add runtime casts to correctly typed code.
Common Mistakes
- Do not expose compiler bridge methods as separate service endpoints.
- Do not assume generic parameters survive as distinct runtime classes.
- Do not remove an override because reflection shows an extra method.
