qq_33466409 2015-12-28 09:36 采纳率: 100%
浏览 2188
已采纳

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
}

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

  • 写回答

2条回答 默认 最新

  • 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

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 加一个K2暂停音乐的功能
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?