Java-反射获取完整的类信息

纯API记录

  • 反射获取类信息日志:
package com.stu.java.reflection;
public final class RelectionTest {
private java.lang.String name;
private int age;
private boolean gender;
private java.lang.String desc;
public com.stu.java.reflection.RelectionTest ( ) {}
public com.stu.java.reflection.RelectionTest ( java.lang.String p0,  int p1,  ) {}
public java.lang.String getName( ) {}
public void setName( java.lang.String mp0) {}
public int getAge( ) {}
public void setAge( int mp0) {}
public boolean isGender( ) {}
public void setGender( boolean mp0) {}
public java.lang.String getDesc( ) {}
public void setDesc( java.lang.String mp0) {}
}

  • 准备一个测试类 RelectionTest
package com.stu.java.reflection;

final public class RelectionTest {

    private String name;
    private int age;
    private boolean gender;
    private String desc;
    
    public RelectionTest(){}
    
    public RelectionTest(String name, int age) {
        this.name = name;
        this.age = age;
    }

···下面是 setter getter方法

  • 使用反射获取完整类信息
    private static void relectionClassInfo() throws Exception {

        Class c = (Class) Class.forName("com.stu.java.reflection.RelectionTest");
        //获取包信息
        Package pg = c.getPackage();
        LogUtils.sysPrintln("package %s;", pg.getName());

        //获取类修饰符 public/ final 
        int m = c.getModifiers();
        LogUtils.sysPrint(Modifier.toString(m));

        //判断类型
        if (Modifier.isInterface(m)) {
            LogUtils.sysPrint(" interface ");
        } else {
            LogUtils.sysPrint(" class ");
        }

        LogUtils.sysPrintln(c.getSimpleName() + " {");


        //获取类的属性
        Field[] fields = c.getDeclaredFields();
        for (Field f : fields) {
            //属性 修饰符
            LogUtils.sysPrint(Modifier.toString(f.getModifiers()));
            //属性类型
            LogUtils.sysPrint(" " + f.getType().getName());
            //属性名称
            LogUtils.sysPrintln(" " + f.getName() + ";");
        }
        //获取构造器
        Constructor[] constructors = c.getDeclaredConstructors();
        for (Constructor con : constructors) {
            LogUtils.sysPrint(Modifier.toString(con.getModifiers()));
            LogUtils.sysPrint(" " + con.getName() + " (");
            //获取构造器参数
            Class[] ps = con.getParameterTypes();
            for (int i = 0; i < ps.length; i++) {
                Class cl = ps[i];
                LogUtils.sysPrint(" " + cl.getName() + " p%s, ", i);
            }
            LogUtils.sysPrintln(" ) {}");
        }
        //获取成员方法
        Method[] methods = c.getDeclaredMethods();
        for (Method md : methods) {
            LogUtils.sysPrint(Modifier.toString(md.getModifiers()));
            //获取返回值类型名称
            LogUtils.sysPrint(" " + md.getReturnType().getName());
            LogUtils.sysPrint(" " + md.getName() + "( ");
            //获取方法参数
            Class[] mPs = md.getParameterTypes();
            for (int i = 0; i < mPs.length; i++) {
                Class mp = mPs[i];
                LogUtils.sysPrint(mp.getName() + " mp%s", +i);
            }
            LogUtils.sysPrintln(") {}");

        }
        LogUtils.sysPrintln("}");

    }


END

参考:https://github.com/HarkBen/JavaSE6Tutorial
补充了获取构造参数及方法参数

你可能感兴趣的:(Java-反射获取完整的类信息)