Java反射“无法设置"错误

编程入门 行业动态 更新时间:2024-10-23 19:31:39
本文介绍了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反射“无法设置"错误

本文发布于:2023-07-30 00:38:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1245009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   错误   Java   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!