Java泛型type体系详解编程语言

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

Type接口

这些都在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 class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">testParameterizedType() throws NoSuchFieldException, SecurityException{ 
 
        Type mapGenericType = GenericTest.class.getDeclaredField(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"map").getGenericType();  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//ParameterizedType 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if(mapGenericType instanceof ParameterizedType){ 
            Type basicType = ((ParameterizedType) mapGenericType).getRawType(); 
 
 
            System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"basicType equals Map.class is " + (basicType ==Map.class)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回True 
 
            System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"基本类型为:"+basicType);  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//Map 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取泛型类型的泛型参数, 分别为  
            Type[] types = ((ParameterizedType) mapGenericType).getActualTypeArguments(); 
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0; i < types.length; i++) { 
                System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"第"+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"个泛型类型是:"+types[i]); 
            } 
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回为 T1, class java.lang.Integer 
 
            System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out.println(((ParameterizedType) mapGenericType).getOwnerType());  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//null 
        } 
    }</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 class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">testTypeVariable() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws NoSuchFieldException, SecurityException{ 
Type mapGenericType = GenericTest.class.getDeclaredField(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"map").getGenericType();  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//ParameterizedType 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if(mapGenericType <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">instanceof ParameterizedType){ 
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取泛型类型的泛型参数, 分别为 T1, class java.lang.Integer 
Type[] types = ((ParameterizedType) mapGenericType).getActualTypeArguments(); 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0; i < types.length; i++) { 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if( types[i] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">instanceof TypeVariable){ 
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// T1 is TypeVariable, and Integer is not. 
System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the "+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"th is TypeVariable"); 
}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else{ 
System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the "+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"th is not TypeVariable"); 
} 
} 
} 
System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"GenericTest TypeVariable"); 
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 class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for(TypeVariable tmp : typeVariables){ 
System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""+tmp); 
Type[] bounds = tmp.getBounds(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//return upper bounds 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if(bounds.length > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0){  
<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 class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//T1's upper bounds is Object which is default. 
System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"bounds[0] is: "+bounds[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0]); 
} 
System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name is: "+tmp.getName());  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  T1, T2 
System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"GenericDeclaration is equals GenericTest.class: "+ (tmp.getGenericDeclaration()==GenericTest.class)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//GenericTest 
} 
} 
</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;">; 
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 class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getDeclaredField(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"map1")<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getGenericType()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
TypeVariable<Class<GenericTest>>[] typeVariables = GenericTest<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getTypeParameters()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
Type[] types = ((ParameterizedType) mapGenericType)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getActualTypeArguments()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
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 class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wildcardType")<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; 
if( ((WildcardType) t)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getLowerBounds()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.length > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0 ) 
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println((((WildcardType) t)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getLowerBounds())[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0])<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //print java.lang.Integer 
if( ((WildcardType) t)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getUpperBounds()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.length > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0 ) 
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println((((WildcardType) t)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getUpperBounds())[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0])<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //print java.lang.Number, Object 
} 
} 
}</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 T1[] tArray = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null; 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void <span class="hljs-title" style="box-sizing: border-box;">testGenericArrayType() throws NoSuchFieldException, SecurityException{ 
Type tArrayGenericType = GenericTest.class.getDeclaredField(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tArray").getGenericType(); 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if(tArrayGenericType instanceof GenericArrayType){ 
System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"is GenericArrayType");  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 
Type t1 = ((GenericArrayType) tArrayGenericType).getGenericComponentType(); 
System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out.println(t1);   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// print T1 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if( t1 instanceof TypeVariable){ 
System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"t1 is TypeVariable"); 
System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out.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本身又有很多子类型,标明具体的泛型类型。这个是我测试的代码源码

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13075.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论