如何检测我的 bean 中的(无意的)只写属性?

发布于 2024-12-29 20:42:02 字数 367 浏览 4 评论 0原文

我一直在使用“Drools Planner”包开发我的第一个 Java 项目,在我的类中使用 JavaBean 模式。我无意中将我的一个类编码为具有只写属性,这触发了这个错误

如果该属性是只写的,确实可能返回 null,如所解释的 在 JavaBeans(TM) 规范 1.01 最终版本中


我如何以编程方式检查我的类并查看哪个类具有违规的只写属性?

或者,由于只有 6 个类,是否有一些经验法则可以通过肉眼找到它?

I have been working on my first Java project with the 'Drools Planner' package, using the JavaBean pattern for my classes. I inadvertently coded one of my classes to have a write-only property, which triggered this bug

might indeed return null if the property is write-only, as explained
in the JavaBeans(TM) Specification 1.01 Final Release


How do I programatically check my classes and see which one has the offending write-only property?

Or, since it's a matter of just 6 classes, is there some rule of thumb to find it by eyeball?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2025-01-05 20:42:02

以编程方式,您可以使用 bean 描述符来找出没有属性 getter 的类

import java.beans.*

Class[] clazzes=new Class[]{<your classes here>};
for(Class c:clazzes){
   try {
    for (PropertyDescriptor pd : Introspector.getBeanInfo(c).getPropertyDescriptors()) {
        System.out.println(pd.getName()+":"+pd.getDisplayName());
          if (pd.getReadMethod() == null){
          System.out.println("No read method for : "+pd.getName()+" in : "+c);
       }
     }
} catch (IntrospectionException e) {
    e.printStackTrace();
}
}

您还可以检查 IDE 的代码完成来检查您的所有属性是否都有 getter,启动它,然后输入“get”和“is”,它将列出所有吸气剂:)

Programatically you can use the bean descriptor to figure out the classes with no getter for properties

import java.beans.*

Class[] clazzes=new Class[]{<your classes here>};
for(Class c:clazzes){
   try {
    for (PropertyDescriptor pd : Introspector.getBeanInfo(c).getPropertyDescriptors()) {
        System.out.println(pd.getName()+":"+pd.getDisplayName());
          if (pd.getReadMethod() == null){
          System.out.println("No read method for : "+pd.getName()+" in : "+c);
       }
     }
} catch (IntrospectionException e) {
    e.printStackTrace();
}
}

You can also check the IDE's code completion to check whether your all properties have getters, initiate it and then type in 'get'and 'is' it will list all the getters :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文