javap and generics' type erasure

岁酱吖の 提交于 2019-11-28 00:31:33

问题


I am reading Herbert Schilds about type erasure in generics in java. Supposedly running javap on a class should give me the bytecode information about public, package protected and protected fields and methods after type erasure. 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

Compiled from "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.

EDIT: This seems to be a result of a new fix in javap. 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. If I run javap from JDK 1.7 b30 which was what I was using initially, I get the result with the generic information.


回答1:


I'm not sure. But it seems signature attribute was introduced in jvm 7 (refer jvm specification).

This attribute will capture signature information used for debugging and reflection api.

To see signature attribute use javap -v <class>



来源:https://stackoverflow.com/questions/15908771/javap-and-generics-type-erasure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!