From 297b177b3ac6c58cd1d789108f5496fec176f3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 14 Feb 2020 04:02:12 +0100 Subject: [PATCH] Remove ASTClassOrInterfaceBodyDeclaration --- pmd-java/etc/grammar/Java.jjt | 4 ++-- .../lang/java/ast/ASTAnnotationTypeBody.java | 1 - .../ASTAnnotationTypeMemberDeclaration.java | 23 ------------------- .../java/ast/ASTAnyTypeBodyDeclaration.java | 7 +++++- .../lang/java/ast/ASTAnyTypeDeclaration.java | 4 +++- .../pmd/lang/java/ast/ASTBodyDeclaration.java | 14 +++++++++++ .../java/ast/ASTClassOrInterfaceBody.java | 10 +------- .../lang/java/ast/ASTEmptyDeclaration.java | 3 ++- .../pmd/lang/java/ast/ASTEnumBody.java | 9 +++++++- .../pmd/lang/java/ast/ASTEnumConstant.java | 1 + .../lang/java/ast/ASTFieldDeclaration.java | 1 + .../lang/java/ast/ASTImportDeclaration.java | 2 +- .../pmd/lang/java/ast/ASTInitializer.java | 2 +- .../ASTMethodOrConstructorDeclaration.java | 1 + .../lang/java/ast/ASTPackageDeclaration.java | 2 +- .../lang/java/ast/ASTTopLevelDeclaration.java | 12 ++++++++++ .../pmd/lang/java/ast/ASTTypeBody.java | 13 +++++++++++ .../java/ast/AbstractTypeBodyDeclaration.java | 17 -------------- .../java/ast/JavaParserVisitorAdapter.java | 6 +++++ .../lang/java/dfa/VariableAccessVisitor.java | 5 +--- .../metrics/internal/ClassFanOutMetric.java | 10 +------- .../pmd/lang/java/rule/AbstractJavaRule.java | 5 ++++ .../bestpractices/UnusedPrivateFieldRule.java | 14 +++-------- .../java/rule/design/ImmutableFieldRule.java | 9 ++++---- .../pmd/lang/java/symboltable/ClassScope.java | 5 ++-- .../typeresolution/ClassTypeResolverTest.java | 4 ++-- .../lang/java/ast/ASTAnonymousClassTest.kt | 2 +- .../pmd/lang/java/ast/ASTEnumConstantTest.kt | 4 +--- .../pmd/lang/java/ast/NodeParsingCtx.kt | 6 ++--- 29 files changed, 96 insertions(+), 100 deletions(-) delete mode 100644 pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeMemberDeclaration.java create mode 100644 pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBodyDeclaration.java create mode 100644 pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTopLevelDeclaration.java diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index ea55e57d5a..8d7c880a0d 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -943,7 +943,7 @@ void ClassOrInterfaceBody(): "{" ( ClassOrInterfaceBodyDeclaration() )* "}" } -void ClassOrInterfaceBodyDeclaration(): +void ClassOrInterfaceBodyDeclaration() #void: {} { LOOKAHEAD(["static"] "{" ) Initializer() | ModifierList() @@ -2372,7 +2372,7 @@ void AnnotationTypeBody(): "{" ( AnnotationTypeMemberDeclaration() )* "}" } -void AnnotationTypeMemberDeclaration(): +void AnnotationTypeMemberDeclaration() #void: {} { ModifierList() diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeBody.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeBody.java index 71f3e2942b..e28dad8051 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeBody.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeBody.java @@ -15,7 +15,6 @@ public final class ASTAnnotationTypeBody extends AbstractJavaNode implements AST return visitor.visit(this, data); } - @Override public void jjtAccept(SideEffectingVisitor visitor, T data) { visitor.visit(this, data); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeMemberDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeMemberDeclaration.java deleted file mode 100644 index 91c3ed00e0..0000000000 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotationTypeMemberDeclaration.java +++ /dev/null @@ -1,23 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.java.ast; - -public final class ASTAnnotationTypeMemberDeclaration extends AbstractTypeBodyDeclaration { - - ASTAnnotationTypeMemberDeclaration(int id) { - super(id); - } - - @Override - public Object jjtAccept(JavaParserVisitor visitor, Object data) { - return visitor.visit(this, data); - } - - - @Override - public void jjtAccept(SideEffectingVisitor visitor, T data) { - visitor.visit(this, data); - } -} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java index 0c7c677b93..64a94a8329 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java @@ -8,7 +8,10 @@ package net.sourceforge.pmd.lang.java.ast; * Marker interface for type body declarations, such as annotation members, field or method declarations. * * @author Clément Fournier + * + * @deprecated This type and subtypes are removed from the tree */ +@Deprecated public interface ASTAnyTypeBodyDeclaration extends JavaNode { @@ -20,7 +23,9 @@ public interface ASTAnyTypeBodyDeclaration extends JavaNode { *

Returns null if this is an empty declaration, * that is, a single semicolon. */ - JavaNode getDeclarationNode(); + default JavaNode getDeclarationNode() { + return this; + } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java index 0fcf929174..2e700fdb46 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java @@ -25,6 +25,8 @@ public interface ASTAnyTypeDeclaration JavaQualifiableNode, AccessNode, TypeParamOwnerNode, + ASTBodyDeclaration, + ASTTopLevelDeclaration, FinalizableNode { /** @@ -100,7 +102,7 @@ public interface ASTAnyTypeDeclaration * class or annotation. */ default boolean isNested() { - return getParent() instanceof ASTAnyTypeBodyDeclaration; + return getParent() instanceof ASTTypeBody; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBodyDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBodyDeclaration.java new file mode 100644 index 0000000000..f0d3daced5 --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBodyDeclaration.java @@ -0,0 +1,14 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.ast; + +/** + * Marker interface for type body declarations, such as annotation members, + * field or method declarations. + */ +public interface ASTBodyDeclaration extends JavaNode { + + +} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassOrInterfaceBody.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassOrInterfaceBody.java index 068a5fa5c5..7f1ba97867 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassOrInterfaceBody.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassOrInterfaceBody.java @@ -10,7 +10,7 @@ package net.sourceforge.pmd.lang.java.ast; * *

  *
- * ClassOrInterfaceBody ::=  "{"  {@linkplain ASTClassOrInterfaceBodyDeclaration ClassOrInterfaceBodyDeclaration}* "}"
+ * ClassOrInterfaceBody ::=  "{"  {@linkplain ASTBodyDeclaration ClassOrInterfaceBodyDeclaration}* "}"
  *
  * 
*/ @@ -31,12 +31,4 @@ public final class ASTClassOrInterfaceBody extends AbstractJavaNode implements A visitor.visit(this, data); } - - public boolean isAnonymousInnerClass() { - return getParent() instanceof ASTAllocationExpression; - } - - public boolean isEnumChild() { - return getParent() instanceof ASTEnumConstant; - } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyDeclaration.java index d719d32f86..3764a83dfe 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEmptyDeclaration.java @@ -14,7 +14,8 @@ package net.sourceforge.pmd.lang.java.ast; * * */ -public final class ASTEmptyDeclaration extends AbstractJavaNode { +public final class ASTEmptyDeclaration extends AbstractJavaNode + implements ASTBodyDeclaration, ASTTopLevelDeclaration { ASTEmptyDeclaration(int id) { super(id); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumBody.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumBody.java index 177b5728be..ae910a2f97 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumBody.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumBody.java @@ -4,6 +4,8 @@ package net.sourceforge.pmd.lang.java.ast; +import net.sourceforge.pmd.lang.ast.NodeStream; + /** * Body of an {@linkplain ASTEnumDeclaration enum declaration}. * @@ -12,7 +14,7 @@ package net.sourceforge.pmd.lang.java.ast; * EnumBody ::= "{" * [ {@link ASTEnumConstant EnumConstant} ( "," ( {@link ASTEnumConstant EnumConstant} )* ] * [ "," ] - * [ ";" ( {@link ASTClassOrInterfaceBodyDeclaration ClassOrInterfaceBodyDeclaration} )* ] + * [ ";" ( {@link ASTBodyDeclaration ClassOrInterfaceBodyDeclaration} )* ] * "}" * * @@ -39,6 +41,11 @@ public final class ASTEnumBody extends AbstractJavaNode implements ASTTypeBody { visitor.visit(this, data); } + @Override + public NodeStream getEnumConstants() { + return children(ASTEnumConstant.class); + } + void setTrailingComma() { this.trailingComma = true; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumConstant.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumConstant.java index 764f16514e..998ccba10e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumConstant.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumConstant.java @@ -18,6 +18,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; public final class ASTEnumConstant extends AbstractJavaNode implements Annotatable, AccessNode, + ASTBodyDeclaration, InternalInterfaces.VariableIdOwner { ASTEnumConstant(int id) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTFieldDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTFieldDeclaration.java index a82c2f1f6e..0b265ec877 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTFieldDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTFieldDeclaration.java @@ -26,6 +26,7 @@ public final class ASTFieldDeclaration extends AbstractJavaNode Iterable, LeftRecursiveNode, AccessNode, + ASTBodyDeclaration, InternalInterfaces.MultiVariableIdOwner { private JavaFieldSignature signature; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclaration.java index 6b909ba958..a8bb029808 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclaration.java @@ -15,7 +15,7 @@ package net.sourceforge.pmd.lang.java.ast; * * @see JLS 7.5 */ -public final class ASTImportDeclaration extends AbstractJavaNode { +public final class ASTImportDeclaration extends AbstractJavaNode implements ASTTopLevelDeclaration { private boolean isImportOnDemand; private boolean isStatic; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTInitializer.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTInitializer.java index 9756c7f4ce..77f07da05a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTInitializer.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTInitializer.java @@ -14,7 +14,7 @@ package net.sourceforge.pmd.lang.java.ast; * * */ -public final class ASTInitializer extends AbstractJavaNode { +public final class ASTInitializer extends AbstractJavaNode implements ASTBodyDeclaration { private boolean isStatic; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodOrConstructorDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodOrConstructorDeclaration.java index e24eaa4c90..1d75b6357e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodOrConstructorDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodOrConstructorDeclaration.java @@ -30,6 +30,7 @@ public interface ASTMethodOrConstructorDeclaration extends MethodLikeNode, AccessNode, SignedNode, + ASTBodyDeclaration, TypeParamOwnerNode { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPackageDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPackageDeclaration.java index 4ef4fb5184..8b29af0770 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPackageDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPackageDeclaration.java @@ -17,7 +17,7 @@ package net.sourceforge.pmd.lang.java.ast; * * */ -public final class ASTPackageDeclaration extends AbstractJavaNode implements Annotatable { +public final class ASTPackageDeclaration extends AbstractJavaNode implements Annotatable, ASTTopLevelDeclaration { ASTPackageDeclaration(int id) { super(id); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTopLevelDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTopLevelDeclaration.java new file mode 100644 index 0000000000..3f2c46f531 --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTopLevelDeclaration.java @@ -0,0 +1,12 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.ast; + +/** + * Marker interface for nodes that can appear on the top-level of a file. + */ +public interface ASTTopLevelDeclaration extends JavaNode { + +} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeBody.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeBody.java index a766c33281..8946fe2874 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeBody.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeBody.java @@ -4,6 +4,8 @@ package net.sourceforge.pmd.lang.java.ast; +import net.sourceforge.pmd.lang.ast.NodeStream; + /** * Body of a type declaration. * @@ -18,4 +20,15 @@ package net.sourceforge.pmd.lang.java.ast; * @author Clément Fournier */ public interface ASTTypeBody extends JavaNode { + + + default NodeStream getDeclarations() { + return children(ASTBodyDeclaration.class); + } + + + default NodeStream getEnumConstants() { + return NodeStream.empty(); + } + } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeBodyDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeBodyDeclaration.java index 8a7779cf39..8ea89407fd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeBodyDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeBodyDeclaration.java @@ -14,21 +14,4 @@ abstract class AbstractTypeBodyDeclaration extends AbstractJavaNode implements A super(id); } - - @Override - public JavaNode getDeclarationNode() { - if (getNumChildren() == 0) { - return null; - } - - // skips the annotations - AccessNode node = getFirstChildOfType(AccessNode.class); - if (node == null) { - return getFirstChildOfType(ASTInitializer.class); - } - - return node; - } - - } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorAdapter.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorAdapter.java index 8a52ba6ce1..8744e1a6c7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorAdapter.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorAdapter.java @@ -459,6 +459,12 @@ public class JavaParserVisitorAdapter implements JavaParserVisitor { return null; } + + @Deprecated + public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) { + return null; + } + @Deprecated public Object visit(ASTStatementExpression node, Object data) { return null; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/dfa/VariableAccessVisitor.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/dfa/VariableAccessVisitor.java index e2e7cf9825..8879f7e99b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/dfa/VariableAccessVisitor.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/dfa/VariableAccessVisitor.java @@ -17,7 +17,6 @@ import net.sourceforge.pmd.lang.dfa.DataFlowNode; import net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode; import net.sourceforge.pmd.lang.dfa.VariableAccess; import net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTExpression; import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter; @@ -40,9 +39,7 @@ import net.sourceforge.pmd.lang.symboltable.NameOccurrence; public class VariableAccessVisitor extends JavaParserVisitorAdapter { public void compute(ASTMethodDeclaration node) { - if (node.getParent() instanceof ASTClassOrInterfaceBodyDeclaration) { - this.computeNow(node); - } + this.computeNow(node); } public void compute(ASTConstructorDeclaration node) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/internal/ClassFanOutMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/internal/ClassFanOutMetric.java index a745992755..1b170d955b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/internal/ClassFanOutMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/internal/ClassFanOutMetric.java @@ -7,7 +7,6 @@ package net.sourceforge.pmd.lang.java.metrics.internal; import org.apache.commons.lang3.mutable.MutableInt; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.MethodLikeNode; import net.sourceforge.pmd.lang.java.metrics.AbstractJavaClassMetric; import net.sourceforge.pmd.lang.java.metrics.AbstractJavaOperationMetric; @@ -58,14 +57,7 @@ public final class ClassFanOutMetric { @Override public double computeFor(MethodLikeNode node, MetricOptions options) { - MutableInt cfo; - // look at the parent to catch annotations - if (node.getParent() instanceof ASTClassOrInterfaceBodyDeclaration) { - ASTClassOrInterfaceBodyDeclaration parent = (ASTClassOrInterfaceBodyDeclaration) node.getParent(); - cfo = (MutableInt) parent.jjtAccept(new ClassFanOutVisitor(options, node), new MutableInt(0)); - } else { - cfo = (MutableInt) node.jjtAccept(new ClassFanOutVisitor(options, node), new MutableInt(0)); - } + MutableInt cfo = (MutableInt) node.jjtAccept(new ClassFanOutVisitor(options, node), new MutableInt(0)); return (double) cfo.getValue(); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java index 0d95425b52..a62b725ef9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java @@ -19,6 +19,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTAndExpression; import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTArguments; import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement; +import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression; import net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression; @@ -234,4 +235,8 @@ public abstract class AbstractJavaRule extends AbstractRule implements JavaParse return null; } + @Deprecated + public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) { + return null; + } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateFieldRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateFieldRule.java index 3e895727e1..408c35ee74 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateFieldRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateFieldRule.java @@ -10,14 +10,13 @@ import java.util.List; import java.util.Map; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTEnumBody; -import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant; import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTName; import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix; import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix; +import net.sourceforge.pmd.lang.java.ast.ASTTypeBody; import net.sourceforge.pmd.lang.java.ast.AccessNode; import net.sourceforge.pmd.lang.java.ast.Annotatable; import net.sourceforge.pmd.lang.java.ast.JavaNode; @@ -87,15 +86,8 @@ public class UnusedPrivateFieldRule extends AbstractLombokAwareRule { return false; } - private boolean usedInOuter(NameDeclaration decl, JavaNode body) { - List classOrInterfaceBodyDeclarations = body - .findChildrenOfType(ASTClassOrInterfaceBodyDeclaration.class); - List enumConstants = body.findChildrenOfType(ASTEnumConstant.class); - List nodes = new ArrayList<>(); - nodes.addAll(classOrInterfaceBodyDeclarations); - nodes.addAll(enumConstants); - - for (JavaNode node : nodes) { + private boolean usedInOuter(NameDeclaration decl, ASTTypeBody body) { + for (JavaNode node : body.getDeclarations()) { for (ASTPrimarySuffix primarySuffix : node.findDescendantsOfType(ASTPrimarySuffix.class, true)) { if (decl.getImage().equals(primarySuffix.getImage())) { return true; // No violation diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ImmutableFieldRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ImmutableFieldRule.java index 95936b2b45..9243d3527b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ImmutableFieldRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ImmutableFieldRule.java @@ -11,7 +11,6 @@ import java.util.Set; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTDoStatement; @@ -24,6 +23,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer; import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement; import net.sourceforge.pmd.lang.java.ast.AccessNode; import net.sourceforge.pmd.lang.java.ast.Annotatable; +import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.rule.AbstractLombokAwareRule; import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence; import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration; @@ -83,7 +83,7 @@ public class ImmutableFieldRule extends AbstractLombokAwareRule { for (NameOccurrence occ : usages) { JavaNameOccurrence jocc = (JavaNameOccurrence) occ; if (jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) { - Node node = jocc.getLocation(); + JavaNode node = jocc.getLocation(); ASTConstructorDeclaration constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class); if (constructor != null && isSameClass(field, constructor)) { if (inLoopOrTry(node)) { @@ -140,9 +140,8 @@ public class ImmutableFieldRule extends AbstractLombokAwareRule { || node.getFirstParentOfType(ASTDoStatement.class) != null; } - private boolean inAnonymousInnerClass(Node node) { - ASTClassOrInterfaceBodyDeclaration parent = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); - return parent != null && parent.isAnonymousInnerClass(); + private boolean inAnonymousInnerClass(JavaNode node) { + return node.getEnclosingType().isAnonymous(); } private List findAllConstructors(ASTClassOrInterfaceDeclaration node) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java index ef5df40f49..03a392df82 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java @@ -15,8 +15,8 @@ import java.util.Set; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; import net.sourceforge.pmd.lang.java.ast.ASTArgumentList; +import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTExtendsList; @@ -605,8 +605,7 @@ public class ClassScope extends AbstractJavaScope { private Class resolveGenericType(Node argument, String typeImage) { List types = new ArrayList<>(); // first search only within the same method - ASTClassOrInterfaceBodyDeclaration firstParentOfType = argument - .getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); + ASTBodyDeclaration firstParentOfType = argument.getFirstParentOfType(ASTBodyDeclaration.class); if (firstParentOfType != null) { types.addAll(firstParentOfType.findDescendantsOfType(ASTTypeParameter.class)); } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java index cdb77bfdcf..99cc855024 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java @@ -39,8 +39,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; import net.sourceforge.pmd.lang.java.ast.ASTAnonymousClassDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTArgumentList; import net.sourceforge.pmd.lang.java.ast.ASTArrayType; +import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; @@ -278,7 +278,7 @@ public class ClassTypeResolverTest { Node acu = java8.parseClass(NestedAnonymousClass.class); ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class); ASTAllocationExpression nestedAllocation - = allocationExpression.getFirstDescendantOfType(ASTClassOrInterfaceBodyDeclaration.class) // get the declaration (boundary) + = allocationExpression.getFirstDescendantOfType(ASTBodyDeclaration.class) // get the declaration (boundary) .getFirstDescendantOfType(ASTAllocationExpression.class); // and dive for the nested allocation TypeNode child = (TypeNode) nestedAllocation.getChild(0); Assert.assertTrue(Converter.class.isAssignableFrom(child.getType())); diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTAnonymousClassTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTAnonymousClassTest.kt index 7db48c82df..5a53b9ecb1 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTAnonymousClassTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTAnonymousClassTest.kt @@ -46,7 +46,7 @@ class ASTAnonymousClassTest : ParserTestSpec({ val anon = it child { - child(ignoreChildren = true) { + child(ignoreChildren = true) { it::getEnclosingType shouldBe anon } } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTEnumConstantTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTEnumConstantTest.kt index 054ebb103c..0f13861daf 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTEnumConstantTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTEnumConstantTest.kt @@ -114,9 +114,7 @@ class ASTEnumConstantTest : ParserTestSpec({ enumBody { it::hasTrailingComma shouldBe false it::hasSeparatorSemi shouldBe true - child { - child {} - } + child {} } } } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/NodeParsingCtx.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/NodeParsingCtx.kt index 729bdf74e0..762ece0840 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/NodeParsingCtx.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/NodeParsingCtx.kt @@ -96,7 +96,7 @@ object StatementParsingCtx : NodeParsingCtx("statement") { .getFirstDescendantOfType(ASTBlock::class.java).getChild(0) } -object TypeBodyParsingCtx : NodeParsingCtx("body declaration") { +object TypeBodyParsingCtx : NodeParsingCtx("body declaration") { override fun getTemplate(construct: String, ctx: ParserTestCtx): String { val source = ctx.fullSource @@ -111,8 +111,8 @@ $construct """ } - override fun retrieveNode(acu: ASTCompilationUnit): JavaNode = - acu.getFirstDescendantOfType(ASTAnyTypeBodyDeclaration::class.java).declarationNode + override fun retrieveNode(acu: ASTCompilationUnit): ASTBodyDeclaration = + acu.typeDeclarations.first().getFirstDescendantOfType(ASTBodyDeclaration::class.java) } object TopLevelTypeDeclarationParsingCtx : NodeParsingCtx("top-level declaration") {