javap and generics' type erasure
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.
At compile time -- in the generated bytecode -- classes will, yes, retain all of their generic type information. What you've seen is exactly what you should expect.
The difference is that types are erased at runtime: for example, an instance of the class Ambiguity<Integer, String>
will not know that its type arguments are Integer
and String
respectively.
上一篇: 图标存在于系统托盘中?
下一篇: javap和泛型'类型擦除