Reflection of the use of generics and reflection-generic

Source: Internet
Author: User
Tags class definition comparable
The previous article sums up the generics, and this article is about the reflection of the generic type.
I mentioned in the generic article that generics are erased in the JVM, but the erased class is still "residue" of some generic traces. For example, the Person<t> class, although wiped out the generics <T>, but through the reflection mechanism can obtain this information, not only that, the person class definition of the generic method can be obtained through reflection. Let's use the code to verify that.
Before using code validation, elephants will slightly change the person class, add a generic variable K and a generic method, and the rest remain unchanged.
public class person<t, K extends Comparable<? Super T> & Serializable>
Extends Superperson<string> implements handle<date> {
......
public static <t extends Comparable<?  Super t>> T Max (list<? Extends t> list) {
iterator<? Extends t> it = List.iterator ();
T result = It.next ();
while (It.hasnext ()) {
T t = It.next ();
if (T.compareto (Result) > 0)
result = t;
}
return result;
}
......
}
Can you start writing code here? Don't worry, elephants also want to first use the code to use a few reflection-related API interface to illustrate that this is JDK5.0 for generics, and then also used a JDK6.0 provided Modifier, it is an enumeration type, can represent the class, method or field modifiers.
Type
It is a public interface of all types. Includes the original type, the parameterized type, the array type, the type variable, and the base type. Parameterizedtype, typevariable, wildcardtype,genericarraytype these four interfaces are its sub-interfaces.
Genericdeclaration
This interface Class, method, constructor all have implementations, we are to use this interface of the Gettypeparameters methods, it returns a typevariable[] array, which is our definition of type variables T and K, CIS The order is the same as when we declare it. If you print the array with a looping statement, you will find that only the output of T and K is not what we want, so what if we want to get the desired result? Please continue to look down.
Typevariable
It represents a type variable.  such as T, such as K extends Comparable<? Super T> & Serializable, this interface has a getbounds () method, which is used to obtain the upper bounds of type array, if the upper limit is not defined, the default is set to Object, please note typevariable Is the interface, actually get the Typevariableimpl implementation class, the following interfaces are the same.
Take T and K to show that T does not define any caps, so it has a default upper Java.lang.Object, when you actually trace the code you will find that the bounds property of T is empty, and only after the GetBounds () method is invoked will there be a type[1] array [ Class Java.lang.Object]. And for K, when the GetBounds method is invoked, the resulting array is [java.lang.comparable< Super T>, interface java.io.Serializable], and their types are different. The 1th one is Parameterizedtype, and the second is class.
Parameterizedtype
Parameterizedtype represents a parameterized type, which is the java.lang.comparable&lt mentioned above;? Super T&gt, such as List<t>,list<string&gt, are called parameterized types. Get comparable<? After Super t>, then call Getrawtype () and Getactualtypearguments () Two methods, you can get the class (java.lang.Comparable) that declares this parameterized type and an array of actual type arguments ([? Super T]), and this? Super T is also a wildcardtype type.
Wildcardtype
It is used to describe wildcard expressions, which are returned above? Super T happens to be this type. Then call the Getupperbounds () upper limit and the getlowerbounds () lower bound to get the type variable? The qualified type (upper and lower), and for this example wildcard character (?), its upper bound is java.lang.Object, and the lower bound is T
Through the analysis of the above several interfaces, you can resolve the generic parameters of the person class, then the superclass of the person and how to implement the interface. The class class also adds Getgenericsuperclass () and Getgenericinterfaces () two methods in version 1.5 to return the superclass and interface with parameterized types.
At this point, through the above interfaces and methods we can already parse the code behind the class person, and the method inside the class is similar to the generic parameter of the parsing class, and it is no longer to repeat. The following figure is the result of printing the defined person class by reflection.

This article mainly wants to verify some knowledge of the generics in the JVM virtual machine by reflection mechanism. Most of the time is actually used by reflection to get the superclass generics, or through the reflection call method, read/Set property values and so on these functions, the bottom of the sample source download.
While reflection is useful, it cannot be abused. There is no way to use reflection at compile-time type check, and the reflection of the code is more complex is not easy to read, but fortunately there are many already encapsulated reflective tool class, to help us do a lot of work in this area. The last and most important point is that the use of reflection will have a certain performance loss, that is, slower than the direct call method, as to how much slower, this is not easy to say, but it will be slow, so unless necessary, the elephant recommends that in general the first consideration to replace the reflection of the interface.
These are all related to the general reflection of the introduction, is an introductory knowledge, there is nothing wrong, or imperfect place, but also please point out, thank you.
SOURCE download: Generics and reflection
This article for the original pineapple elephant, if you want to reprint please specify the source. Http://www.blogjava.net/bolo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.