Spring中的Type學習

(1)、首先須要學習Type,第一步掌握Class的 getSuperclass與getGenericSuperclass

    getSuperclass:返回繼承的父類,因爲泛型擦除,因此不會獲取到泛型參數。java

                               返回表示此 Class 所表示的實體(類、接口、基本類型或 void)的超類的 Class;spring

                              若是此 Class 表示 Object 類、一個接口、一個基本類型或 void,則返回 null;數組

                              若是此對象表示一個數組類,則返回表示該 Object 類的 Class 對象。ide

   getGenericSuperclass:返回繼承的父類,包含泛型參數。學習

                             若是超類是參數化類型,則返回的 Type 對象必須準確反映源代碼中所使用的實際類型參數。若是之前不曾建立表示超類的參數化類型,則建立這個類型。有關參數化類型建立過程的語義,請參閱 ParameterizedType 聲明。測試

                             若是此 Class 表示 Object 類、接口、基本類型或 void,則返回 null。spa

                            若是此對象表示一個數組類,則返回表示 Object 類的 Class 對象。 code

       拋出:
      GenericSignatureFormatError - 若是常規類簽名不符合 Java Virtual Machine Specification, 3rd edition 規定的格式;
      TypeNotPresentException - 若是常規超類引用不存在的類型聲明;
      MalformedParameterizedTypeException - 若是常規超類引用的參數化類型因爲某種緣由沒法實例化。
使用例子進行理解:
public class Test {

    public static void main(String[] args) {
        System.out.println("Student.class.getSuperclass()\t"
                + Student.class.getSuperclass());
        System.out.println("Student.class.getGenericSuperclass()\t"
                + Student.class.getGenericSuperclass());

        System.out.println("Test.class.getSuperclass()\t"
                + Test.class.getSuperclass());
        System.out.println("Test.class.getGenericSuperclass()\t"
                + Test.class.getGenericSuperclass());

        System.out.println("Object.class.getGenericSuperclass()\t"
                + Object.class.getGenericSuperclass());
        System.out.println("Object.class.getSuperclass()\t"
                + Object.class.getSuperclass());

        System.out.println("void.class.getSuperclass()\t"
                + void.class.getSuperclass());
        System.out.println("void.class.getGenericSuperclass()\t"
                + void.class.getGenericSuperclass());

        System.out.println("int[].class.getSuperclass()\t"
                + int[].class.getSuperclass());
        System.out.println("int[].class.getGenericSuperclass()\t"
                + int[].class.getGenericSuperclass());
    }

}

class Person<T> {

}

class Student extends Person<Test> {

}

獲取打印結果:orm

Student.class.getSuperclass()    class com.spring.type.Person
Student.class.getGenericSuperclass()    com.spring.type.Person<com.spring.type.Test>
Test.class.getSuperclass()    class java.lang.Object
Test.class.getGenericSuperclass()    class java.lang.Object
Object.class.getGenericSuperclass()    null
Object.class.getSuperclass()    null
void.class.getSuperclass()    null
void.class.getGenericSuperclass()    null
int[].class.getSuperclass()    class java.lang.Object
int[].class.getGenericSuperclass()    class java.lang.Object

(2):java-Type

Type是對java數據類型的分類抽象,子接口有:ParameterizedType、TypeVariable、WildcardType、GenericArrayType;對象

ParameterizedType:java泛型參數類型,在(1)中講述方法getGenericSuperclass獲取的類型即爲參數化類型

 方法介紹:

       Type[] getActualTypeArguments():獲取<>中的實際類型;

      Type getRawType():獲取<>前面的實際類型;

      Type getOwnerType():獲取全部者類型,不然會返回null.

/*以上文中說起的Person類和Student類進行舉例*/ 
    public static void main(String[] args) {
        System.out.println("Student.class.getGenericSuperclass()\t"
                + Student.class.getGenericSuperclass());
        Type type =  Student.class.getGenericSuperclass();
        System.out.println("type.getRawType()\t"
                + ((ParameterizedType)type).getRawType());
        System.out.println("type.getActualTypeArguments()\t"
                + ((ParameterizedType)type).getActualTypeArguments());
        System.out.println("type.getOwnerType()\t"
                + ((ParameterizedType)type).getOwnerType());
    }

 如下爲測試結果:

Student.class.getGenericSuperclass()    com.spring.type.Person<com.spring.type.Test>
type.getRawType()    class com.spring.type.Person
type.getActualTypeArguments()    [Ljava.lang.reflect.Type;@1218025c
//雖然Person類寫在Test類中,可是不是子類,因此getOwnerType()方法返回null
type.getOwnerType() null

若是修改Person類爲Test子類:

//寫在Test類中
static class Person<T> {

    }

能夠看到:

//會返回Person的全部類
type.getOwnerType()    class com.spring.type.Test

TypeVariable:表明泛型中的變量,以下例中的「T」;

方法介紹:

   Type[] getBounds():返回泛型中變量類型的上限數組,若是沒有就直接打印Object;

   String getName():返回變量類型名;

   D getGenericDeclaration():獲取聲明該類型變量的實體.

   AnnotatedType[] getAnnotatedBounds():JDK1.8中新增的方法,AnnotatedType接口用來表明被註解修飾的類型,能夠獲取變量上線類型的AnnotatedType封裝類型數組,經過getType(),能夠獲取類型名稱

如下舉例說明:

public class TypeVariableTest<T extends Integer & Override> {
    T t;
}

測試例:

public  static void main(String[]args) throws NoSuchFieldException {
            Field fieldT=TypeVariableTest.class.getDeclaredField("t");
            TypeVariable typeVariable=(TypeVariable)fieldT.getGenericType();
            //返回類型的上限數組,若是沒有就直接打印Object
            Type[] types = typeVariable.getBounds();
            for (Type type:types) {
                System.out.println(type);
            }
            //打印類型的名稱
            System.out.println(typeVariable.getName());
            //獲取聲明該類型變量的實體()
            System.out.println(typeVariable.getGenericDeclaration());
            AnnotatedType[]  annotatedBounds = typeVariable.getAnnotatedBounds();
            for (AnnotatedType type:annotatedBounds) {
                System.out.println(type.getType());
            }
        }

 WildcardType:獲取<>中的類型;

直接使用https://www.jianshu.com/p/7649f86614d3幫助理解:

public class WildcardTypeTest {

    private List<? extends String> listStr;

    private List<? super String> b;

    public static void testGetBounds(String field) throws  NoSuchFieldException {

        System.out.println(field);
        Field fieldNum = WildcardTypeTest.class.getDeclaredField(field);
        Type typeNum = fieldNum.getGenericType();
        ParameterizedType parameterizedTypeTypeNum = (ParameterizedType) typeNum;
        Type[] typesNum = parameterizedTypeTypeNum.getActualTypeArguments();

        WildcardType wildcardType = (WildcardType) typesNum[0];
        {
            Type[] types = wildcardType.getUpperBounds();
            for (Type type : types) {
                System.out.println(type);
            }

            types = wildcardType.getLowerBounds();
            for (Type type : types) {
                System.out.println(type);
            }
        }

    }

    public static void testGetUpperBounds() throws  NoSuchFieldException
    {
        testGetBounds("listNum");
        testGetBounds("b");
    }

    public static void main(String[] args) throws NoSuchFieldException {
        testGetUpperBounds();
    }
}

GenericArrayType:表示泛型數組類型。泛型數組類型,例如List<String>[] 、T[]等;

方法:

   Type getGenericComponentType():僅僅脫去最右邊的[]以後剩下的內容就做爲這個方法的返回值。

https://www.iteye.com/blog/jinnianshilongnian-1993608  ResolvableType 的學習可參考該文章 

相關文章
相關標籤/搜索