GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] New Rule InvalidJavaBean · Issue #4177 · pmd/pmd

[java] New Rule InvalidJavaBean #4177

New issue
Copy link
New issue
Copy link
Closed
#4203
Closed
[java] New Rule InvalidJavaBean#4177
#4203
Copy link
Labels
a:new-ruleProposal to add a new built-in rule
Milestone
6.52.0

Description

@adangel
adangel
opened on Oct 27, 2022
Issue body actions

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:

  • [java] BeanMembersShouldSerialize : add a property to ignore class not implementing the Serializable interface #403
  • [java] BeanMembersShouldSerialize is extremely noisy #1668

References:

  • https://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/
  • https://en.wikipedia.org/wiki/JavaBeans

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

Activity

  1. adangel
    added
    a:new-ruleProposal to add a new built-in rule
    on Oct 27, 2022
  2. jsotuyod commented on Oct 27, 2022

    @jsotuyod
    jsotuyod
    on Oct 27, 2022
    Member
    More actions

    Some personal comments…

    Firstly, I'd go for the approach of having to setup the rule with a required package to avoid the noisiness. The criteria for inference are too vague and would produce significant FPs / FNs.

    I'd also have the check for serialization be optional through a property. Why? Because even though the actual usage of Java Beans as communication value objects has declined with JavaEE, the idea of an object with a default constructor with getters and setters has endured, and is used massively in other scenarios, such as:

    • Bean Validation (JSR 303 / 380), used standalone but also supported by Jersey, Spring and plenty of others.
    • JAXB
    • Jackson JSON library (when no annotations present)

    Being able to enforce the compliance of these would be useful, as an error is mostly silent, with properties being dropped…

    Reacted by Andreas Dangel
  3. adangel
    added this to the 6.52.0 milestone on Oct 27, 2022
  4. adangel
    added 2 commits that reference this issue on Nov 10, 2022

    [java] New Rule InvalidJavaBean

    1a344d8

    [doc] Update release notes (pmd#4177)

    a505bbf
  5. adangel
    closed this as completedin #4203on Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.52.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.