码迷,mamicode.com
首页 > 其他好文 > 详细

反射泛型

时间:2017-10-16 19:44:25      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:ati   end   string   stty   ima   reflect   div   lang   ceo   

对泛型进行反射:

技术分享

package com.zby;

import java.io.Closeable;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.List;

/*K,V,N是TypeVariable;字段调用getGenericType()方法,list1返回Class,list2、list3、list4、list5返回ParameterizedType,list6返回GenericArrayType;
*list2的ParameterizedType调用getActualTypeArguments返回的是TypeVariable,list3,list4,list5调用getActualTypeArguments返回的是WildcardType,
*list6调用getGenericComponentType()返回的是ParameterizedType.
*
*/

public class TestType<K extends InputStream & Closeable, V extends K> {
    public K k;
    @SuppressWarnings("rawtypes")
    public List list1;
    public List<K> list2;
    public List<?> list3;
    public List<? extends K> list4;
    public List<? super V> list5;
    public List<?>[] list6;

    public <N extends Number> N testV(List<? extends Number> list5, List<? super Number> list6) {
        return null;
    }


    @SuppressWarnings("rawtypes")
    public static void main(String[] args) {
        TypeVariable<Class<TestType>>[] typeParameters = TestType.class.getTypeParameters();
        System.out.println("类的泛型:" + Arrays.toString(typeParameters));
        for (TypeVariable<Class<TestType>> typeVariable : typeParameters) {
            System.out.print("泛型【" + typeVariable + "】:");
            String name = typeVariable.getName();
            System.out.print("泛型名称=" + name);
            Class<TestType> genericDeclaration = typeVariable.getGenericDeclaration();
            System.out.print(",泛型定义的类=" + genericDeclaration + ",");
            Type[] bounds = typeVariable.getBounds();
            for (Type type : bounds) {
                // 边界如果是确定的类型,type就会封装成Class类型,如果还是一个泛型,type就会封装成TypeVariable对象
                System.out.print("上边界Type类型=" + type.getClass().getName() + ",实际类型=");
                if (type instanceof Class) {
                    Class<?> clazz = (Class) type;
                    System.out.print(clazz);
                }
                if (type instanceof TypeVariable<?>) {
                    TypeVariable<?> typeVariable2 = (TypeVariable<?>) type;
                    System.out.print(typeVariable2);
                }
            }
            System.out.println();
        }
        System.out.println();

        Field[] fields = TestType.class.getDeclaredFields();
        for (Field field : fields) {
            System.out.print("字段【" + field.getName() + "】:");
            Type type = field.getGenericType();
            System.out.print("字段Type类型为=" + type.getClass().getName() + ",字段实际类型=");
            if (type instanceof Class) {
                Class<?> clazz = (Class) type;
                System.out.print(clazz);
            }
            if (type instanceof TypeVariable<?>) {
                TypeVariable<?> typeVariable2 = (TypeVariable<?>) type;
                System.out.print(typeVariable2);
            }
            if (type instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) type;
                System.out.print(parameterizedType);
                Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
                for (Type type2 : actualTypeArguments) {
                    if (type2 instanceof WildcardType) {
                        WildcardType wildcardType = (WildcardType) type2;
                        System.out.print(",参数化类型的Type类型为=" + type2.getClass().getName() + ",上边界为="
                                + wildcardType.getUpperBounds()[0] + ",下边界为=" + wildcardType.getLowerBounds().length
                                + ",实际类型=" + type2);
                    }
                    System.out.print(",参数化类型的Type类型为=" + type2.getClass().getName() + ",实际类型=" + type2);
                }
            }
            if (type instanceof GenericArrayType) {
                GenericArrayType genericArrayType = (GenericArrayType) type;
                System.out.println(genericArrayType.getGenericComponentType().getClass().getName());
            }
            System.out.println();
        }
        System.out.println();

        Method[] methods = TestType.class.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("-" + method.getGenericReturnType().getClass().getName());
        }
    }
}
类的泛型:[K, V]
泛型【K】:泛型名称=K,泛型定义的类=class com.zby.TestType,上边界Type类型=java.lang.Class,实际类型=class java.io.InputStream上边界Type类型=java.lang.Class,实际类型=interface java.io.Closeable
泛型【V】:泛型名称=V,泛型定义的类=class com.zby.TestType,上边界Type类型=sun.reflect.generics.reflectiveObjects.TypeVariableImpl,实际类型=K

字段【k】:字段Type类型为=sun.reflect.generics.reflectiveObjects.TypeVariableImpl,字段实际类型=K
字段【list1】:字段Type类型为=java.lang.Class,字段实际类型=interface java.util.List
字段【list2】:字段Type类型为=sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl,字段实际类型=java.util.List<K>,参数化类型的Type类型为=sun.reflect.generics.reflectiveObjects.TypeVariableImpl,实际类型=K
字段【list3】:字段Type类型为=sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl,字段实际类型=java.util.List<?>,参数化类型的Type类型为=sun.reflect.generics.reflectiveObjects.WildcardTypeImpl,上边界为=class java.lang.Object,下边界为=0,实际类型=?,参数化类型的Type类型为=sun.reflect.generics.reflectiveObjects.WildcardTypeImpl,实际类型=?
字段【list4】:字段Type类型为=sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl,字段实际类型=java.util.List<? extends K>,参数化类型的Type类型为=sun.reflect.generics.reflectiveObjects.WildcardTypeImpl,上边界为=K,下边界为=0,实际类型=? extends K,参数化类型的Type类型为=sun.reflect.generics.reflectiveObjects.WildcardTypeImpl,实际类型=? extends K
字段【list5】:字段Type类型为=sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl,字段实际类型=java.util.List<? super V>,参数化类型的Type类型为=sun.reflect.generics.reflectiveObjects.WildcardTypeImpl,上边界为=class java.lang.Object,下边界为=1,实际类型=? super V,参数化类型的Type类型为=sun.reflect.generics.reflectiveObjects.WildcardTypeImpl,实际类型=? super V
字段【list6】:字段Type类型为=sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl,字段实际类型=sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl


-java.lang.Class
-sun.reflect.generics.reflectiveObjects.TypeVariableImpl

 泛型相关的反射开始理解有点绕,但是结合源码,配合手动尝试,半天就能理解!

反射泛型

标签:ati   end   string   stty   ima   reflect   div   lang   ceo   

原文地址:http://www.cnblogs.com/zby9527/p/7678007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!