【Java面向对象】07 反射,通过常用反射api打印一个类的信息(1)

反射

能够分析类能力的程序称为反射

1.Class类

获取Class的三种方式:

  • {@link Object#getClass()}

  • {@link Class#forName(String)}

  • {@code Employee.class }

public void testClass() throws ClassNotFoundException {
    Employee employee = new Employee();
    Class<? extends Employee> c1 = employee.getClass();
    Class<Employee> c2 = Employee.class;
    Class<?> c3 = Class.forName("com.java.practice.oop.oop05.Employee");
}
  • Class代表一种类型,这种类型不一定是类
public void testType(){
    Class<Integer> intClass = int.class;
    System.out.println(intClass); // int
}
  • newInstance 动态创建类的实例,必须提供无参构造
 public void testNewInstance() throws Exception {
     Class<?> c = Class.forName("com.java.practice.oop.oop05.Employee");
     // 必须提供无参构造
     Employee employee = (Employee) c.newInstance();
 }

2.反射常用api

  • Field
  • Method
  • Constructor
/**
     * 返回名称:
     * {@link Field#getName()}
     * {@link Method#getName()}
     * {@link Constructor#getName()}
     *
     * 返回Class
     * {@link Field#getType()}
     *
     * 修饰符描述
     * {@link Field#getModifiers()}
     * {@link Method#getModifiers()}
     * {@link Constructor#getModifiers()}
     *
     * {@link Modifier} 判断修饰符
     */
public void reflectApi() throws Exception {
    printClassInfo("java.lang.Double");
}

// 打印某个类的信息 fields constructors methods
public static void printClassInfo(String className) throws Exception {
    // print class name and super class name
    Class<?> cl = Class.forName(className);
    Class<?> superclass = cl.getSuperclass();
    String modifiers = Modifier.toString(cl.getModifiers());
    if (modifiers.length() > 0)
        System.out.print(modifiers + " ");
    System.out.print("class " + className);
    if (superclass != null && superclass != Object.class)
        System.out.print(" extends " + superclass.getName() + " {");
    else
        System.out.print(" {");

    System.out.println();

    printFields(cl);

    System.out.println();

    printConstructor(cl);

    System.out.println();

    printMethods(cl);

    System.out.println("}");
}

private static void printMethods(Class<?> cl) {
    Method[] declaredMethods = cl.getDeclaredMethods();
    System.out.println("\t// methods");
    for (Method c : declaredMethods) {
        int modifiers = c.getModifiers();
        Class[] parameterTypes = c.getParameterTypes();
        StringBuilder params = new StringBuilder();
        for (Class p : parameterTypes) {
            params.append(p.getName()).append(",");
        }
        if (params.length() > 0)
            params.deleteCharAt(params.length() - 1);
        System.out.println("\t" + Modifier.toString(modifiers) + " " + c.getReturnType().getSimpleName() + " " + c.getName() + "(" + params + ")" + ";");
    }
}

private static void printConstructor(Class<?> cl) {
    Constructor<?>[] constructors = cl.getDeclaredConstructors();
    System.out.println("\t// constructors");
    for (Constructor c : constructors) {
        int modifiers = c.getModifiers();
        Class[] parameterTypes = c.getParameterTypes();
        StringBuilder params = new StringBuilder();
        for (Class p : parameterTypes) {
            params.append(p.getName()).append(",");
        }
        if (params.length() > 0)
            params.deleteCharAt(params.length() - 1);
        System.out.println("\t" + Modifier.toString(modifiers) + " " + c.getName() + "(" + params + ")" + ";");
    }
}

/**
     * {@link Class#getDeclaredFields()} 返回的是Field对象数组,记录这个类的全部域
     * {@link Class#getFields()} 返回这个类或父类的public
     * @param cl
     */
private static void printFields(Class<?> cl) {
    Field[] fields = cl.getDeclaredFields();
    System.out.println("\t// fields");
    for (Field f : fields) {
        int modifiers = f.getModifiers();
        System.out.println("\t" + Modifier.toString(modifiers) + " " + f.getType().getSimpleName() + " " + f.getName() + ";");
    }
}
  • 反射分析对象
public void getFieldsValue() throws Exception {
    Employee employee = new Employee();
    employee.setName("abc");
    Class<? extends Employee> c = employee.getClass();
    Field name = c.getDeclaredField("name");
    name.setAccessible(true); // 私有属性需要设置此属性才能访问
    Object res = name.get(employee);
    System.out.println(res); // abc
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值