From 9532552eaaad564b4aabbb70b4f663b53f5f5f00 Mon Sep 17 00:00:00 2001 From: Shubham Date: Wed, 30 Jan 2019 11:09:44 +0530 Subject: [PATCH] BeanMembersShouldSerializeRule aware of lombok accessors Extended AbstractLombokAwareRule to make BeanMembersShouldSerializeRule aware of lombok annotations. Checking if class or any of the variables have lombok annotations and avoid BeanMembersShouldSerializeRule on these variables. --- .../BeanMembersShouldSerializeRule.java | 10 ++- .../xml/BeanMembersShouldSerialize.xml | 78 +++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/BeanMembersShouldSerializeRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/BeanMembersShouldSerializeRule.java index b36674a30e..9b16bf3c5c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/BeanMembersShouldSerializeRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/BeanMembersShouldSerializeRule.java @@ -21,14 +21,15 @@ import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator; import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType; import net.sourceforge.pmd.lang.java.ast.ASTResultType; import net.sourceforge.pmd.lang.java.ast.AccessNode; -import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; +import net.sourceforge.pmd.lang.java.ast.Annotatable; +import net.sourceforge.pmd.lang.java.rule.AbstractLombokAwareRule; import net.sourceforge.pmd.lang.java.symboltable.ClassScope; import net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration; import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration; import net.sourceforge.pmd.lang.symboltable.NameOccurrence; import net.sourceforge.pmd.properties.PropertyDescriptor; -public class BeanMembersShouldSerializeRule extends AbstractJavaRule { +public class BeanMembersShouldSerializeRule extends AbstractLombokAwareRule { private String prefixProperty; @@ -62,6 +63,8 @@ public class BeanMembersShouldSerializeRule extends AbstractJavaRule { return data; } + boolean classHasLombok = hasLombokAnnotation(node); + Map> methods = node.getScope().getEnclosingScope(ClassScope.class) .getMethodDeclarations(); List getSetMethList = new ArrayList<>(methods.size()); @@ -81,7 +84,8 @@ public class BeanMembersShouldSerializeRule extends AbstractJavaRule { for (Map.Entry> entry : vars.entrySet()) { VariableNameDeclaration decl = entry.getKey(); AccessNode accessNodeParent = decl.getAccessNodeParent(); - if (entry.getValue().isEmpty() || accessNodeParent.isTransient() || accessNodeParent.isStatic()) { + if (entry.getValue().isEmpty() || accessNodeParent.isTransient() || accessNodeParent.isStatic() + || classHasLombok || hasIgnoredAnnotation((Annotatable) accessNodeParent)) { continue; } String varName = StringUtils.capitalize(trimIfPrefix(decl.getImage())); diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/BeanMembersShouldSerialize.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/BeanMembersShouldSerialize.xml index 90b4112ad4..983fca10cd 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/BeanMembersShouldSerialize.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/BeanMembersShouldSerialize.xml @@ -185,6 +185,84 @@ public class Foo { private String baz; public void setBaz(String b) { baz = b; } public String getBaz() { return baz; } +} + ]]> + + + #780 [java] BeanMembersShouldSerializeRule does not recognize lombok accessors - 1 + 0 + + + + #780 [java] BeanMembersShouldSerializeRule does not recognize lombok accessors - 2 + 0 + + + + #780 [java] BeanMembersShouldSerializeRule does not recognize lombok accessors - 3 + 0 + + + + #780 [java] BeanMembersShouldSerializeRule does not recognize lombok accessors - 4 + 0 + + + + #780 [java] BeanMembersShouldSerializeRule does not recognize lombok accessors - 5 + 0 + + + + #780 [java] BeanMembersShouldSerializeRule does not recognize lombok accessors - 6 + 0 + + + + #780 [java] BeanMembersShouldSerializeRule does not recognize lombok accessors - 7 + 0 +