The type of the origin of the type and its sub-interfaces before generics occur
When there are no generics, only the original type. At this point, all primitive types are abstracted through the bytecode file class class. A specific object of class is represented by a specified primitive type.
Types after generics appear
After the generics appear, the data types are expanded. The parameterized type, type variable type, qualifier type, generic array type are expanded from only the original type.
The reason that the type associated with the generic cannot be uniform to class with the original type
- Causes of generic Erasure
Both the original type and the newly generated type should be unified into their respective bytecode file type objects. But because generics are not components of the original java. If you do join generics, it is very deadly to involve the modification of the JVM instruction set.
- How to introduce generics in Java
In order to use generics and not really introduce generics, Java uses a generic erase mechanism to introduce generics. Generics in Java are only used by compiler javac, ensuring data security and eliminating the hassle of forcing type conversions. However, once the compilation is complete, all and generics-related types are erased.
- Class cannot express the type associated with a generic type
Therefore, parameterized types related to generics, type variable types, qualifier types, generic array types are all returned to their original form after compilation, in the bytecode file are all primitive types erased by generics, and there is no bytecode file corresponding to their type. Therefore, the newly-expanded types associated with generics cannot be unified into class classes.
- Representation of generics-related types in Java
To manipulate these types to reflect the actual development needs through reflection, Java has added ParameterizedType, TypeVariable<D>, GenericArrayType, WildcardType several types to represent types that cannot be grouped into class classes but are also the same as the original type.
- Reason for introducing type
For the extensibility of the program, the type interface is eventually introduced as the Class和ParameterizedType, TypeVariable<D>, GenericArrayType, WildcardType total parent interface for these kinds of types. This allows you to accept the arguments of the above five seed classes with the type parameter or the type of the return value as a parameter of type. Unifies the type and primitive type class associated with generics
- Reason for no method in type interface
As seen from the above, the appearance of type only acts as a function of improving the program extensibility through polymorphism. Therefore, there is no method in the source code of the type interface.
Public Static voidgettypeparameters () {List<Integer> list =NewArraylist<integer>(); Map<integer, string> map =NewHashmap<integer, string>(); System.out.println (Arrays.tostring (List.getclass (). Gettypeparameters ())); [E] System.out.println (arrays.tostring (Map.getclass (). Gettypeparameters ())); [K, V]}/*** The most critical difference is that the variable declaration in this section has a pair of curly braces * The variable declaration actually creates an anonymous inner class, which is a subclass of HashMap * * Java introduces generic erasure because it avoids the need to create the runtime by introducing generics The class you want. So we can actually get these generic information at run time by defining the class, preserving the generic information in the * class information * * In short, Java generic erase is scoped, that is, generics in the class definition are not erased **/ Public Static voidgetactualtypearguments () {//map<string, integer> Map = new hashmap<string, integer> ();map<string, integer> map =NewHashmap<string, integer>() {}; Type type=Map.getclass (). Getgenericsuperclass (); Parameterizedtype Parameterizedtype= Parameterizedtype.class. Cast (type); for(Type typeArgument:parameterizedType.getActualTypeArguments ()) {System.out.println (typeargument.gettype Name ()); Java.lang.String, Java.lang.Integer}}
Reference:
The type of Java is detailed
JSON serialization and deserialization (-) generics and Java.lang.reflect.Type