GeezerVison is showing github.com — [java] BeanMembersShouldSerialize is extremely noisy · Issue #1668 · pmd/pmd
[java] BeanMembersShouldSerialize is extremely noisy #1668DescriptionRule doc: BeanMembersShouldSerialize
This is more of a question but could be seen as documentation bug.
How does the BeanMembersShouldSerialize rule classify a class as bean or used by a bean?
I have a class that will never be serialized ever or someone did something horribly wrong.
I see that for some cases it makes sense to mark members as transient even if they do not implement Serializable as other serialization mechanisms like Gson could also take that modifier into account.
But from the description I don't see when this rule applies to a class and when not and why it is applied to my class which is actually used by no other code as it is meant to be used by consumers of my library. Reacted by Cristian Garcia, Daniel Jipa, Christian Hujer, Philip Moore, Anders, jontierno, Joe Lust, DavidGuillen, Kunal Dabir, jochenberger and 6 more Activityjsotuyod commented on Feb 19, 2019 @Vampire the rule has no criteria to determine which classes are intended as a bean and which are not; and defaults to treating all classes as beans.
After all, a Java Bean is just a standard class where:
- All properties are private (use getters/setters)
- Has a public no-argument constructor
- Implements Serializable
Any rule to enforce these can't rely on them being followed.
I agree the rule is probably too noisy… but the only way around I can think of, is requiring the user to configure a property with a set of packages where beans are to be found, and disable the rule as misconfigured if not present… I'm not thrilled by this alternative either, but maybe someone else has a better idea. Reacted by Luhao and LeonMarino1 Vampire commented on Feb 19, 2019 ContributorAuthor More actions If not, maybe at least the name should change. :-)
I think I just disable it here. wyldbill commented on Oct 16, 2019 @jsotuyod - This:
is where the hassle is. PMD seems to assume that classes are beans when they don't implement/aren't Serializable. BeanMembersShouldSerialize is triggered despite the fact that by (the supplied) definition, a class is not a bean.
Check it out:
public class NotSerializable {
private final int myValue;
public NotSerializable(final int value) {
myValue = value;
}
}
PMD will raise BeanMembersShouldSerialize on the myValue field, despite the fact that the class is not Serializable. As far as being used by another bean, the failure should be in the composing bean for including a field of non-Serializable type. In this case, the composing bean should declare the offending field transient Reacted by Christian Hujer, Mark, Martin Meeser, Kunal Dabir and Jan Holthuis wyldbill commented on Oct 16, 2019 Reacted by Christian Hujer and Joe Lust jsotuyod commented on Oct 17, 2019 @wyldbill thanks for your comments.
Unfortunately, as already explained, looking for serializable classes is not a fix. Not all serializable classes are intended to be Java beans, and the same problem would arise with anyone using standard Java serialization and coming across these reports. We would be just shoving this under the carpet.
The only true fix I can think for this rule, as already stated is requiring the user to configure a property with a set of packages where beans are to be found, and disable the rule as misconfigured if not present.
Moreover, given the rule name, I would say the rule is actually incomplete, and if something is intended to be a bean, it should check for both, the existence of the no-arg constructor and the implementation of Serializable (none of which are currently performed). This way, when I say "my beans are under this package" the rule can enforce those are actually proper Java beans without assuming I did right any particular requirement.
Help implementing this is always welcomed. changed the title [-][java] BeanMembersShouldSerialize documentation[/-] [+][java] BeanMembersShouldSerialize is extremely noisy[/+] on Oct 17, 2019CristianGM commented on Oct 23, 2019 This rule is forcing me to stop using ErrorProne ruleset on all the company projects.
Every single warning I have it's a false positive, and I have hundreds, I would vote to at least remove it from ErrorProne as that's the Gradle plugin default ( I guess it's useful for someone so I don't say removing it ) Reacted by Christian Hujer, Emac and Martin Meeser adangel commented on Oct 26, 2019 @CristianGM It seems, that you don't use a custom ruleset.
Please refer to https://pmd.github.io/latest/pmd_userdocs_making_rulesets.html to customize, which rules you want to have executed and are useful for your project.
We definitely won't remove the rule from the error prone category, but I'd rather recommend, that you override the default gradle configuration to use your custom ruleset.
Note: Since gradle provides two ways for configuring rulesets, you need probably to remove the defaults via ruleSets = [] and configure your custom ruleset via ruleSetFiles = files("config/pmd/myRuleSet.xml").
Hope that helps. Reacted by Cihat Imamoglu and rkrisztian CristianGM commented on Oct 26, 2019 I know how to use a custom ruleset but, don't you think that having a rule in the default ruleset that is known to give false positives is not the right thing to do?
IMHO for a rule to be a default it should be proven to be useful and correct. Reacted by Christian Hujer, DavidGuillen, Martin Meeser and Ting-Lan Wang jsotuyod commented on Oct 27, 2019 @CristianGM please notice, PMD has no such thing as a "default ruleset", we only group rules into standard categories. Choosing to use a given category as default configuration is actually up to Gradle. CristianGM commented on Oct 27, 2019 @jsotuyod true, Gradle defaults to ErrorProne and this rule is definitely ErrorProne, but the rule itself, not just what it checks for. Reacted by Christian Hujer, DavidGuillen and Martin Meeser oowekyala commented on Feb 6, 2020 This is probably because the type of the example classes used in the tests are not on the classpath. We should improve this function to look into the ExtendsList and ImplementsList if the parameter is a type declaration. Reacted by Christian Hujer CristianGM commented on Mar 26, 2020 marcindabrowski commented on Jul 21, 2022 It is still very noisy :(. added a commit that references this issue on Nov 4, 2022This issue will close once commit 51067c2 is merged into the 'main' branch.51067c2
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|