We all know that Java source code will erase the generic information after compiling, now see a generic type is not erased, see the Concurrenthashmap.comparableclassfor method.
Parameterizedtype.getactualtypearguments can get generic information that is not erased in the interface implemented by the class. The experiment is as follows:
Public classParameterized { Public Static voidMain (string[] args) {type[] types= Integer.class. Getgenericinterfaces ();//type[] types = HashMap.class.getGenericInterfaces (); for(Type Type:types) {System.out.println (type); if(TypeinstanceofParameterizedtype) { Parameterizedtype P = (parameterizedtype) type; System.out.println ("-rawtype:" +P.getrawtype ()); System.out.println ("-ownertype:" +P.getownertype ()); type[] Typeargs = p.getactualtypearguments (); System.out.println ("-actualtypearguments:" +arrays.tostring (Typeargs)); } } }}
Output:
Integer.class
Interfacenull -actualtypearguments:[class Java.lang.Integer]
Hashmap.class
Interfacenull -actualtypearguments:[k, V]interface Java.lang.CloneableInterface java.io.Serializable
Types of Java generics are not erased