获取类的运行时结构

HeJin大约 2 分钟Java注解和反射

Class对象的常用方法

/**
 * @Description 获得类的信息
 * @Author Administrator
 * @Date 2020/12/2 10:55
 */
public class Test08 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {

        Class c1 = Class.forName("com.hejin.reflection.User");

        // 获得类的名字:包名 + 类名
        System.out.println(c1.getName());
        // 获得类的简单名字:类名
        System.out.println(c1.getSimpleName());

        System.out.println("==============");
        /**
         * 获得类的属性
         * getFields()只能找到public属性
         * getDeclaredFields()能找到全部的属性
         */
        Field[] fields = c1.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        // 获取指定属性的值
        System.out.println("==============");
        Field field = c1.getDeclaredField("name");
        System.out.println(field);

        // 获得类的方法getMethods:获取本类以及父类的所有public方法
        System.out.println("==============");
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println("正常的:" + method);
        }
        System.out.println("==============");
        // getDeclaredMethods:获取本类的所有方法
        Method[] methods1 = c1.getDeclaredMethods();
        for (Method method : methods1) {
            System.out.println("getDeclaredMethods:" + method);
        }

        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println("==============");
        System.out.println("getName:" + getName);
        System.out.println("setName:" + setName);

        // 获取所有的构造器
        System.out.println("==============");
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("getConstructors:" + constructor);
        }
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println("getDeclaredConstructors:" + declaredConstructor);
        }

        // 获取指定的构造器
        System.out.println("==============");
        System.out.println(c1.getConstructor(null));
        System.out.println(c1.getConstructor(int.class,String.class,int.class));
    }
}

测试结果

com.hejin.reflection.User
User
==============
private int com.hejin.reflection.User.id
private java.lang.String com.hejin.reflection.User.name
private int com.hejin.reflection.User.age
==============
private java.lang.String com.hejin.reflection.User.name
==============
正常的:public java.lang.String com.hejin.reflection.User.toString()
正常的:public java.lang.String com.hejin.reflection.User.getName()
正常的:public int com.hejin.reflection.User.getId()
正常的:public void com.hejin.reflection.User.setName(java.lang.String)
正常的:public void com.hejin.reflection.User.setAge(int)
正常的:public void com.hejin.reflection.User.setId(int)
正常的:public int com.hejin.reflection.User.getAge()
正常的:public final void java.lang.Object.wait() throws java.lang.InterruptedException
正常的:public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
正常的:public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
正常的:public boolean java.lang.Object.equals(java.lang.Object)
正常的:public native int java.lang.Object.hashCode()
正常的:public final native java.lang.Class java.lang.Object.getClass()
正常的:public final native void java.lang.Object.notify()
正常的:public final native void java.lang.Object.notifyAll()
==============
getDeclaredMethods:public java.lang.String com.hejin.reflection.User.toString()
getDeclaredMethods:public java.lang.String com.hejin.reflection.User.getName()
getDeclaredMethods:public int com.hejin.reflection.User.getId()
getDeclaredMethods:public void com.hejin.reflection.User.setName(java.lang.String)
getDeclaredMethods:private void com.hejin.reflection.User.test()
getDeclaredMethods:public void com.hejin.reflection.User.setAge(int)
getDeclaredMethods:public void com.hejin.reflection.User.setId(int)
getDeclaredMethods:public int com.hejin.reflection.User.getAge()
==============
getName:public java.lang.String com.hejin.reflection.User.getName()
setName:public void com.hejin.reflection.User.setName(java.lang.String)
==============
getConstructors:public com.hejin.reflection.User()
getConstructors:public com.hejin.reflection.User(int,java.lang.String,int)
getDeclaredConstructors:public com.hejin.reflection.User()
getDeclaredConstructors:public com.hejin.reflection.User(int,java.lang.String,int)
==============
public com.hejin.reflection.User()
public com.hejin.reflection.User(int,java.lang.String,int)

Process finished with exit code 0