(1). Interface types related to reflection + generics
Common parent interfaces for all types in the Java.lang.reflect.Type:java language
Java.lang.reflect.ParameterizedType
Java.lang.reflect.GenericArrayType
Java.lang.reflect.WildcardType
1. Type Direct sub-interface
Parameterizedtype,genericarraytype,typevariable and Wildcardtype four types of interfaces
Parameterizedtype: Represents a parameterized type, such as collection
Genericarraytype: Indicates that an element type is an array type of a parameterized type or a type variable
Typevariable: is a common parent interface for various types of variables
Wildcardtype: Represents a wildcard type expression, such as a?,? Extends number, huh? Super Integer "Wildcard is a word:" wildcard character ""
2. Type implements subclasses directly: Class class
3. Java.lang.reflect.Type interface
Type all types refer to the original type (raw types) "corresponding class", the parameterized type (parameterizedtypes) "corresponds to Parameterizedtype", the array type (arrays types) " corresponding to Genericarraytype ", type variable (type variables)" corresponds to Typevariable ", basic data Type (primitivetypes)" Still corresponds to class "
4. Java.lang.reflect.ParameterizedType interface
The meaning of the Parameterizedtype interface type
Represents a parameterized type. For example, the parameterized type of map
Get the actual type in the parameterized type <>
SOURCE statement: type[] getactualtypearguments ();
"Note" No matter how many layers <> nesting in <>, this method only takes off the outermost <> after the remainder of the content as the return value of this method.
public static E Methodiv (
Arraylist> Al1,
ArrayList Al2,
ArrayList Al3,
Arraylistextends number> Al4,
ArrayList al5) {}
Each of his parameters is, in general, a parameterized type.
{1}. For Arraylist>, after Getactualtypearguments (), after removing the outermost <>, the remaining type is ArrayList. So the return type for this parameter is Parameterizedtype.
{2}. For ArrayList, after Getactualtypearguments (), after removing the outermost <>, the remaining type is E. So the return type for this parameter is typevariable.
{3}. For ArrayList, after Getactualtypearguments (), after removing the outermost <>, the remaining type is string. So the return type for this parameter is class.
{4}. For Arraylistextends Number>, after removing the outermost <> after getactualtypearguments (), what is the remaining type? Extendsnumber. So the return type for this parameter is Wildcardtype.
{5}. For ArrayList, after Getactualtypearguments (), after removing the outermost <>, the remaining type is e[]. So the return type for this parameter is Genericarraytype.
Therefore, it is possible to obtain various types of actual parameters, so in order to unify, adopt direct parent class array type[] to receive.
4. Java.lang.reflect. Genericarraytype interface
The meaning of the Genericarraytype interface type
Represents a generic array type. For example: Void Method (Arraylist[] al) {...}
"Note" <> cannot appear in the initialization of the array, that is, <> cannot occur after the new array, otherwise javac cannot pass. However, as a reference variable or a parameter of the method is perfectly OK.
Gets the type of an element in a generic array
SOURCE statement: Type Getgenericcomponenttype ();
"Note" Whether there are several [] sides from left to right, this method only takes off the rightmost [] after which the remainder is the return value of this method.
Why are the return value types type?
public static E METHODV (
string[] p1,
e[] P2,
arraylist[] P3,
e[][] p4) {}
{1}. For string[], after you return through Getcomponenttype (), the remaining type is string after you remove the rightmost []. So the return type for this parameter is class
{2}. For e[], after returning via Getcomponenttype (), after removing the rightmost [], the remaining type is E. So the return type for this parameter is typevariable
{3}. For arraylist[], after the Getcomponenttype () is returned, after removing the rightmost [], the remaining type is ArrayList. So the return type for this parameter is Parameterizedtype
{4}. For e[][], after you return via Getcomponenttype (), the remaining type is e[] After you remove the rightmost []. So the return type for this parameter is Genericarraytype
5. Java.lang.reflect. Genericarraytype interface
The meaning of the Typevariable interface type
Represents a type parameter or is also called a type variable. For example, the E in void method (E e) {} is a type variable
Gets the type of the upper boundary of a generic qualified type variable
SOURCE statement: type[] getactualtypearguments ();
"Note" This is just the upper boundary. The reason is that a type variable can be defined only by using extends for (multiple) boundary limits. Cannot use Super, otherwise compilation cannot pass. At the same time, extends gives the upper boundary of the type variable.
Why is the return type an array? Because a type variable can be qualified with multiple upper bounds by &, there are multiple upper bounds, so the return value type is an array type [].
For example, the following method:
public static extends map& cloneable&serializable> e Methodvi (e e) {...}
The first upper boundary of E is map, which is the Parameterizedtype type
The second upper boundary of E is cloneable, which is class type
Therefore, for unification, the element type of an array of returned values is
6. Java.lang.reflect.WildcardType interface
The meaning of the Wildcardtype interface type
An expression that represents a wildcard type.
such as void Printcoll (arraylistal); In the? Extends number
Note According to the comments on the API above: At this stage, the wildcard expression only accepts an upper or lower boundary, and it is different to define a type variable when multiple upper bounds can be specified. But the API says, in order to maintain extensibility, the return value type is written in an array form. Actually, the size of the array returned now is 1.
Gets the type of the upper boundary of the generic qualification of a wildcard expression object
SOURCE statement: type[] Getupperbounds ();
The "note" above says that the size of the array in the type[] returned at this stage is one. Written as type[] is an extension for language upgrades.
For example, the following method:
{1}. public static Voidprintcoll (Arraylistextends arraylist> al) {}
Wildcard expressions are:? Extendsarraylist, so that extends is the upper boundary of the upper boundary, which is the Parameterizedtype type.
{2}. public static Voidprintcoll (Arraylistextends e> al) {}
Wildcard expressions are:? Extends E, so that extends is behind the upper boundary of the?, this upper boundary is the typevariable type
{3}.public static voidprintcoll (Arraylistextends e[]> al) {}
Wildcard expressions are:? Extends e[] so that extends is the upper boundary of the upper boundary, which is the Genericarraytype type
{4}.public static voidprintcoll (Arraylistextends number> al) {}
Wildcard expressions are:? Extends number, so that the upper boundary of the extends is followed by the upper boundary, which is the class type
The element type that is eventually unified into type as an array.
7. The origin of type and its sub-interface
I. Type before the generics appear
When there is no generics, only the original type is called. At this point, all of the original types are abstracted through the bytecode file class class classes. A specific object of class classes represents a specified original type.
Two. Type after generics appear
After generics appear, the data type is expanded. From only the original type, the parameterized type, type variable type, generic qualified parameterized type (with wildcard + wildcard qualifier expression), and generic array type are expanded.
Three. Why a generic type cannot be unified with the original type to class
[1]. "Cause of generic erasure"
Originally, the newly generated type + original type should be unified into the respective bytecode file type object. However, because generics are not components in the original java. If generics are really added, it is very fatal to involve modifications to the JVM's instruction set.
[2]. "How to introduce generics in Java"
In order to use the advantages of generics and not really introduce generics, Java uses the mechanism of generic erasure to introduce generics. Generics in Java are only used for compiler javac, ensuring data security and eliminating the hassle of forced type conversions. However, once the compilation is complete, all types related to the generic type are erased.
[3]. "Class cannot express the type associated with a generic type"
Therefore, the parameterized types associated with generics, type variable types, generic qualified parameterized types (including wildcard + wildcard qualifier expressions), generic array types All of these types are typed back to their original form, and in bytecode files all are primitive types that have been erased by generics and do not have byte-code files that are consistent with their own type. So the newly-expanded type associated with generics cannot be unified into class classes.
(4). Representation of generics-related types in Java
In order to manipulate these types of reflection to meet the actual development needs, Java has added parameterizedtype,genericarraytype,typevariable and wildcardtype several types to represent types that cannot be categorized into class classes but are similar to the original type.
(5). Introduction of type: Unifying types and primitive type classes related to generics
"Reason for introducing type"
For the extensibility of the program, the type interface is introduced as the total parent interface of the class,parameterizedtype,genericarraytype,typevariable and Wildcardtype. This implements the type parameter to accept the arguments of the above five seed classes or the return value type is the type of parameter.
"Reason for no method in type interface"
From the above, the appearance of type only plays the role of increasing program extensibility through polymorphism, no other function. Therefore, there is no method in the source code of the type interface.