[java] InvalidJavaBean - update rule doc

This commit is contained in:
Andreas Dangel committed 2022-11-11 15:18:09 +01:00
1 parent 7e8a886b93
commit 9ced8ad63c
3 files changed
+23 -3

No files matched your search

@@ -904,15 +904,30 @@ public class Foo {
<rule name="InvalidJavaBean"
language="java"
since="6.52.0"
message="Invalid Java Bean"
message="The bean ''{0}'' is missing a getter for property ''{1}''."
class="net.sourceforge.pmd.lang.java.rule.design.InvalidJavaBeanRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_design.html#invalidjavabean">
<description>
Identifies beans, that don't follow the [JavaBeans API specification](https://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/).
Each non-static field should have both a getter and a setter method. If the field is just used internally and is not
a bean property, then the field should be marked as `transient`.
The rule verifies that the type of the field is the same as the result type of the getter. And that this type matches
the type used in the setter.
The rule also checks, that there is a no-arg or default constructor available.
Optionally the rule also verifies, that the bean implements `java.io.Serializable`. While this is a requirement for the
original JavaBeans specification, frameworks nowadays don't strictly require this anymore.
In order to avoid many false positives in classes that are not beans, the rule needs to be explicitly
enabled by configuring the property `packages`.
</description>
<priority>3</priority>
<example>
<![CDATA[
package org.example.beans;
public class MyBean { // <-- bean is not serializable, missing "implements Serializable"
private String label; // <-- missing setter for property "label"
@@ -141,6 +141,11 @@
<rule ref="category/java/design.xml/FinalFieldCouldBeStatic"/>
<!-- <rule ref="category/java/design.xml/GodClass" /> -->
<!-- <rule ref="category/java/design.xml/ImmutableField" /> -->
<!-- <rule ref="category/java/design.xml/InvalidJavaBean">-->
<!-- <properties>-->
<!-- <property name="packages" value="org.example.beans" />-->
<!-- </properties>-->
<!-- </rule>-->
<!-- <rule ref="category/java/design.xml/LawOfDemeter" /> -->
<rule ref="category/java/design.xml/LogicInversion"/>
<!-- <rule ref="category/java/design.xml/LoosePackageCoupling"> -->
@@ -7,14 +7,14 @@
<test-code>
<description>Example code</description>
<rule-property name="ensureSerialization">true</rule-property>
<rule-property name="packages"></rule-property>
<expected-problems>2</expected-problems>
<expected-linenumbers>1,2</expected-linenumbers>
<expected-linenumbers>2,3</expected-linenumbers>
<expected-messages>
<message>The bean 'MyBean' does not implement java.io.Serializable.</message>
<message>The bean 'MyBean' is missing a setter for property 'Label'.</message>
</expected-messages>
<code><![CDATA[
package org.example.beans;
public class MyBean { // <-- bean is not serializable, missing "implements Serializable"
private String label; // <-- missing setter for property "label"