Java反射二 获取类的基本信息

来源:互联网 发布:centos 7 编译安装git 编辑:程序博客网 时间:2024/06/15 12:19

有如下类

package com.nextyu.study.reflection;/** * 2016-12-25 下午2:00 * * @author nextyu */@MyAnnotation(name = "小灰灰", value = "喜洋洋")public class MyObject implements MyInterface {    private String name;    private Integer age;    public String address;    public MyObject() {    }    public MyObject(String name) {        this.name = name;    }    public MyObject(String name, Integer age) {        this.name = name;        this.age = age;    }    @Override    public String toString() {        return "MyObject{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }    private String privateMethod1() {        return "privateMethod1";    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}

使用java反射机制,我们可以在运行期获取Java类的信息。

在获取Java类的信息之前,得首先拿到这个类的Class对象。Java中的所有类型包括基本类型(int, long, float等等),即使是数组都有与之关联的Class类的对象。

获取Class对象

// 获取Class对象Class<MyObject> aClass = MyObject.class;// 或者通过全限定类名获取Class对象Class aClass1 = Class.forName("com.nextyu.study.reflection.MyObject");

获取其他信息

// 获取全限定类名(包含包名)String name = aClass.getName();logger.debug("全限定类名 {}", name);// 获取类的名字(不包含包名)String simpleName = aClass.getSimpleName();logger.debug("类的名字(不包含包名) {}", simpleName);// 获取当前类或者接口的全部修饰符比如public, protected, private, final, static, abstract and interface等等// ,但是返回的是一个编码之后的int整数int modifiers = aClass.getModifiers();logger.debug("修饰符 {}", modifiers);// 可以使用Modifier类,来判断当前修饰符是否包含指定的修饰符logger.debug("是否包含抽象修饰符 {}", Modifier.isAbstract(modifiers));logger.debug("是否包含私有修饰符 {}", Modifier.isPrivate(modifiers));// 获取包名Package aPackage = aClass.getPackage();logger.debug("包名 {}", aPackage.getName());// 获取父类Class<? super MyObject> superclass = aClass.getSuperclass();logger.debug("父类 {}", superclass);// 获取实现的接口// 注意:getInterfaces()方法仅仅只返回当前类所实现的接口。当前类的父类如果实现了接口,这些接口是不会在返回的Class集合中的,尽管实际上当前类其实已经实现了父类接口。Class<?>[] interfaces = aClass.getInterfaces();for (Class<?> anInterface : interfaces) {    logger.debug("接口 {}", anInterface);}// 获取构造方法Constructor<?>[] constructors = aClass.getConstructors();for (Constructor<?> constructor : constructors) {    logger.debug("构造方法 {}", constructor);}// 获取方法,包括父类的(只能获取public的)Method[] methods = aClass.getMethods();for (Method method : methods) {    logger.debug("public方法 {}", method);}// 获取成员变量(只能获取public的)Field[] fields = aClass.getFields();for (Field field : fields) {    logger.debug("public成员变量 {}", field);}// 获取注解Annotation[] annotations = aClass.getAnnotations();for (Annotation annotation : annotations) {    logger.debug("注解 {}", annotation);}    

结果如下

全限定类名 com.nextyu.study.reflection.MyObject类的名字(不包含包名) MyObject修饰符 1是否包含抽象修饰符 false是否包含私有修饰符 false包名 com.nextyu.study.reflection父类 class java.lang.Object接口 interface com.nextyu.study.reflection.MyInterface构造方法 public com.nextyu.study.reflection.MyObject(java.lang.String,java.lang.Integer)构造方法 public com.nextyu.study.reflection.MyObject(java.lang.String)构造方法 public com.nextyu.study.reflection.MyObject()public方法 public java.lang.String com.nextyu.study.reflection.MyObject.toString()public方法 public java.lang.String com.nextyu.study.reflection.MyObject.getName()public方法 public void com.nextyu.study.reflection.MyObject.setName(java.lang.String)public方法 public void com.nextyu.study.reflection.MyObject.setAge(java.lang.Integer)public方法 public java.lang.Integer com.nextyu.study.reflection.MyObject.getAge()public方法 public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedExceptionpublic方法 public final native void java.lang.Object.wait(long) throws java.lang.InterruptedExceptionpublic方法 public final void java.lang.Object.wait() throws java.lang.InterruptedExceptionpublic方法 public boolean java.lang.Object.equals(java.lang.Object)public方法 public native int java.lang.Object.hashCode()public方法 public final native java.lang.Class java.lang.Object.getClass()public方法 public final native void java.lang.Object.notify()public方法 public final native void java.lang.Object.notifyAll()public成员变量 public java.lang.String com.nextyu.study.reflection.MyObject.address注解 @com.nextyu.study.reflection.MyAnnotation(name=小灰灰, value=喜洋洋)

源码地址

https://github.com/nextyu/study-source/tree/master/java-reflection

0 0
原创粉丝点击