编写案例,使用反射读取类的全部信息,并将其按照类的结构将其打印

一、需要打印的类 :

package com.test1;


import java.io.Serializable;

public final class A extends B implements Serializable,Cloneable {

    private static int i ;
    final int j = 10 ;
    volatile protected String m ;
    transient public String n ;

    public void t1(){}
    public final static  void t1(final int i)throws NullPointerException,ArrayIndexOutOfBoundsException{
    }
    public void t1(int i, int j){}

    private void t2(){}


    public A()throws NullPointerException{}

    public A(int i ,int j)throws Exception{}

    private A(int i)throws ArrayIndexOutOfBoundsException {}

}

随便继承一个父类 

package com.test1;

public class B {
}

二、反射读取类的全部信息

1.获得类的基本信息

c.getName();//获得类长名
c.getSimpleName();//获得类短名
c.getModifiers();//获得修饰符组合的数字和
				 //Modifier.toString( c.getModifiers() ) 获得修饰符关键字
c.getSuperClass();//获得父类
c.getInterfaces();//获得接口数组
c.getPackage();//获得包对象
			   //package.getName() 获得包名

2.获得属性信息

c.getFields();//获得所有属性对象(公共的,从父类继承的)
c.getField(name);//获得指定名字的属性对象
c.getDeclaredFields();//获得本类所有权限的属性对象
c.getDeclaredField(name);
field.getName();//获得属性名
field.getModifiers();//获得属性修饰符
field.getType();//获得属性类型

3.获得方法信息

c.getMethods(); //获得所有的public方法,可以继承的
c.getDeclaredMethods();//获得本类所有权限的方法
c.getMethod(name,Class...paramTypes) ;//根据指定的方法名和参数列表类型获得只当的方法
									  //c.getMethod("t1");   			t1()
									  //c.getMethod("t1",int.class);	t1(int)
									  //c.getMethod("t1",int.class,String.class) t1(i,s)
c.getDeclaredMethod(name , Class...pts) ;

method.getName();//获得方法名
method.getModifiers();
method.getReturnType();//获得返回类型
method.getParameterCount();//获得参数数量
method.getParameterTypes();//获得参数列表类型 返回Class[]
method.getParameters();//获得参数列表对象 返回Parameter[]
					   //parameter.getName();获得参数名
					   //parameter.getType();获得参数类型
					   //parameter.getModifiers();获得参数修饰符
method.getExceptionTypes();//获得异常声明列表

4.获得构造方法信息

 

c.getConstructors();//获得所有公共的构造方法(本类和继承的)
c.getConstructor(Class...pts);//根据参数列表类型获得指定的构造方法
c.getDeclaredConstructors();
c.getDeclaredConstructor(Class...pts);

constructor.getName();//获得方法名
constructor.getModifiers();
constructor.getParameterCount();//获得参数数量
constructor.getParameterTypes();//获得参数列表类型 返回Class[]
constructor.getParameters();//获得参数列表对象 返回Parameter[]
					   //parameter.getName();获得参数名
					   //parameter.getType();获得参数类型
					   //parameter.getModifiers();获得参数修饰符
constructor.getExceptionTypes();//获得异常声明列表

三、测试打印的逻辑代码

package com.test1;

import java.lang.reflect.*;

public class Test {
    private static final String BLANK = " ";
    private static final String LINE = "\r\n";
    private static final String DH = ",";
    private static final String SJ = "\t\n";//缩进

    public static void main(String[] args) throws ClassNotFoundException {
        String className = "com.test1.A";
        Class c = Class.forName(className);

        StringBuilder classInfo = new StringBuilder();

        classInfo.append(Modifier.toString(c.getModifiers()));
        classInfo.append(BLANK);
        classInfo.append("class");
        classInfo.append(BLANK);
        classInfo.append(c.getSimpleName());
        classInfo.append(BLANK);
        Class cs = c.getSuperclass();
        if (cs != null) {
            classInfo.append("extends");
            classInfo.append(BLANK);
            classInfo.append(cs.getSimpleName());
        }
        Class[] is = c.getInterfaces();
        if (is != null) {
            classInfo.append(BLANK);
            classInfo.append("implements");
            classInfo.append(BLANK);
            for (Class c1 : is) {
                classInfo.append(c1.getSimpleName());
                classInfo.append(DH);
            }
            classInfo.delete(classInfo.length() - 1, classInfo.length());
        }
        classInfo.append("{");

        Field[] fs = c.getDeclaredFields();
        for (Field f : fs) {
            if (fs != null) {
                classInfo.append(LINE);
                classInfo.append("\t");
                classInfo.append(Modifier.toString(f.getModifiers()));
                classInfo.append(BLANK);
                classInfo.append(f.getType().getSimpleName());
                classInfo.append(BLANK);
                classInfo.append(f.getName());
                classInfo.append(";");
            }
        }
        classInfo.append("\n");

        Method[] ms = c.getDeclaredMethods();
        if (ms != null) {
            for (Method m : ms) {
                classInfo.append(LINE);
                classInfo.append("\t");
                classInfo.append(Modifier.toString(m.getModifiers()));
                classInfo.append(BLANK);
                classInfo.append(m.getReturnType());
                classInfo.append(BLANK);
                classInfo.append(m.getName());
                classInfo.append(BLANK);
                classInfo.append("(");
                Parameter[] ps = m.getParameters();
                if (ps.length != 0) {
                    for (Parameter p : ps) {
                        if (p.getModifiers()!=0) {
                            classInfo.append(Modifier.toString(p.getModifiers()));
                            classInfo.append(BLANK);
                        }
                        classInfo.append(p.getType());
                        classInfo.append(BLANK);
                        classInfo.append(p.getName());
                        classInfo.append(",");
                    }
                    classInfo.delete(classInfo.length()-1,classInfo.length());
                }
                classInfo.append(")");
                Class[] es = m.getExceptionTypes();
                if (es.length != 0) {
                    for (Class e : es) {
                        classInfo.append("throws");
                        classInfo.append(BLANK);
                        classInfo.append(e.getSimpleName());
                        classInfo.append(",");
                    }
                    classInfo.delete(classInfo.length() - 1, classInfo.length());
                }
                classInfo.append("{}");
                classInfo.append(LINE);
            }
        }


        classInfo.append("\n");
        Constructor[] ccs = c.getDeclaredConstructors();
        for (Constructor cc : ccs) {
            if (ccs.length != 0) {
                classInfo.append(LINE);
                classInfo.append("\t");
                classInfo.append(Modifier.toString(cc.getModifiers()));
                classInfo.append(BLANK);
                classInfo.append(c.getSimpleName());
                classInfo.append("(");
                Parameter[] ps = cc.getParameters();
                if (ps.length != 0) {
                    for (Parameter p : ps) {
                        classInfo.append(p.getType());
                        classInfo.append(BLANK);
                        classInfo.append(p.getName());
                        classInfo.append(",");
                    }
                    classInfo.delete(classInfo.length() - 1, classInfo.length());
                }
                classInfo.append(")");
                classInfo.append("throws");
                classInfo.append(BLANK);
                Class[] es = cc.getExceptionTypes();
                if (es.length != 0) {
                    for (Class e : es) {
                        classInfo.append(e.getSimpleName());
                        classInfo.append(",");
                    }
                    classInfo.delete(classInfo.length() - 1, classInfo.length());
                    classInfo.append("{}");
                }
                classInfo.append(LINE);
            }
        }
        classInfo.append(SJ);
        classInfo.append("}");

        System.out.println(classInfo);
    }
}

四、结果截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值