Java通过反射获取属性、方法、构造器等结构

反射获取属性、方法、构造器

一、获取属性

/**
     * 获取当前运行时类的属性结构
     */
    @Test
    public void test01() {
        Class<Person> personClass = Person.class;

        //获取属性结构:
        //getFields():获取当前运行时类及其父类中声明为public访问权限的属性
        Field[] fields = personClass.getFields();
        for (Field f : fields) {
            System.out.println(f);
        }

        //getDeclaredFields():获取当前运行时类(不包含父类)中声明的所有属性
        Field[] declaredFields = personClass.getDeclaredFields();
        for (Field f : declaredFields) {
            System.out.println(f);
        }
    }

    /**
     * 拿到属性的 权限修饰符 数据类型 变量名
     */
    @Test
    public void test02() {
        Class<Person> personClass = Person.class;

        //获取运行时类中声明的所有属性
        Field[] declaredFields = personClass.getDeclaredFields();
        for (Field f : declaredFields) {
            //权限修饰符,用数字代替某个权限
            int modifier = f.getModifiers();
            System.out.print(Modifier.toString(modifier) + "\t");
            //数据类型
            Class<?> type = f.getType();
            System.out.print(type + "\t");
            //变量名
            String name = f.getName();
            System.out.print(name);
            System.out.println();
        }
    }

二、方法

/**
     * 获取运行时类的方法结构
     */
    @Test
    public void test03() {
        Class<Person> personClass = Person.class;

        //获取当前运行时类及其父类中声明为public访问权限的方法
        Method[] methods = personClass.getMethods();
        for (Method m : methods) {
            System.out.println(m);
        }

        System.out.println();

        //获取当前运行时类(不包含父类)中声明的所有方法
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method m : declaredMethods) {
            System.out.println(m);
        }
    }

    /**
     * 获取方法中的 注解 权限修饰符 返回值类型 方法名(参数类型1 形参1,...) throws XxxException
     */
    @Test
    public void test04() {
        Class<Person> personClass = Person.class;

        //获取运行时类中声明的所有方法
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method m : declaredMethods) {
            //获取注解
            Annotation[] annotations = m.getAnnotations();
            for (Annotation a : annotations) {
                System.out.println(a);
            }

            //权限修饰符
            System.out.print(Modifier.toString(m.getModifiers()) + " ");

            //返回值类型
            System.out.print(m.getReturnType().getName() + " ");

            //方法名
            System.out.print(m.getName() + "(");

            //形参列表
            Class<?>[] types = m.getParameterTypes();
            if (!(types == null || types.length == 0)) {
                for (int i = 0; i < types.length; i++) {
                    if (i == types.length - 1) {
                        System.out.print(types[i] + " args_" + i);
                    } else {
                        System.out.print(types[i] + " args_" + i + ",");
                    }
                }
            }
            System.out.print(")");

            //抛出的异常
            Class<?>[] exceptionTypes = m.getExceptionTypes();
            if (!(exceptionTypes == null || exceptionTypes.length == 0)) {
                System.out.print("throws ");
                for (int i = 0; i < exceptionTypes.length; i++) {
                    if (i != exceptionTypes.length - 1) {
                        System.out.print(exceptionTypes[i].getName() + ",");
                    } else {
                        System.out.print(exceptionTypes[i].getName());
                    }
                }
            }

            System.out.println();
        }
    }

三、构造器

@Test
    public void test05(){
        Class<Person> personClass = Person.class;
        //获取当前运行时类中声明为public的构造器
        Constructor<?>[] constructors = personClass.getConstructors();
        for(Constructor c : constructors){
            System.out.println(c);
        }
        //获取当前运行时类中所有的构造器
        Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
        for(Constructor c : declaredConstructors){
            System.out.println(c);
        }
    }

四、父类

@Test
    public void test06() {
        Class<Person> personClass = Person.class;
        //获取当前运行时类的父类
        Class<? super Person> superclass = personClass.getSuperclass();
        System.out.println(superclass);
    }

    @Test
    public void test07() {
        Class<Person> personClass = Person.class;
        //获取当前运行时类的带泛型的父类
        Type genericSuperclass = personClass.getGenericSuperclass();
        System.out.println(genericSuperclass);

        //获取当前运行时类的带泛型的父类的泛型
        //强转获取Type子类ParameterizedType
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        //获取泛型类型数组
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        for (int i = 0; i < actualTypeArguments.length; i++) {
            System.out.println(actualTypeArguments[i]);
        }
    }

五、接口

@Test
    public void test08() {
        Class<Person> personClass = Person.class;

        //获取运行时类的所有接口
        Class<?>[] interfaces = personClass.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
            System.out.println(interfaces[i]);
        }

        //获取运行时类的父类的所有接口
        Class<?>[] interfaces1 = personClass.getSuperclass().getInterfaces();
        for (int i = 0; i < interfaces1.length; i++) {
            System.out.println(interfaces1[i]);
        }
    }

六、包

@Test
    public void test09() {
        Class<Person> personClass = Person.class;
        Package aPackage = personClass.getPackage();
        System.out.println(aPackage);
    } 

七、类声明的注解

@Test
    public void test10() {
        Class<Person> personClass = Person.class;
        Annotation[] annotations = personClass.getAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            System.out.println(annotations[i]);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值