qq_33466409
qq_33466409
采纳率100%
2015-12-28 09:36 阅读 2.1k
已采纳

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

50

有如下的泛型类:

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条回答 默认 最新

  • 已采纳
    u013740988 qq_335498556 2015-12-28 09:47

    用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

    }

    点赞 2 评论 复制链接分享

相关推荐