For generics, you must know the class type. However, writing T. Class directly is obviously not acceptable. I checked a lot of information from the Internet and found only one result. Because Java's generic implementation uses the "wiping method" (I did not go into details, haha ), as a result, Java generics cannot directly obtain their declared generics.
However, I found something useful in Jiangnan's blog: Using Reflection to get "T. Class ".
Address: http://www.blogjava.net/calvin/archive/2009/12/10/43830.html
We mainly use the following sentence:
Class <t> entityclass = (class <t>) (parameterizedtype) getclass (). getgenericsuperclass (). getactualtypearguments () [0]; <br/>
I have queried Java APIs. There are two methods in the class: getgenericinterfaces () and getgenericsuperclass ()
Let's take a look at the usage of both methods:
1. Public type getgenericsuperclass ()
Used to return the currentClassDirectly superclass of the object (class, interface, basic type, or void)Type. If the direct superclass are parameterized, the returned type object must be clearly reflected in the type used for declaration in the source code. For example:
Package COM. mot. hyena. test; <br/> Import Java. lang. reflect. parameterizedtype; <br/> public class gt1 extends gt2 <integer >{< br/> Public static void main (string [] ARGs) {<br/> system. out. println (parameterizedtype) New gt1 (). getclass (). getgenericsuperclass (); <br/>}< br/>}
The output result is: COM. Mot. hyena. Test. gt2 <java. Lang. Integer>
If this class representsObjectClass, interface, basic type, or void, return null. If this object represents an array class, the return value indicatesObjectClassClassObject.
2. Public type [] getgenericinterfaces ()
Similar to the method above, the Java class can implement multiple interfaces, so the returned type must be stored in an array.
The above two methods return type objects or arrays. In our topic, class represents the parameterized type, so we can cast the type object as a parameterizedtype object. The parameterizedtype object has a method, getactualtypearguments ().
Public type [] getactualtypearguments ()
Returns an array of type objects. This array represents the type actually used in the type declaration. Next, use the example above:
Package COM. mot. hyena. test; <br/> Import Java. lang. reflect. parameterizedtype; <br/> public class gt1 extends gt2 <integer >{< br/> Public static void main (string [] ARGs) {<br/> system. out. println (parameterizedtype) New gt1 (). getclass (). getgenericsuperclass ()). getactualtypearguments () [0]); <br/>}< br/>}
The result will be: Class java. Lang. Integer
Therefore, we can inherit from the + Reflection Method to T. Class.
It should be noted that the key statement used by Jiangnan Baiyi
Class <t> entityclass = (class <t>) (parameterizedtype) getclass (). getgenericsuperclass (). getactualtypearguments () [0];
Put it in the constructor of the super class, that is, the class that declares the generic type. In this way, when a subclass inherits a generic superclass, it automatically calls the constructor of the superclass.In the constructor of this superclass, The getclass that is called returns the class type of the subclass.(It is different from the usual rewrite mechanism. Well, it is worth further research, but the test result is true.) in this case, you do not need to explicitly use methods such as getgenericinterfaces () and getgenericsuperclass () in the subclass.
Then, use (class <t>) to perform casting on the elements returned by getactualtypearguments () to obtain the so-called T. Class.