反射工具-ReflectUtil

1 介绍

Java的反射机制,可以让语言变得更加灵活,对对象的操作也更加“动态”,因此在某些情况下,反射可以做到事半功倍的效果。Hutool针对Java的反射机制做了工具化封装,封装包括:

  • 获取构造方法
  • 获取字段
  • 获取字段值
  • 获取方法
  • 执行方法(对象方法和静态方法)

2 使用

2.1 获取构造方法

在这里插入图片描述

2.2 获取字段

为了看到效果,先随便建两个类:

@Data
@Accessors(chain = true)
public class User {

    private String userName;

    private int userAge;

    private String userIdCard;

}

@Data
@Accessors(chain = true)
public class Student extends User{

    private String grade;

    private String schoolName;

}
  • 获得一个类中所有字段列表,包括其父类中的字段
public static void main(String[] args) {
        Field[] fields = ReflectUtil.getFields(Student.class);
        for (Field field : fields) {
            System.out.println(field.getName());
        }
}

输出:

grade
schoolName
userName
userAge
userIdCard
  • 获取指定类中字段名和字段对应的有序Map,包括其父类中的字段
public static void main(String[] args) {
        Map<String, Field> fieldMap = ReflectUtil.getFieldMap(Student.class);
        System.out.println("获取指定类中字段名和字段对应的有序Map,包括其父类中的字段:" + fieldMap);
}

在这里插入图片描述

2.3 获取字段值

public static void main(String[] args) {
        User user = new Student()
                .setSchoolName("xx小学")
                .setGrade("3")
                .setUserName("小明")
                .setUserAge(9);

        Object[] fieldsValue = ReflectUtil.getFieldsValue(user);
        System.out.println("获取所有字段的值:" + StrUtil.join(",", fieldsValue));
}

执行结果:

获取所有字段的值:3,xx小学,小明,9,null
获取指定字段值:小明

2.4 获取方法

  • 获取某个类的所有方法 getMethods

那就通过这个方法看一下ReflectUtil这个工具类都有哪些方法吧:

public static void main(String[] args) {
        // 获取某个类的所有方法
        Method[] methods = ReflectUtil.getMethods(ReflectUtil.class);
        System.out.println("获取某个类的所有方法:");
        for (Method method : methods) {
            System.out.println(method);
        }
}

结果:

获取某个类的所有方法:
public static java.lang.Object cn.hutool.core.util.ReflectUtil.invoke(java.lang.Object,java.lang.reflect.Method,java.lang.Object[]) throws cn.hutool.core.exceptions.UtilException
public static java.lang.Object cn.hutool.core.util.ReflectUtil.invoke(java.lang.Object,java.lang.String,java.lang.Object[]) throws cn.hutool.core.exceptions.UtilException
public static java.lang.reflect.Constructor cn.hutool.core.util.ReflectUtil.getConstructor(java.lang.Class,java.lang.Class[])
public static java.lang.reflect.Constructor[] cn.hutool.core.util.ReflectUtil.getConstructors(java.lang.Class) throws java.lang.SecurityException
public static java.lang.reflect.Field cn.hutool.core.util.ReflectUtil.getField(java.lang.Class,java.lang.String) throws java.lang.SecurityException
public static java.lang.reflect.Field[] cn.hutool.core.util.ReflectUtil.getFields(java.lang.Class) throws java.lang.SecurityException
public static java.lang.reflect.Method cn.hutool.core.util.ReflectUtil.getMethod(java.lang.Class,boolean,java.lang.String,java.lang.Class[]) throws java.lang.SecurityException
public static java.lang.reflect.Method cn.hutool.core.util.ReflectUtil.getMethod(java.lang.Class,java.lang.String,java.lang.Class[]) throws java.lang.SecurityException
public static java.lang.reflect.Method[] cn.hutool.core.util.ReflectUtil.getMethods(java.lang.Class) throws java.lang.SecurityException
public static java.lang.reflect.Method[] cn.hutool.core.util.ReflectUtil.getMethods(java.lang.Class,cn.hutool.core.lang.Filter) throws java.lang.SecurityException
public static java.lang.Object cn.hutool.core.util.ReflectUtil.newInstance(java.lang.Class,java.lang.Object[]) throws cn.hutool.core.exceptions.UtilException
public static java.lang.Object cn.hutool.core.util.ReflectUtil.newInstance(java.lang.String) throws cn.hutool.core.exceptions.UtilException
public static java.lang.reflect.AccessibleObject cn.hutool.core.util.ReflectUtil.setAccessible(java.lang.reflect.AccessibleObject)
public static java.lang.Object cn.hutool.core.util.ReflectUtil.invokeStatic(java.lang.reflect.Method,java.lang.Object[]) throws cn.hutool.core.exceptions.UtilException
public static java.lang.Object[] cn.hutool.core.util.ReflectUtil.getFieldsValue(java.lang.Object)
public static java.lang.Object cn.hutool.core.util.ReflectUtil.getFieldValue(java.lang.Object,java.lang.String) throws cn.hutool.core.exceptions.UtilException
public static java.lang.Object cn.hutool.core.util.ReflectUtil.getFieldValue(java.lang.Object,java.lang.reflect.Field) throws cn.hutool.core.exceptions.UtilException
public static java.lang.reflect.Method cn.hutool.core.util.ReflectUtil.getMethodOfObj(java.lang.Object,java.lang.String,java.lang.Object[]) throws java.lang.SecurityException
public static boolean cn.hutool.core.util.ReflectUtil.isHashCodeMethod(java.lang.reflect.Method)
public static void cn.hutool.core.util.ReflectUtil.setFieldValue(java.lang.Object,java.lang.String,java.lang.Object) throws cn.hutool.core.exceptions.UtilException
public static void cn.hutool.core.util.ReflectUtil.setFieldValue(java.lang.Object,java.lang.reflect.Field,java.lang.Object) throws cn.hutool.core.exceptions.UtilException
public static boolean cn.hutool.core.util.ReflectUtil.hasField(java.lang.Class,java.lang.String) throws java.lang.SecurityException
public static boolean cn.hutool.core.util.ReflectUtil.isEqualsMethod(java.lang.reflect.Method)
public static java.lang.reflect.Method cn.hutool.core.util.ReflectUtil.getMethodByName(java.lang.Class,boolean,java.lang.String) throws java.lang.SecurityException
public static java.lang.reflect.Method cn.hutool.core.util.ReflectUtil.getMethodByName(java.lang.Class,java.lang.String) throws java.lang.SecurityException
public static java.lang.String cn.hutool.core.util.ReflectUtil.getFieldName(java.lang.reflect.Field)
public static java.lang.reflect.Method cn.hutool.core.util.ReflectUtil.getPublicMethod(java.lang.Class,java.lang.String,java.lang.Class[]) throws java.lang.SecurityException
public static boolean cn.hutool.core.util.ReflectUtil.isToStringMethod(java.lang.reflect.Method)
public static java.util.Set cn.hutool.core.util.ReflectUtil.getMethodNames(java.lang.Class) throws java.lang.SecurityException
public static java.lang.reflect.Method[] cn.hutool.core.util.ReflectUtil.getPublicMethods(java.lang.Class)
public static java.util.List cn.hutool.core.util.ReflectUtil.getPublicMethods(java.lang.Class,cn.hutool.core.lang.Filter)
public static java.util.List cn.hutool.core.util.ReflectUtil.getPublicMethods(java.lang.Class,java.lang.String[])
public static java.util.List cn.hutool.core.util.ReflectUtil.getPublicMethods(java.lang.Class,java.lang.reflect.Method[])
public static boolean cn.hutool.core.util.ReflectUtil.isEmptyParam(java.lang.reflect.Method)
public static java.lang.Object cn.hutool.core.util.ReflectUtil.invokeWithCheck(java.lang.Object,java.lang.reflect.Method,java.lang.Object[]) throws cn.hutool.core.exceptions.UtilException
public static java.util.Map cn.hutool.core.util.ReflectUtil.getFieldMap(java.lang.Class)
public static java.lang.Object cn.hutool.core.util.ReflectUtil.getStaticFieldValue(java.lang.reflect.Field) throws cn.hutool.core.exceptions.UtilException
private static boolean cn.hutool.core.util.ReflectUtil.lambda$getPublicMethods$1(java.util.HashSet,java.lang.reflect.Method)
public static java.lang.reflect.Method cn.hutool.core.util.ReflectUtil.getMethodByNameIgnoreCase(java.lang.Class,java.lang.String) throws java.lang.SecurityException
public static java.util.Set cn.hutool.core.util.ReflectUtil.getPublicMethodNames(java.lang.Class)
public static java.lang.reflect.Field[] cn.hutool.core.util.ReflectUtil.getFieldsDirectly(java.lang.Class,boolean) throws java.lang.SecurityException
public static java.lang.reflect.Constructor[] cn.hutool.core.util.ReflectUtil.getConstructorsDirectly(java.lang.Class) throws java.lang.SecurityException
public static java.lang.Object cn.hutool.core.util.ReflectUtil.newInstanceIfPossible(java.lang.Class)
private static boolean cn.hutool.core.util.ReflectUtil.lambda$getField$0(java.lang.String,java.lang.reflect.Field)
public static java.lang.reflect.Method cn.hutool.core.util.ReflectUtil.getMethodIgnoreCase(java.lang.Class,java.lang.String,java.lang.Class[]) throws java.lang.SecurityException
public static java.lang.reflect.Method[] cn.hutool.core.util.ReflectUtil.getMethodsDirectly(java.lang.Class,boolean) throws java.lang.SecurityException
private static boolean cn.hutool.core.util.ReflectUtil.lambda$getPublicMethods$2(java.util.HashSet,java.lang.reflect.Method)
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()
  • 获取某个类的指定方法 getMethod
    此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回null

下边这个方法也是ReflectUtil工具类中的,就指定获取这个方法吧:
在这里插入图片描述
简单的测试一下:

public static void main(String[] args) {
        Method getMethod = ReflectUtil.getMethod(ReflectUtil.class, "isToStringMethod");
        System.out.println("获取某个类的指定方法:");
        System.out.println(getMethod);
        System.out.println("获取某个类的指定方法,加参数:");
        Method getMethodByParamType = ReflectUtil.getMethod(ReflectUtil.class, "isToStringMethod", Method.class);
        System.out.println(getMethodByParamType);
}

结果:

获取某个类的指定方法:
null
获取某个类的指定方法,加参数:
public static boolean cn.hutool.core.util.ReflectUtil.isToStringMethod(java.lang.reflect.Method)

2.5 执行方法

主要有以下三个方法可选:
在这里插入图片描述
其中:

  • obj – 方法所在对象
  • methodName – 方法名
  • args – 参数列表

写一个测试类测试一下。
先随便建一个类:

public class TestInvokeMeth {

    public String test(String str) {
        System.out.println("你输入的字符为:" + str);
        return "这是返回的信息";
    }

}

测试方法:

public static void main(String[] args) {
        TestInvokeMeth testInvokeMeth = new TestInvokeMeth();
        Object getFields = ReflectUtil.invoke(testInvokeMeth, "test","你好");
        System.out.println("返回结果" + getFields);
}

执行结果:

你输入的字符为:你好
返回结果这是返回的信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值