What is Java Bridge Method (ACC_BRIDGE)?

Java method could be tagged with bridge (ACC_BRIDGE) in the .class file, denotes this method is generated by Java Compiler for type erasure purpose of Java Generics.

Code is the best document, so let's check the code of java.util.ArrayDeque as an example for the details.

In the source code of ArrayDeque, there is only one (1) clone() method defined, as bellow:


    public ArrayDeque<E> clone() {
        try {
            @SuppressWarnings("unchecked")
            ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
            result.elements = Arrays.copyOf(elements, elements.length);
            return result;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }

Well in the generated the .class file for ArrayDeque, there are two (2) clone() methods, as the generated UML diagram from the ArrayDeque.class file shown:

UML Diagram of ArrayDeque.class

Here are the details of these two clone() methods:
  1. public ArrayDeque<E> clone()
    which exists in the original Java source code
  2. public Object clone()
    which is generated by the Java compiler, this method is doing nothing but simply calling the other method.

The reason to generate the 2nd clone() method is to keep the agreement of Polymorphism, since the clone() method is defined in the parent class Object of ArrayDeque.

  • protected native Object clone() throws CloneNotSupportedException;

UML Hierarchy Diagram for Java class ArrayDeque


Comments

Popular posts from this blog

What is Java Synthetic Class (ACC_SYNTHETIC)? with the example of BoundMethodHandle$1.class in Java package java.lang.invoke

Evolution of Java Dependency Diagrams, from Java 7 to Java 13