Spring ReflectionUtils 使用记录

System.out.println("---------- findMethod ----------");
// 获取方法
Method method1 = ReflectionUtils.findMethod(DemoObject.class, "method01");
Method method2 = ReflectionUtils.findMethod(DemoObject.class, "method02");
System.out.println(method1);
System.out.println("---------- findField ----------");
// 获取属性
Field field1 = ReflectionUtils.findField(DemoObject.class, "field01");
Field field2 = ReflectionUtils.findField(DemoObject.class, "field02");
System.out.println(field1);
System.out.println("---------- accessibleConstructor ----------");
// 获取构造方法
Constructor<DemoObject> constructor1 = ReflectionUtils.accessibleConstructor(DemoObject.class);
Constructor<DemoObject> constructor2 = ReflectionUtils.accessibleConstructor(DemoObject.class, String.class);
System.out.println(Arrays.toString(new String[] { constructor1.toString(), constructor2.toString() }));
System.out.println("---------- declaresException ----------");
// 方法是否存在指定的抛出异常
assert method2 != null;
boolean existIOException = ReflectionUtils.declaresException(method2, IOException.class);
boolean existException = ReflectionUtils.declaresException(method2, Exception.class);
System.out.println(existIOException + " - " + existException);
System.out.println("---------- doWithFields ----------");
// 返回所有字段,通过回调
ReflectionUtils.doWithFields(DemoObject.class, System.out::println);
System.out.println("---------- doWithLocalMethods ----------");
// 返回当前类所有方法,通过回调
ReflectionUtils.doWithLocalMethods(DemoObject.class, System.out::println);
System.out.println("---------- doWithMethods ----------");
// 返回所有方法包括继承类,通过回调
ReflectionUtils.doWithMethods(DemoObject.class, System.out::println);
System.out.println("---------- getAllDeclaredMethods ----------");
// 返回所有方法包括继承
Method[] methods = ReflectionUtils.getAllDeclaredMethods(DemoObject.class);
System.out.println(Arrays.toString(methods));
System.out.println("---------- getDeclaredMethods ----------");
// 返回所有当前类的方法
methods = ReflectionUtils.getDeclaredMethods(DemoObject.class);
System.out.println(Arrays.toString(methods));
System.out.println("---------- doWithLocalFields ----------");
// 当前类的所有方法,通过回调
ReflectionUtils.doWithLocalFields(DemoObject.class, System.out::println);
System.out.println("---------- getUniqueDeclaredMethods ----------");
// 若在子类重新父类方法则该方法将被移除
methods = ReflectionUtils.getUniqueDeclaredMethods(DemoObject.class);
System.out.println(Arrays.toString(methods));
System.out.println("---------- getField ----------");
// 获取字段的值
Object ret1 = ReflectionUtils.getField(field2, new DemoObject());
System.out.println(ret1);
System.out.println("---------- invokeMethod ----------");
// 调用方法
Object ret2 = ReflectionUtils.invokeMethod(method1, new DemoObject());
System.out.println(ret2);
System.out.println("---------- isToStringMethod ----------");
// 是toString方法吗? true是,false不是
boolean isString = ReflectionUtils.isToStringMethod(ReflectionUtils.findMethod(DemoObject.class, "toString"));
System.out.println(isString);
System.out.println("---------- isPublicStaticFinal ----------");
// 是否公共静态final修饰属性吗? true是,false不是
boolean isPublicStatic = ReflectionUtils.isPublicStaticFinal(field2);
System.out.println(isPublicStatic);
System.out.println("---------- isObjectMethod ----------");
// 是Object类声明的方法吗? true是,false不是
boolean isObject = ReflectionUtils.isObjectMethod(ReflectionUtils.findMethod(DemoObject.class, "toString"));
System.out.println(isObject);
System.out.println("---------- isEqualsMethod ----------");
// 是equals方法吗? true是,false不是
boolean isEquals = ReflectionUtils.isEqualsMethod(ReflectionUtils.findMethod(DemoObject.class, "equals", Object.class));
System.out.println(isEquals);
System.out.println("---------- isHashCodeMethod ----------");
// 是hashCode方法吗? true是,false不是
boolean isHashCode = ReflectionUtils.isHashCodeMethod(ReflectionUtils.findMethod(DemoObject.class, "hashCode"));
System.out.println(isHashCode);
// 清空缓存,每次查询(方法,参数)时都会做缓存。
ReflectionUtils.clearCache();
// 是Cglib重命名的方法吗? TODO test
// ReflectionUtils.isCglibRenamedMethod()

控制台

---------- findMethod ----------
public java.lang.Object cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01()
---------- findField ----------
private java.lang.String cn.xiaoyuganzeng.ispring.reflection.DemoObject.field01
---------- accessibleConstructor ----------
[public cn.xiaoyuganzeng.ispring.reflection.DemoObject(), public cn.xiaoyuganzeng.ispring.reflection.DemoObject(java.lang.String)]
---------- declaresException ----------
true - false
---------- doWithFields ----------
private java.lang.String cn.xiaoyuganzeng.ispring.reflection.DemoObject.field01
public static final java.lang.String cn.xiaoyuganzeng.ispring.reflection.DemoObject.field02
---------- doWithLocalMethods ----------
public boolean cn.xiaoyuganzeng.ispring.reflection.DemoObject.equals(java.lang.Object)
public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method02() throws java.io.IOException
public java.lang.Object cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01()
public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01(java.lang.String)
---------- doWithMethods ----------
public boolean cn.xiaoyuganzeng.ispring.reflection.DemoObject.equals(java.lang.Object)
public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method02() throws java.io.IOException
public java.lang.Object cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01()
public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01(java.lang.String)
protected void java.lang.Object.finalize() throws java.lang.Throwable
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 java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
private static native void java.lang.Object.registerNatives()
---------- getAllDeclaredMethods ----------
[public boolean cn.xiaoyuganzeng.ispring.reflection.DemoObject.equals(java.lang.Object), public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method02() throws java.io.IOException, public java.lang.Object cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01(), public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01(java.lang.String), protected void java.lang.Object.finalize() throws java.lang.Throwable, 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 java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException, public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll(), private static native void java.lang.Object.registerNatives()]
---------- getDeclaredMethods ----------
[public boolean cn.xiaoyuganzeng.ispring.reflection.DemoObject.equals(java.lang.Object), public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method02() throws java.io.IOException, public java.lang.Object cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01(), public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01(java.lang.String)]
---------- doWithLocalFields ----------
private java.lang.String cn.xiaoyuganzeng.ispring.reflection.DemoObject.field01
public static final java.lang.String cn.xiaoyuganzeng.ispring.reflection.DemoObject.field02
---------- getUniqueDeclaredMethods ----------
[public boolean cn.xiaoyuganzeng.ispring.reflection.DemoObject.equals(java.lang.Object), public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method02() throws java.io.IOException, public java.lang.Object cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01(), public void cn.xiaoyuganzeng.ispring.reflection.DemoObject.method01(java.lang.String), protected void java.lang.Object.finalize() throws java.lang.Throwable, 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 java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException, public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll(), private static native void java.lang.Object.registerNatives()]
---------- getField ----------
val=field02
---------- invokeMethod ----------
exe method01
---------- isToStringMethod ----------
true
---------- isPublicStaticFinal ----------
true
---------- isObjectMethod ----------
true
---------- isEqualsMethod ----------
true
---------- isHashCodeMethod ----------
true

Process finished with exit code 0

演示对象

public class DemoObject {

    private String field01 = "val=field01";

    public static final String field02 = "val=field02";

    public DemoObject() {
    }

    public DemoObject(String field01) {
        this.field01 = field01;
    }

    public Object method01() {
        return "exe method01";
    }

    public void method01(String name) {
        System.out.println("exe method01." + name);
    }

    public void method02() throws IOException {
        System.out.println("exe method02");
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值