Java generic type System

Source: Internet
Author: User

Type is the top-level interface in the Java type information architecture, where class is a direct implementation class of type. In addition, type has four direct sub-interfaces: Parameterizedtype,typevariable,wildcardtype,genericarraytype.

Cite this man's introduction to these interfaces:

Type
It is a public interface of all types. Includes primitive types, parameterized types, array types, type variables, and base types.Parameterizedtype, typevariable, wildcardtype,genericarraytype these four interfaces are its sub-interfaces.
genericdeclaration
This interfaceClass,Method,Constructor, we're going to use this interface.Gettypeparameters method, which returns aTypevariable[] Array, in which we define the type variablesT andK, in the same order as we declare. If you print the array with a looping statement, you will see that only the outputT andK, that's not the result we want, so what do we do to get the results we expect? Please keep looking down.
typevariable
It represents a type variable. Like whatT, for exampleKExtendscomparable<?SuperT> & Serializable, this interface has aGetBounds() method, which is used to obtain the upper bound of the type variable.Type array, if no upper bounds are defined, the default setting isObject, please noteTypevariable is an interface that actually gets theTypevariableimpl implementation class, the following interfaces are the same.
Take T and K to illustrate that T does not define any upper bound, so it has a default upper Java.lang.Object, the actual tracking code when you will find that the bounds property of T is empty, only after calling the GetBounds () method, there will be a type[1] array [ Class Java.lang.Object]. For K, when the GetBounds method is called, 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 one is class.
Parameterizedtype
Parameterizedtype represents the parameterized type, which is said above Java.lang.comparable< Super T> and, again, List<t>,list<string> these are called parameterized types. Get comparable<? After Super t>, call Getrawtype () and Getactualtypearguments () two methods to get a 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 a wildcard expression, which is returned above? Super T is exactly this type. Then call the Getupperbounds () Upper and getlowerbounds () lower bound methods, get the type variable? The qualified type (the upper and lower bound), for this example of the wildcard (?), its upper limit is java.lang.Object, the lower bound is t
Through the analysis of the above several interfaces, you canIf the generic parameters of the person class are resolved, what should be handled by the superclass of person and the interface of the implementation? The class class also adds the getgenericsuperclass () and getgenericinterfaces () two methods in version 1.5 to return the superclass and interface with parameterized types.

Genericarraytype is actually a generic array type.

We say that class to some extent saved the erased type information, we can get the erased type parameter information through these interfaces, these interfaces are simply a classification of type parameters, through some of the methods they provide, we can gradually get to the most primitive type parameters of the class object.

Specific instructions and APIs you can go to the documentation, I'm documenting a practical application, and of course the applications abound in various frameworks.

In the DAO layer of Java EE we typically encapsulate a generic generic basedao, which can implement a basic crud for various entities such as user,order, and then concrete userdao,orderdao and so on will inherit it and provide other DAO methods:

[Java]View PlainCopy
    1. Public class Userdao extends basedao<user>{}


I use the Basedao is based on the dbutils, it requires the entity's class object in order to make a common query method, such as user class object, we can through the constructor, function parameters and other means to pass to Basedao, but with reflection, can be more elegant implementation.

[Java]View PlainCopy
    1. Public class Basedao<t> {
    2. private class<t> Clszz;
    3. Public Basedao () {
    4. Type type = this.getclass (). Getgenericsuperclass (); Get the generic parent class with the type parameter
    5. if (type instanceof parameterizedtype) {//This type object is declared according to a generic type, it is possible that one of the 4 interfaces, if it is basedao<user> this form
    6. Parameterizedtype Parameterizedtype = (parameterizedtype) type;
    7. type[] actualtypearguments = parameterizedtype.getactualtypearguments (); //Gets the type parameter array of the generic type
    8. if (actualtypearguments! = null && actualtypearguments.length = = 1) {
    9. if (actualtypearguments[0] instanceof Class) {//type parameter also may not be class type
    10. This.clszz = (class<t>) actualtypearguments[0];
    11. }else{
    12. ///For example: Basedao<basedao<user>>, the obtained is not a class, but also Parameterizedtype, that is, nested
    13. Parameterizedtype, peel off one layer at a to be able to get the user's class object eventually
    14. }
    15. }
    16. }
    17. }
    18. Public T Get (String sql,object...params) {
    19. Queryrunner qr = new Queryrunner ();
    20. T obj;
    21. Connection Connection;
    22. try {
    23. Connection = Jdbcutil.getconnection ();
    24. obj = Qr.query (connection,sql,new Beanhandler<t> (CLSZZ), params);
    25. } catch (SQLException e) {
    26. E.printstacktrace ();
    27. return null;
    28. }
    29. return obj;
    30. }
    31. }

Java generic type System

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.