Skip to content

[java] New Rule InvalidJavaBean #4177

@adangel

Description

@adangel

Proposed Rule Name: InvalidJavaBean

Proposed Category: Design

Description:

This rule would provide the "bean" part of BeanMembersShouldSerialize.
It would check, that the analyzed class adheres to the Java Beans Specification, that means:

  • Each field has a correctly named getter and setter
  • for indexed properties, check the field is an array and the types match
  • event listeners implement java.util.EventListener
  • event listeners always come in pairs (addXXListener, removeXXListener)
  • the bean as a default no-arg constructor
  • the bean is serializable (that means, it implements java.io.Serializable- for the actually check, see the rule NonSerializableClass [java] Rename BeanMembersShouldSerialize to NonSerializableClass #4176)

Care must be taken to make this rule not as noisy as #1668 . One strategy could be:

  • consider only classes, that have at least one getter or setter (that means, a method matching the naming conventions). These classes could be beans.
  • consider imports: PropertyChangeListener, VetoableChangeListener, EventListener might give a hint, that the class under analysis might be a bean.
  • maybe have a minimum number of properties - so that the rule is not triggered yet, when a class has only one getter?
  • providing a property to configure a package or a naming convention for bean classes?
  • Consider annotations in java.beans

Related issues:

References:

Code Sample:

public class MyBean {        // <-- bean is not serializable, missing "implements Serializable"
    private String label;    // <-- missing setter for property "label"

    public String getLabel() {
        return label;
    }
}

Possible Properties:

  • package - a regex to configure the packages where this rule should be applied to
  • ensureSerialization - whether or not to check for java.io.Serializable

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-ruleProposal to add a new built-in rule

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions