简体   繁体   English

javap和泛型的类型擦除

[英]javap and generics' type erasure

I am reading Herbert Schilds about type erasure in generics in java. 我正在阅读有关Java泛型中的类型擦除的Herbert Schilds。 Supposedly running javap on a class should give me the bytecode information about public, package protected and protected fields and methods after type erasure. 假设在类上运行javap应该为我提供有关类型擦除后有关public,包保护和受保护字段以及方法的字节码信息。 However, I wrote the following class: 但是,我编写了以下课程:

    class Ambiguity<T, V extends String>{
    T ob1;
    V ob2;

    void set(T o){
        ob1 = o;
    }

    void set(V o){
        ob2 = o;
    }
}

and ran javap on the class file that was generated and got the following output 并在生成的类文件上运行javap并获得以下输出

Compiled from "Test.java" 从“ Test.java”编译

class Ambiguity<T, V extends java.lang.String> {
  T ob1;
  V ob2;
  Ambiguity();
  void set(T);
  void set(V);
}

I was expecting an output that looked like this based on what I read. 根据我阅读的内容,我期待的输出看起来像这样。

Compiled from "Test.java"
class Ambiguity<java.lang.Object, java.lang.String> {
  java.lang.Object ob1;
  java.lang.String ob2;
  Ambiguity();
  void set(java.lang.Object);
  void set(java.lang.String);
}

Am I missing something here? 我在这里想念什么吗? I should add that I understand that it is not a good practice to overload methods in the above manner. 我应该补充一点,我理解以上述方式重载方法不是一个好习惯。 I was just seeing interested in seeing the results of javap under this ambiguity. 我只是看到有兴趣在这种歧义下看到javap的结果。

EDIT: This seems to be a result of a new fix in javap. 编辑:这似乎是javap中新修复程序的结果。 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4870651 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4870651

If I run javap from JDK 1.6 I get the results as I was expecting. 如果我从JDK 1.6运行javap,则会得到预期的结果。 If I run javap from JDK 1.7 b30 which was what I was using initially, I get the result with the generic information. 如果我从最初使用的JDK 1.7 b30运行javap,则将获得带有通用信息的结果。

I'm not sure. 我不确定。 But it seems signature attribute was introduced in jvm 7 (refer jvm specification) . 但似乎signature属性是在jvm 7中引入的(请参阅jvm规范)

This attribute will capture signature information used for debugging and reflection api. 此属性将捕获用于调试和反射api的签名信息。

To see signature attribute use javap -v <class> 要查看signature属性,请使用javap -v <class>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM