Java泛型type体系

最近看开源代码,看到里面很多Java泛型,并且通过反射去获取泛型信息。如果说要看懂泛型代码,那还是比较容易,但是如果要自己利用泛型写成漂亮巧妙的框架,那必须对泛型有足够的了解。所以这两三天就不在不断地看Java泛型相关的东西。如果说想要明白通过反射去获取泛型信息,那么Type体系是必须要了解的。Java从1.5开始引入泛型,并且也引入了Type体系。首先给出Type的类型树UML图,以便有个整体的认识。

这些都在java的reflect包下面,图中带有s的接口返回的是数组,由于画图工具的原因,标记的只有一个类型,没有带[]。可以说是Java泛型反射的基础。这个UML图我只选取了这些类与接口与Type相关的部分。

Method,Field,Constructor,Class与Type

Method,Field,Constructor都是与Type关联的,而Class是Type的子类。从实际泛型中看也是这样的,因为方法,域,构造方法都可以是泛型。

如果从来没有接触过这些,那么一开始接触这些泛型的反射会非常突兀。如果从一些疑问,或者说需求开始思考可能会能够更好地了解这些东西的作用,比如说:我们已经知道能够通过反射(从1.1就开始支持了)获得一个Class里面的方法以及域,但是我们怎么获得一个带有模版参数的Class里面的模版参数呢?比如说下面这个类

class Test<T> {
public <R> void testMethod(R params){
}
}

我们怎么根据Test.class获得T呢?获得T的具体类型?Class实现了接口GenericDeclaration。GenericDeclaration的getTypeParameters()方法就可以获得模版参数。

同样Method能够包含模版参数的地方有四个,模版参数,返回值,函数参数,异常。Method的四个方法分别对应这四个地方(有一个是继承自GenericDeclaration)。Field,Constructor也都是类似。

Method,Field,Constructor,Class都有获得声明的模版参数的方法,这样我们就可以通过反射来获取模版参数类型。

Type的子接口

Type有四个子接口:ParameterizedType,TypeVariable,WildcardType,GenericArrayType。下面分别介绍这四个类型的具体表示的意思。假设我们申明有下面这个类

public class GenericTest<T1,T2 extends Number> {
    private Map<T1 , Integer> map = null;
}

下面我们开始举例说明。

ParameterizedType

ParameterizedType是表示泛型类型,比如说Map

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">testParameterizedType</span>() throws NoSuchFieldException, SecurityException{

        Type mapGenericType = GenericTest.class.getDeclaredField(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"map"</span>).getGenericType();  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//ParameterizedType</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(mapGenericType instanceof ParameterizedType){
            Type basicType = ((ParameterizedType) mapGenericType).getRawType();


            System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"basicType equals Map.class is "</span> + (basicType ==Map.class)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回True</span>

            System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"基本类型为:"</span>+basicType);  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//Map</span>
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取泛型类型的泛型参数, 分别为 </span>
            Type[] types = ((ParameterizedType) mapGenericType).getActualTypeArguments();
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < types.length; i++) {
                System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"第"</span>+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"个泛型类型是:"</span>+types[i]);
            }
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回为 T1, class java.lang.Integer</span>

            System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(((ParameterizedType) mapGenericType).getOwnerType());  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//null</span>
        }
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

TypeVariable

这个是泛型参数类型,比如Map

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">testTypeVariable</span>() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws</span> NoSuchFieldException, SecurityException{

        Type mapGenericType = GenericTest.class.getDeclaredField(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"map"</span>).getGenericType();  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//ParameterizedType</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(mapGenericType <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">instanceof</span> ParameterizedType){
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取泛型类型的泛型参数, 分别为 T1, class java.lang.Integer</span>
            Type[] types = ((ParameterizedType) mapGenericType).getActualTypeArguments();
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < types.length; i++) {
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>( types[i] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">instanceof</span> TypeVariable){
                    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// T1 is TypeVariable, and Integer is not.</span>
                    System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the "</span>+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"th is TypeVariable"</span>);
                }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{
                    System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the "</span>+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"th is not TypeVariable"</span>);
                }
            }

        }

        System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"GenericTest TypeVariable"</span>);

        TypeVariable<Class<GenericTest>>[] typeVariables = GenericTest.class.getTypeParameters();

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// console print  T1, T2</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(TypeVariable tmp : typeVariables){
            System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>+tmp);
            Type[] bounds = tmp.getBounds(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//return upper bounds</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(bounds.length > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>){ 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//T2 has upper bounds which is class java.lang.Number, </span>
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//T1's upper bounds is Object which is default.</span>
                System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"bounds[0] is: "</span>+bounds[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]);
            }

            System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name is: "</span>+tmp.getName());  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  T1, T2</span>
            System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"GenericDeclaration is equals GenericTest.class: "</span>+ (tmp.getGenericDeclaration()==GenericTest.class)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//GenericTest</span>
        }
    }
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li></ul>

WildcardType

WildcardType是通配符类型,也就是? extends Number这种。可以获得上下界。具体使用如下

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    private Map<? extends Number, ? super Integer> map1 = new HashMap<Integer,Integer>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    public static void testWildcardType() throws NoSuchFieldException, SecurityException{
        Type mapGenericType = GenericTest<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getDeclaredField</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"map1"</span>)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getGenericType</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        TypeVariable<Class<GenericTest>>[] typeVariables = GenericTest<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getTypeParameters</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        Type[] types = ((ParameterizedType) mapGenericType)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getActualTypeArguments</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        for(Type t : types){
            if(t instanceof WildcardType){
                System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wildcardType"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

                if( ((WildcardType) t)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getLowerBounds</span>()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.length</span> > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> )
                System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>((((WildcardType) t)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getLowerBounds</span>())[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>])<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //print java.lang.Integer</span>

                if( ((WildcardType) t)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getUpperBounds</span>()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.length</span> > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> )
                    System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>((((WildcardType) t)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getUpperBounds</span>())[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>])<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //print java.lang.Number, Object</span>

            }
        }
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li></ul>

GenericArrayType

这个是数组泛型。比如T1[] tArray。这是变量的声明。比如下面这段代码

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> T1[] tArray = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">testGenericArrayType</span>() throws NoSuchFieldException, SecurityException{
        Type tArrayGenericType = GenericTest.class.getDeclaredField(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tArray"</span>).getGenericType();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(tArrayGenericType instanceof GenericArrayType){
            System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"is GenericArrayType"</span>);  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span>
            Type t1 = ((GenericArrayType) tArrayGenericType).getGenericComponentType();
            System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(t1);   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// print T1</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>( t1 instanceof TypeVariable){
                System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"t1 is TypeVariable"</span>);
                System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(((TypeVariable) t1).getGenericDeclaration());
            }
        }
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

Class

Class也是实现了Type接口。像前面的代码中得到了java.lang.Integer等,就是返回Class。Class也可以通过getGenericInterfaces()获取实现的接口,通过getGenericSuperclass()获取父类的类型。

总结

整个Type体系主要就是前面介绍的内容了,从Method,Field,Class反射获取Type,然后Type本身又有很多子类型,标明具体的泛型类型。这个是我测试的代码源码

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏desperate633

LintCode 有效回文串题目分析代码

给定一个字符串,判断其是否为一个回文串。只包含字母和数字,忽略大小写。 ** 注意事项** 你是否考虑过,字符串有可能是空字符串?这是面试过程中,面试官常常...

9320
来自专栏一个会写诗的程序员的博客

第11章 运算符重载与约定第11章 运算符重载与约定

我们在《第2章 Kotlin 语法基础》中已经学习过关于运算符的相关内容,本章将继续深入探讨Kotlin中的运算符的重载与约定。

16540
来自专栏一个会写诗的程序员的博客

《Kotlin 程序设计》第四章 Kotlin 语法基础

Kotlin 可以省略变量定义的类型声明,但是在定义参数列表和定义返回值类型时则必须明确指定类型(这个类型推断Kotlin居然没做,这地方用起来比Scala,G...

11120
来自专栏blackheart的专栏

[C#3] 1-扩展方法

1.从DEMO开始 先看一个扩展方法的例子: 1 class Program 2 { 3 public static void Main(...

224100
来自专栏个人分享

JAVA源码走读(二)二分查找与Arrays类

对数组排序:通过sort方法,按升序。 比较数组:通过equals方法比较数组中元素值是否相等。 查找数组元素:通过binarySearch方法能对排序好的数组...

15040
来自专栏GIS讲堂

面向对象下计算器的编码实现

首先是新建一个运算类(Calcultor.cs),它包含两个属性strNumA和strNumB,用以储存计算时的两个数,此外,还有一个虚方法Getresult(...

33330
来自专栏Micro_awake web

JavaScript中创建对象的几种模式

11400
来自专栏Coding迪斯尼

java开发编译器:把C语言的循环指令编译成java字节码

20930
来自专栏三好码农的三亩自留地

5分钟彻底理解-Java自动装箱、拆箱

当表格中左边列出的基础类型与它们的包装类有如下几种情况时,编译器会自动帮我们进行装箱或拆箱.

44120
来自专栏偏前端工程师的驿站

JS魔法堂:再识Number type

Brief                                   本来只打算理解JS中0.1 + 0.2 == 0.300000000000000...

23950

扫码关注云+社区

领取腾讯云代金券