[英]Get field values in Runtime
How to get value of the field in Runtime for Java? 如何在Runtime for Java中获取字段的值?
EDIT: 编辑:
using this construction: 使用此构造:
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(
false);
for (BeanDefinition bd : scanner
.findCandidateComponents("aspectlogging"))
{
Class myTarget = null;
try {
myTarget = Class.forName(bd.getBeanClassName());
}
catch (ClassNotFoundException e) {...}
for (Field f:myTarget.getDeclaredFields()){
try {
System.out.println("FIELD: " + f.get(myTarget));
} catch (IllegalArgumentException e) {...}
catch (IllegalAccessException e) {...}
} }
I've got java.lang.IllegalAccessException
, 我有java.lang.IllegalAccessException
,
when call f.get(myTarget)
, 当调用f.get(myTarget)
,
where myTarget
is the instance of bean got in Runtime and f
is its field. 其中myTarget
是运行时中获得的bean的实例,而f
是其字段。
when run in loop following: 在以下循环运行时:
System.out.println("FIELD: " + f);
got field names OK: 字段名称确定:
FIELD: private java.lang.String aspectlogging.TestableBean.name
FIELD: private java.lang.String aspectlogging.TestableBean.name
It's quite strange,that value can't be got. 很奇怪,无法获得价值。
arg
( called obj
in the Javadoc ) is the instance on which to operate. arg
( 在Javadoc中称为obj
)是要对其进行操作的实例。 In your example, the instance is bd
, so use f.getInt(bd)
to extract an int
field and so on. 在您的示例中,实例为bd
,因此请使用f.getInt(bd)
提取一个int
字段,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.