public class K_GenricTypes {
public static void method(List list) {
System.out.println("method");
}
}
Compile the above categories:. Javac -encoding utf-8 \ K_GenricTypes.java
After generating class decompile: javap -verbose \ K_GenricTypes.class.
Write method with a method of the generic type, then compile such, decompile class
public class K_GenricTypes {
public static void method(List<String> list) {
System.out.println("method");
}
}
These two classes method Comparative method in the compiled code trans
Generic method method (List <String> list) with non-generic method method (List list) Contrast:
1, code exactly the same properties
2, a more generic method Signature properties than non-generic method.
Because generic java language used is pseudo-erased generic implementation method, bytecodes (Code attribute), followed by the generic information (type variable, parameterized types) have all been compiled erased. Therefore, for the operation of the Java language is, List <String>, List <Integer> is the same class.
In order to compensate for the lack erased, added Signature Properties. Signature role attribute is stored in a signature method wherein bytecode level, This property holds the parameter types are not native types, including but parameterized type.
At runtime, the generic information may be acquired by reflection, for example:
public class K_GenricTypes {
public static void method(List<String> list) {
System.out.println("method");
}
}
class K_GenricTypesMain {
public static void main(String[] args) throws Exception{
Method method = K_GenricTypes.class.getMethod("method",List.class);
Type[] types = method.getGenericParameterTypes();
for (Type type : types) {
System.out.println("参数类型:"+type);
//参数类型:java.util.List<java.lang.String>
if(type instanceof ParameterizedType){
Type[] paramType = ((ParameterizedType) type).getActualTypeArguments();
for (Type t : paramType) {
System.out.println("泛型类型:"+t);
//泛型类型:class java.lang.String
}
}
}
}
}