Java反射“无法设置"错误 [英] Java reflection 'Can not set' error

查看:91
本文介绍了Java反射“无法设置"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 反射获取通用 Field 属性的实例,以便执行此 Field 的方法.

I am trying to get the instance of a generic Field attribute using Java reflection in order to execute a method of this Field.

例如,如果我有一个带有方法 getValue() 的 MyType 类型类,并且我有另一个带有 MyType 类属性的 MyContainer 类,我正在寻找的是动态获取此字段并执行 getValue() 方法.这可能吗?

For instance, If I have a type MyType class with a method getValue() and I have another class MyContainer with an attribute of MyType class, what I am looking for is to get this Field dynamically and execute the getValue() method. Is this possible?

使用以下代码结构,我得到:

With the following code structure, I am getting:

Exception in thread "main" java.lang.IllegalArgumentException: Can not set test.MyType field test.MyContainer.field1 to test.MyType
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:393)
    at test.Main.main(Main.java:24)

我的容器

public class MyContainer {

MyType field1;
MyType field2;

public MyContainer() {
    super();
}

public MyContainer(MyType field1, MyType field2) {
    super();
    this.field1 = field1;
    this.field2 = field2;
}

public MyType getField1() {
    return field1;
}

public MyType getField2() {
    return field2;
}
}

我的类型

public class MyType {

private MyEnum var1;
private String var2;

public MyType() {
    super();
}

public MyType(MyEnum var1, String var2) {
    this.var1 = var1;
    this.var2 = var2;
}

public MyEnum getVar1() {
    return var1;
}

public String getVar2() {
    return var2;
}   
}

MyEnum

public enum MyEnum {

A(""),

N("0");

private final String value;

private MyEnum(String value) {
    this.value = value;
}

@Override
public String toString() {
    return this.value;
}
}

执行

    java.lang.reflect.Field[] allFields = MyContainer.class.getDeclaredFields();        
    MyType obj = new MyType();

    for (java.lang.reflect.Field field : allFields) {

        if ( MyType.class == field.getType() ){                 

            field.setAccessible(true);              
            MyType myAux = (MyType) field.get(obj);             
            String dowhatever = myAux.getVar2();    

        }
    }

正如您在异常中看到的那样,test.MyType 字段类型似乎完全等同于转换中提供的 test.MyType.我做错了什么?

As you could see in the exception, test.MyType field type seems to be totally equivalent to test.MyType provided in the casting. What I am doing wrong?

推荐答案

obj 应该属于 MyContainer 类,而不是 MyType

obj should be of class MyContainer, not MyType

java.lang.reflect.Field[] allFields = MyContainer.class.getDeclaredFields();        
MyContainer obj = new MyContainer();

for (java.lang.reflect.Field field : allFields) {

    if ( MyType.class == field.getType() ){                 

        field.setAccessible(true);              
        MyType myAux = (MyType) field.get(obj);             
        String dowhatever = myAux.getVar2();    

    }
}

请参阅此处

get(Object obj)

在指定的对象上返回由此 Field 表示的字段的值.

Returns the value of the field represented by this Field, on the specified object.

这篇关于Java反射“无法设置"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆