• 反射与泛型--使用泛型反射API打印出给定类的所有内容


    package chapter8Demos;
    
    import java.lang.reflect.*;
    import java.util.Arrays;
    import java.util.Scanner;
    import source.*;
    public class GenericReflectionTest {
    
        public static void main(String[] args) {
            String name;
            if (args.length > 0) {
                name = args[0];
            }
            else {
                try (Scanner in = new Scanner(System.in)){
                    System.out.println("Enter class name (e.g. java.util.Collections): ");
                    name = in.next();
                }
            }
            //反射手动输入的这个类
            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();
            //Modifier 修饰语
            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(")");
        }
        private 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) {
                    //getBounds获取类型变量的上边界, 若未明确声明上边界则默认为Object
                    printTypes(t.getBounds(), " extends ", " & ", "", false);
                }
            }
            //wildcard 通配符
            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("[]");
            }
        }
        //通用打印,可以处理继承、接口、参数的打印 
        //isDefinition输入true则表明该参数变量已经打印完全了,输入false则表明该参数变量还可以被打印的更准确
        private 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);
            }
        }
    }

    JAVA核心技术8-4

  • 相关阅读:
    C#使用Json.Net遍历Json
    晓晨高效IP提取工具 附源码
    JavaScript原生秒表、计时器
    网盘直链工具 winform版 V1.0
    【架构】RPC 使用 Haproxy、keepalive作为负载均衡
    【架构】Google的大规模集群管理工具Borg
    【Networking】gRPC golang 相关资料
    【Linux】find grep 联合使用 过滤所有子目录、文件
    【Storage】Ubuntu LVM 安装配置
    【Ansible】SSH Error: ssh_exchange_identification: Connection closed by remote host
  • 原文地址:https://www.cnblogs.com/Joey777210/p/11851589.html
Copyright © 2020-2023  润新知