反射ツール-ReflectUtil

1 はじめに

Java のリフレクション メカニズムにより、言語がより柔軟になり、オブジェクトの操作がより「動的」になるため、場合によっては、リフレクションは半分の労力で 2 倍の結果を達成できます。Hutool は、Java のリフレクション メカニズム用のツールベースのパッケージを作成しました。このパッケージには次のものが含まれます。

  • コンストラクターを取得する
  • フィールドの取得
  • フィールド値を取得する
  • 取得メソッド
  • 実行メソッド(オブジェクトメソッドと静的メソッド)

2回使用

2.1 構築方法を取得する

ここに画像の説明を挿入します

2.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
  • 親クラスのフィールドを含む、指定されたクラスのフィールドに対応するフィールド名と順序付きマップを取得します。
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 入手方法

  • クラスのすべてのメソッドを取得します。

次に、このメソッドを通じて 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 実行方法

主に次の 3 つの方法から選択できます
ここに画像の説明を挿入します

  • 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);
}

結果:

你输入的字符为:你好
返回结果这是返回的信息

おすすめ

転載: blog.csdn.net/weixin_46505978/article/details/130045755