Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMD "Bean Members Should Serialize" rule. Can we do it in more smart way?

Here is (probably good for someone) "Bean Members Should Serialize" PMD rule which states the following:

If a class is a bean, or is referenced by a bean directly or indirectly it needs to be serializable. Member variables need to be marked as transient, static, or have accessor methods in the class. Marking variables as transient is the safest and easiest modification. Accessor methods should follow the Java naming conventions, i.e. for a variable named foo, getFoo() and setFoo() accessor methods should be provided.

The problem is it currently neither checks 'class is a bean, or is referenced by a bean' condition or even checks class implements 'Serializable' interface. So having dumb simple class with couple of private fields without accessors (even if class is final) you get major defect marked by Sonar. For most classes it looks funny so currently I definitely am going to deactivate this rule.

But I do like to have such check for classes that are real beans. Is it possible (and how) to do similar (but smarter) checks with Sonar and 'standard set' of open sources analyzers (CheckStyle, PMD, findbugs)? Is there any other way to do so?

like image 457
Roman Nikitchenko Avatar asked Aug 07 '14 10:08

Roman Nikitchenko


1 Answers

Using Sonar, enter as administrator and go to Settings > Quality Profiles > Java > [Your profile] Search "Fields in a "Serializable" class should either be transient or serializable" (without quotes, of course) and activate it.

like image 160
dtortola Avatar answered Oct 27 '22 23:10

dtortola