java - PropertyUtils.copyProperties 什么时候可以静默失败?

标签 java reflection playframework apache-commons-beanutils

我正在使用 PropertyUtils.copyProperties()通过反射复制对象的属性,它曾经工作得很好。然而最近,它开始无所事事。

它不会抛出异常,但不会复制任何字段。尽管源对象中有非空字段,但目标对象的所有字段都保持为空。

我不知道如何重现这个。对我来说,它一直在发生,但它在我不能在这里发布的项目中。该项目使用 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);

}

关于java - PropertyUtils.copyProperties 什么时候可以静默失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8384814/

相关文章:

java - Android 视频直播无法正常工作

reflection - Kotlin - 是否有一种聪明的方法可以为类的所有方法抛出 NotImplementedError?

java - 您最喜欢 Java API 的哪个领域?

c# - 避免用户代码调用 C# 中的反射

java - 如何在 JavaDocs 中引用 "*/"

java - AWS 可以用作 android 后端吗?或者后端程序使用 AWS?

java - 关于 OCA 考试垃圾收集的问题

java - formFactory.form() 不存在!游戏框架

java - 玩!框架运行抛出 UnsupportedClassVersionError

java - 在 Play 1.2 中,有没有办法在不编辑模块源代码的情况下保护模块提供的 Controller ?