【发布时间】:2014-08-04 14:12:15
【问题描述】:
我创建了一个类的 CGLib 动态代理,但是当我尝试访问在原始类中声明的任何字段时,我得到 java.lang.NoSuchFieldException。我需要获取该字段才能更改其值。
顺便说一下,这是代理所基于的类:
public class Person {
private String name;
....
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
...
}
这是引发上述异常的代码 sn-p(在“MethodInterceptor”的“intercept”方法内)(更具体地说是第一行):
public Object intercept(Object instance, Method jdkMethod, Object[] args, MethodProxy method) throws Throwable {
...
Field field = instance.getClass().getField("name");
field.setAccessible(true);
field.set(instance, "foo");
....
您知道访问所需字段或更改其值的其他方法吗?
谢谢。
【问题讨论】: