获取非实例化类的字段值-反射

时间:2019-02-25 17:07:12

标签: java reflection

如何迭代和获取不可实例化类的所有字段的值:

import java.lang.reflect.Field;

public class NonInstantiableClass {
    private Integer a1 = 1;
    private String a2 = "a";

    private NonInstantiableClass () {
         throw new AssertionError();
    }

    public static void printVariables () throws IllegalAccessException {

        for (Field field : NonInstantiableClass.class.getDeclaredFields()) {
            field.setAccessible(true);
            System.out.println(field.getName()
                    + " - " + field.getType()
                    + " - " + field.get(NonInstantiableClass.class));
        }
    }


    public static void main(String args[]) throws IllegalAccessException {
        NonInstantiableClass.printVariables();
    }
}

鉴于此代码出现以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.Integer field NonInstanciableClass.a1 to java.lang.Class
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.base/java.lang.reflect.Field.get(Field.java:418)
    at NonInstanciableClass.printVariables(NonInstanciableClass.java:17)
    at NonInstanciableClass.main(NonInstanciableClass.java:23)

如果该类是可实例化的,则相同的代码可用于实例:

import java.lang.reflect.Field;

public class NonInstantiableClass {
    private Integer a1 = 1;
    private String a2 = "a";

    //private NonInstantiableClass () {
    //     throw new AssertionError();
    //}

    public static void printVariables () throws IllegalAccessException {

        for (Field field : NonInstantiableClass.class.getDeclaredFields()) {
            field.setAccessible(true);
            System.out.println(field.getName()
                    + " - " + field.getType()
                    + " - " + field.get(new NonInstantiableClass()));
        }
    }


    public static void main(String args[]) throws IllegalAccessException {
        NonInstantiableClass.printVariables();
    }
}

是否有更好的方法来解决此问题?

谢谢。

编辑:

正如Dylan所指出的,如果类不可实例化,则实例字段将无法初始化。当然,这些字段是静态的:

private static Integer a1 = 1;
private static String a2 = "a";

1 个答案:

答案 0 :(得分:1)

尝试一下:

import java.lang.reflect.Field;

public class NonInstantiableClass {
    private static Integer a1 = 1;
    private static String a2 = "a";

    private NonInstantiableClass () {
         throw new AssertionError();
    }

    public static void printVariables () throws IllegalAccessException {

        for (Field field : NonInstantiableClass.class.getDeclaredFields()) {
            field.setAccessible(true);
            System.out.println(field.getName()
                    + " - " + field.getType()
                    + " - " + field.get(NonInstantiableClass.class));
        }
    }

    public static void main(String args[]) throws IllegalAccessException {
        NonInstantiableClass.printVariables();
    }
}

这只会告诉您静态字段。如果字段不是静态的,则getVariableValue返回null。