java中反射的泛型简介说明

java-教程王 Java经验 发布时间:2022-04-16 10:04:11 阅读数:3472 1
下文笔者讲述反射中泛型的获取方法分享,如下所示:
java反射之泛型
   GenericArrayType:描述数组 参数/变量 类型,例如:T[]
   ParameterizedType:描述泛型参数类型,例如:Collection<String>
   TypeVariable:描述泛型 的完整类型,例如 T
   WildcardType:描述统配符的,例:? extends A 
例:

public class GenericReflectionTest {
    public static void main(String[] args) {
        String name;
        if(args.length > 0) name = args[0];
        else{
            Scanner in = new Scanner(System.in);
            System.out.println("Enter class name(e.g. java.util.Collections)");
            name = in.nextLine();
        }
 
        try{
            Class cl = Class.forName(name);
            printClass(cl);
            for(Method m:cl.getDeclaredMethods()){
                printMethod(m);
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
 
    public static void printClass(Class cl){
        System.out.print(cl);
        printTypes(cl.getTypeParameters(),"<",",",">",true);
        Type sc = cl.getGenericSuperclass();
        if(sc!=null){
            System.out.print(" extends ");
            printType(sc, false);
        }
        printTypes(cl.getGenericInterfaces(), " implements ",",", "",false);
        System.out.println();
    }
 
    public static void printMethod(Method m){
        String name = m.getName();
        System.out.print(Modifier.toString(m.getModifiers()));
        System.out.print(" ");
        printTypes(m.getTypeParameters(),"<",",","> ",true);
 
        printType(m.getGenericReturnType(),false);
        System.out.print(" ");
        System.out.print(name);
        System.out.print("(");
        printTypes(m.getGenericParameterTypes(),"",",","",false);
        System.out.println(");");
    }
 
    public static void printTypes(Type[] types, String pre, String sep, String suf, boolean isDefinition){
        if(pre.equals(" extends ") && Arrays.equals(types, new Type[]{Object.class})) return;
        if(types.length > 0) System.out.print(pre);
        for(int i = 0; i < types.length; i++){
            if(i > 0) System.out.print(sep);
            printType(types[i], isDefinition);
        }
        if(types.length>0) System.out.print(suf);
    }
 
    public static void printType(Type type, boolean isDefinition){
        if(type instanceof Class){
            Class t = (Class) type;
            System.out.print(t.getName());
        }else if(type instanceof TypeVariable){
            TypeVariable t = (TypeVariable) type;
            System.out.print(t.getName());
            if(isDefinition)
                printTypes(t.getBounds(), " extends ", " & ","", false);
        }else if(type instanceof WildcardType){
            WildcardType t = (WildcardType) type;
            System.out.print("?");
            printTypes(t.getUpperBounds()," extends "," & ","",false);
            printTypes(t.getLowerBounds(), " super ", " & ","", false);
        }else if(type instanceof ParameterizedType){
            ParameterizedType t = (ParameterizedType) type;
            Type owner = t.getOwnerType();
            if(owner!=null){
                printType(owner, false);
                System.out.print(".");
            }
            printType(t.getRawType(), false);
            printTypes(t.getActualTypeArguments(), "<",", ",">",false);
        }else if(type instanceof  GenericArrayType){
            GenericArrayType t = (GenericArrayType) type;
            System.out.print("");
            printType(t.getGenericComponentType(),isDefinition);
            System.out.print("[]");
        }
    }
 
}

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202204/16500750582894.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者