簡單驗(yàn)證
public interface SuperClass<T> {
public T Apply(T t) ;
}
public class SonClass implements SuperClass<String> {
public String Apply(String str) {
return str;
}
}
運(yùn)行javap -c -v SuperClass.class SonClass.class
查看編譯后的結(jié)果 可以看到SonClass額外生成了一個(gè)Object Apply (Object obj)
的橋接方法
.
.
.
{
public abstract T Apply(T);
descriptor: (Ljava/lang/Object;)Ljava/lang/Object;
flags: ACC_PUBLIC, ACC_ABSTRACT
MethodParameters:
Name Flags
t
Signature: #8 // (TT;)TT;
}
Signature: #9 // <T:Ljava/lang/Object;>Ljava/lang/Object;
SourceFile: "SuperClass.java"
.
.
.
{
public com.example.bridge.demo.SonClass();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 6: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/example/bridge/demo/SonClass;
public java.lang.String Apply(java.lang.String);
descriptor: (Ljava/lang/String;)Ljava/lang/String;
flags: ACC_PUBLIC
Code:
stack=1, locals=2, args_size=2
0: aload_1
1: areturn
LineNumberTable:
line 9: 0
LocalVariableTable:
Start Length Slot Name Signature
0 2 0 this Lcom/example/bridge/demo/SonClass;
0 2 1 str Ljava/lang/String;
MethodParameters:
Name Flags
str
public java.lang.Object Apply(java.lang.Object);
descriptor: (Ljava/lang/Object;)Ljava/lang/Object;
flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC
Code:
stack=2, locals=2, args_size=2
0: aload_0
1: aload_1
2: checkcast #2 // class java/lang/String
5: invokevirtual #3 // Method Apply:(Ljava/lang/String;)Ljava/lang/String;
8: areturn
LineNumberTable:
line 6: 0
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 this Lcom/example/bridge/demo/SonClass;
MethodParameters:
Name Flags
str synthetic
}
Signature: #21 // Ljava/lang/Object;Lcom/example/bridge/demo/SuperClass<Ljava/lang/String;>;
SourceFile: "SonClass.java"
分析
我們都知道JAVA泛型并不是真正的泛型,在編譯完成后泛型會(huì)被擦除,變成Object嫉鲸。如果沒有橋接方法,編譯完成后其實(shí)子類并沒有實(shí)現(xiàn)父類方法拯啦。所以為了語義,編譯器會(huì)自動(dòng)生成橋接方法熔任,來保證兼容性褒链。