java 扩展类中的Java反射字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12485351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java reflection field value in extends class
提问by ?ukasz Wo?niczka
Possible Duplicate:
Access to private inherited fields via reflection in Java
可能的重复:
通过 Java 中的反射访问私有继承字段
Hello i hava got problem with init value with java reflection.
您好,我在使用 Java 反射时遇到了 init 值问题。
i have got simple class
我有简单的课
public class A extends B {
private String name;
}
public class B {
private String superName;
}
and also i have got simple function:
而且我还有简单的功能:
public void createRandom(Class<T> clazz , List<String> classFields){
try {
T object = clazz.newInstance();
for(String s : classFields){
clazz.getDeclaredField(s);
}
} catch(Exception e){
}
}
My function do other stuff but i have got problem because i have got error :
我的函数做其他事情,但我有问题,因为我有错误:
java.lang.NoSuchFieldException: superName
How can i set all class field also field from super Class using reflection ??
如何使用反射设置所有类字段也来自超类的字段?
I have got all class fields (also inherited) and i am using function field.set(Object obj, Object value)
but in this way i can not set inherited class fields :/
我已经获得了所有类字段(也继承了)并且我正在使用函数field.set(Object obj, Object value)
但是这样我无法设置继承的类字段:/
I havent got problem to get all class field i am using Spring ReflectionUtils.doWithfield.
i stored all field names in List<String> classField
, so i known all clazz fields also inherited. But my problem is how to set values into all clazz fields.
我在使用 Spring ReflectionUtils.doWithfield 时获取所有类字段没有问题。我将所有字段名称存储在 中List<String> classField
,所以我知道所有 clazz 字段也继承了。但我的问题是如何将值设置到所有 clazz 字段中。
回答by Alex Coleman
If I had to guess, I'm assuming you are calling this method on class A, and expecting to be able to view the underlying fields declared in class B, like so:
如果我不得不猜测,我假设您在类 A 上调用此方法,并希望能够查看类 B 中声明的基础字段,如下所示:
A.class.getDeclaredField("superName");
This is not the case, and will throw an Exception (java.lang.NoSuchFieldException
). Reflection does not check super classes to find fields or methods. So since class A
doesn't define superName
, it will not be found using reflection like that. However, you could modify your code to make it check all superclasses until it reaches null
as the superclass, at which point if it's still not found, it definately doesn't exist.
情况并非如此,并且会抛出异常 ( java.lang.NoSuchFieldException
)。反射不会检查超类来查找字段或方法。因此,由于 classA
没有定义superName
,因此不会像那样使用反射找到它。但是,您可以修改代码以使其检查所有超类,直到它null
成为超类,此时如果仍未找到,则它肯定不存在。
Here's an example:
下面是一个例子:
public static Field findUnderlying(Class<?> clazz, String fieldName) {
Class<?> current = clazz;
do {
try {
return current.getDeclaredField(fieldName);
} catch(Exception e) {}
} while((current = current.getSuperclass()) != null);
return null;
}
Here's an example call: findUnderlying(A.class, "superName")
; it would first check class A for the field. Since A doesn't have it, the dowhile then moves on to it's superclass, which is B (not null so continue). B does have it, so it then returns the field. If B didn't have it, it would then check Object, then return null since Object doesn't have a superclass.
下面是一个例子电话:findUnderlying(A.class, "superName")
; 它会首先检查该字段的 A 类。由于 A 没有它,所以 dowhile 会转移到它的超类,即 B(不为空,所以继续)。B 确实有它,所以它然后返回该字段。如果 B 没有它,它将检查 Object,然后返回 null,因为 Object 没有超类。
回答by aymeric
You can use:
您可以使用:
clazz.getSuperclass().getDeclaredField(s);
instead of (or in addition with some try-catch):
而不是(或另外加上一些 try-catch):
clazz.getDeclaredField(s);
EDIT:
编辑:
To set the value for the superclass, use the following:
要设置超类的值,请使用以下命令:
Field f = clazz.getSuperclass().getDeclaredField(s);
f.setAccessible(true); // Especially necessary if the field is not public
f.set(yourObject, theValue);
回答by Nandkumar Tekale
get super class using Class.getSuperclass(). Then using super class, you can get it's fields.
使用Class.getSuperclass()获取超类。然后使用超类,您可以获得它的字段。
回答by sharadendu sinha
It wont work with instance of A even if A extends from B , it is because private members are not accessible in sub classes . try changing the access of superName to protected and if it works ...
即使 A 从 B 扩展,它也不适用于 A 的实例,这是因为在子类中无法访问私有成员。尝试将 superName 的访问权限更改为 protected ,如果它有效...