tamping Java Basic Two--reflection (2): Generic related peripheral information acquisition

Source: Internet
Author: User

Preface: Believe oneself firmly, insist oneself insist, always choose to believe oneself.

In the previous article, we briefly explained how to use reflection to get the usage of ordinary types of classes, and today we explain how to use reflection to get the information in generics. In advance to wake up, the content of this article is slightly difficult, we may need to see a few more.
This article will be a large number of generic knowledge, if the generic declaration and fill the students do not know very well, please read the "tamping java one of the basic--generic series"
First , to obtain the generic superclass and interface of the trust information in this section, we will describe how to obtain generic superclass and interface, the last two functions left in the previous chapter to finish.
1. Get generic Super Class trust information in the previous chapter, we have said that to get a generic type of superclass, you need to use a function:

Design public type Getgenericsuperclass () for the generic parent class;
Let's take a look at how this function is used, and we still have the point class in the above and its derived class Pointimpl as an example:
The implementation of the point generic class is public class Point<t> {    private T x, y;    Public T GetX () {        return x;    }    public void SetX (T x) {        this.x = x;    }    Public T GetY () {        return y;    }    public void Sety (T y) {        this.y = y;    }} Implementation of the Pointimpl class public class Pointimpl extends point<integer> {}
From the code above, we can see that the point class is a generic class with a generic variable t, whereas Pointimpl derives from point and, when derived, fills point as a point, and the generic variable in point is populated with an integer type.
Below, we will get the type of Pointimpl's parent class through reflection, and the fill type of Pointimpl
Before we look at the code, we look at the results, we know that Pointimpl's parent class type is point, and Pointimpl's fill type should be integer.
And then we'll look at the code:
class<?> clazz = Pointimpl.class; Type type = Clazz.getgenericsuperclass (); if (type instanceof parameterizedtype) {    Parameterizedtype Parameterizedtype = (parameterizedtype) type;    Returns an array of type objects representing the actual type parameters of this type    type[] actualtypearguments = parameterizedtype.getactualtypearguments ();    for (Type parameterargtype:actualtypearguments) {        Class Parameterargclass = (Class) Parameterargtype;        LOG.D (TAG, "Fill type:" + parameterargclass.getname ());    }    Returns a Type object that represents a class or interface that declares this type.    Type type1 = Parameterizedtype.getrawtype ();    Class Class22 = (Class) type1;    LOG.D (TAG, "Pointimpl's parent class type is:" +class22.getname ());}
I believe the above code, we certainly do not understand .... Because it's really tough, no matter what, let's look at the results:

tamping Java Basic Two--reflection (2): Generic related peripheral information acquisition

Related Article

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.