javap and generics' type erasure

2019-01-25 13:44发布

问题:

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>