我在用着PropertyUtils.copyProperties() http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/PropertyUtils.html#copyProperties%28java.lang.Object,%20java.lang.Object%29通过反射复制对象的属性,并且它曾经运行良好。然而最近,它开始什么也不做。
它不会抛出异常,但只是不会复制任何字段。尽管源对象中存在非空字段,但目标对象的所有字段仍为空。
我不知道如何重现这个。对我来说,这种情况一直在发生,但它是在一个项目中,我不能在这里发布。该项目使用 Play Framework,它会进行一些字节码操作,因此这可能是罪魁祸首。
关于可能导致此问题的原因或如何调试有什么建议或想法吗?我可以尝试的替代字段复制器也很受欢迎(我之前尝试过 BeanUtils 一次,但由于一些我现在不记得的警告而切换到 PropertyUtils)。
我想我明白了。今天发生在我身上。我只是用它做了一些小测试,但没有成功。这是代码:
static class TesteP {
private String a;
private String b;
private String c;
public String getA() {
return this.a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return this.b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return this.c;
}
public void setC(String c) {
this.c = c;
}
@Override
public String toString() {
return new ToStringBuilder(this.getClass()).add("a", this.a).add("b", this.b).toString();
}
}
static class RestP {
private String a;
public String getA() {
return this.a;
}
public void setA(String a) {
this.a = a;
}
@Override
public String toString() {
return this.a;
}
}
public static void main(String[] args)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TesteP p = new TesteP();
p.setA("AAAA");
p.setB("BBB");
TesteP pp = new TesteP();
RestP p2 = new RestP();
p2.setA("aaaa");
PropertyUtils.copyProperties(p,p2);
}
它解决了这个问题,将课程公开。也许您的一门课程不是公开的。这是我的案例的解决方案:
public static class TesteP {
private String a;
private String b;
private String c;
public String getA() {
return this.a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return this.b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return this.c;
}
public void setC(String c) {
this.c = c;
}
@Override
public String toString() {
return new ToStringBuilder(this.getClass()).add("a", this.a).add("b", this.b).toString();
}
}
public static class RestP {
private String a;
public String getA() {
return this.a;
}
public void setA(String a) {
this.a = a;
}
@Override
public String toString() {
return this.a;
}
}
public static void main(String[] args)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TesteP p = new TesteP();
p.setA("AAAA");
p.setB("BBB");
TesteP pp = new TesteP();
RestP p2 = new RestP();
p2.setA("aaaa");
PropertyUtils.copyProperties(p,p2);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)