java 泛型擦除发生在哪个阶段,如何用反编译工具查看泛型擦除后的代码?

有如下的泛型类:

public class Pair
{
public Pair() { first = null; second = null; }
public Pair(T first, T second) { this.first = first; this.second = second; }

public T getFirst() { return first; }
public T getSecond() { return second; }

public void setFirst(T newValue) { first = newValue; }
public void setSecond(T newValue) { second = newValue; }

private T first;
private T second;
}

用javac编译后再用javap 查看代码,如下:

Compiled from "Pair.java"
public class Pair {
public Pair();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."":()V
4: aload_0
5: aconst_null
6: putfield #2 // Field first:Ljava/lang/Object;
9: aload_0
10: aconst_null
11: putfield #3 // Field second:Ljava/lang/Object;
14: return

public Pair(T, T);
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."":()V
4: aload_0
5: aload_1
6: putfield #2 // Field first:Ljava/lang/Object;
9: aload_0
10: aload_2
11: putfield #3 // Field second:Ljava/lang/Object;
14: return

public T getFirst();
Code:
0: aload_0
1: getfield #2 // Field first:Ljava/lang/Object;
4: areturn

public T getSecond();
Code:
0: aload_0
1: getfield #3 // Field second:Ljava/lang/Object;
4: areturn

public void setFirst(T);
Code:
0: aload_0
1: aload_1
2: putfield #2 // Field first:Ljava/lang/Object;
5: return

public void setSecond(T);
Code:
0: aload_0
1: aload_1
2: putfield #3 // Field second:Ljava/lang/Object;
5: return
}

说好的类型擦除呢?怎么才能看到类型擦除后的代码?

1个回答

用javap的-s参数。题主给的javap输出是 javap -c 得到的。用 javap -c -s 就会看到多了一些Signature行,它们其中一些是擦除后的signature。例如这里: public void setFirst(T);
Signature: (Ljava/lang/Object;)V
这Signature行里就是 void (Object) 而不是 void (T) 了。还有在字节码(方法体)里面您也是看不到泛型的,因为已经被擦除了。在这个层面上保留的泛型信息只有调试用的LocalVariableTypeTable(LVTT)里。LVTT用 javap -verbose 看。$ javap -c -s Pair
Compiled from "Pair.java"
public class Pair {
public Pair();
Signature: ()V
Code:
0: aload_0

1: invokespecial #1 // Method java/lang/Object."":()V
4: aload_0

5: aconst_null

6: putfield #2 // Field first:Ljava/lang/Object;
9: aload_0

10: aconst_null

11: putfield #3 // Field second:Ljava/lang/Object;
14: return

public Pair(T, T);
Signature: (Ljava/lang/Object;Ljava/lang/Object;)V
Code:
0: aload_0

1: invokespecial #1 // Method java/lang/Object."":()V
4: aload_0

5: aload_1

6: putfield #2 // Field first:Ljava/lang/Object;
9: aload_0

10: aload_2

11: putfield #3 // Field second:Ljava/lang/Object;
14: return

public T getFirst();
Signature: ()Ljava/lang/Object;
Code:
0: aload_0

1: getfield #2 // Field first:Ljava/lang/Object;
4: areturn

public T getSecond();
Signature: ()Ljava/lang/Object;
Code:
0: aload_0

1: getfield #3 // Field second:Ljava/lang/Object;
4: areturn

public void setFirst(T);
Signature: (Ljava/lang/Object;)V
Code:
0: aload_0

1: aload_1

2: putfield #2 // Field first:Ljava/lang/Object;
5: return

public void setSecond(T);
Signature: (Ljava/lang/Object;)V
Code:
0: aload_0

1: aload_1

2: putfield #3 // Field second:Ljava/lang/Object;
5: return

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问