通过反射获取类的完整结构

通过反射获取类的完整结构

1.获取属性的各个部分的内容:权限修饰符 变量类型 变量名等

2.获取运行时类的方法
注 解 ,权限修饰符, 返回值类型 ,方法名, 形参列表, 异常等

3.其他

TestConstructor

package com.aff.reflection;
import java.lang.reflect.Constructor;

import org.junit.Test;
public class TestConstructor {
    @Test
    public void test1() throws Exception {
        Class clazz = Person.class;
        /*创建对应的运行时类的对象,使用newInstance(),实际上就是调用了运行时类的空参的构造器
        想要能够创建成功:①要求对应的运行时类要有空参的构造器
                       ②构造器的权限修饰符的权限要足够
        */
        Object obj = clazz.newInstance();
        Person p = (Person) obj;
        System.out.println(p);//Person [name=null, age=0]
    }
    
    @Test
    public  void test2() throws ClassNotFoundException{
        
        String   className = "com.aff.reflection.Person";
        Class clazz = Class.forName(className);
        Constructor[] cons = clazz.getDeclaredConstructors();
        for(Constructor   c:cons){
            System.out.println(c);//public com.aff.reflection.Person(java.lang.String,int)
        }
    }
}

TestField

package com.aff.reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.junit.Test;

public class TestField {
    @Test
    public void test1() {
        Class clazz = Person.class;
        // getFields() :只能获取到运行时类中及其父类中声明为public的属性
        Field[] fields1 = clazz.getFields();
        System.out.println(fields1);
        System.out.println();

        // 2.getDeclaredFields():获取运行时类本身声明的所有的属性
        Field[] fields2 = clazz.getDeclaredFields();
        for (Field f : fields2) {
            System.out.println(fields2);
        }
    }

    // 权限修饰符 变量类型 变量名
    // 获取属性的各个部分的内容
    @Test
    public void test2() {
        Class clazz = Person.class;
        Field[] fields2 = clazz.getDeclaredFields();
        for (Field f : fields2) {
            // 1.获取每个属性的权限修饰符
            int i = f.getModifiers();
            String str = Modifier.toString(i);
            System.out.println(str);

            // 2.获取属性的变量类型
            Class type = f.getType();
            System.out.println(type.getName() + "");

            // 3.获取属性名
            System.out.println(f.getName());
            System.out.println();
        }
    }

    // 获取运行时类的方法
    // 注解,权限修饰符, 返回值类型 ,方法名, 形参列表, 异常
    @Test
    public void test3() {
        Class clazz = Person.class;
        // 1.getMethods(): 获取运行时类及其父类的所有的声明为public的方法
        Method[] m1 = clazz.getMethods();
        for (Method m : m1) {
            System.out.println(m);
        }
        System.out.println("----------------------");

        // getDeclaredMethods():获取运行时类本身声明的所有的方法
        Method[] m2 = clazz.getDeclaredMethods();
        for (Method m : m2) {
            // 1.注解
            Annotation[] ann = m.getAnnotations();
            for (Annotation a : ann) {
                System.out.println(a + "--");
            }

            // 2.权限修饰符
            String str = Modifier.toString(m.getModifiers());
            System.out.print(str + "--");

            // 3.返回值类型
            Class returnType = m.getReturnType();
            System.out.print(returnType.getName() + "--");

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

            // 5.形参列表
            // System.out.print("(");
            Class[] param = m.getParameterTypes();
            for (int i = 0; i < param.length; i++) {
                System.out.print("(" + param[i].getName() + " args-" + i + ")");
            }
            // 6.异常类型
            Class[] exps = m.getExceptionTypes();
            if (exps.length != 0) {
                System.out.println("throws");
            }
            for (int i = 0; i < exps.length; i++) {
                System.out.print(exps[i].getName() + "--");
            }
            System.out.println();
        }
    }
}

其他

TestOthers

package com.aff.reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.junit.Test;

public class TestOthers {
    // 1.获取运行时类的父类
    @Test
    public void test1() {
        Class clazz = Person.class;
        Class superclass = clazz.getSuperclass();
        System.out.println(superclass);// class java.lang.Object
    }

    // 2.获取带泛型的父类
    @Test
    public void test2() {
        Class clazz = Person.class;
        Type type1 = clazz.getGenericSuperclass();
        System.out.println(type1);// class java.lang.Object
    }

    // 3.获取父类的泛型
    @Test
    public void test3() {
        Class clazz = Person.class;
        Type type1 = clazz.getGenericSuperclass();
        ParameterizedType parm = (ParameterizedType) type1;
        Type[] ars = parm.getActualTypeArguments();
        System.out.println(((Class) ars[0]).getName());
    }

    // 4.获取实现的接口
    @Test
    public void test4() {
        Class clazz = Person.class;
        Class[] interfaces = clazz.getInterfaces();
        for (Class i : interfaces) {
            System.out.println(i);
        }
    }

    // 5获取所在的包
    @Test
    public void test5() {
        Class clazz = Person.class;
        Package pack = clazz.getPackage();
        System.out.println(pack);// package com.aff.reflection

    }

    // 6.获取注解
    @Test
    public void test6() {
        Class clazz = Person.class;
        Annotation[] anno = clazz.getAnnotations();
        for (Annotation a : anno) {
            System.out.println(a);
        }
    }
}
All that work will definitely pay off
原文地址:https://www.cnblogs.com/afangfang/p/12624188.html