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 1/7] 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") { From 05e1ba0c10124b306ba8c312d3d1ebc0578abc22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 14 Feb 2020 04:14:15 +0100 Subject: [PATCH 2/7] Remove TypeDeclaration --- pmd-java/etc/grammar/Java.jjt | 2 +- .../lang/java/ast/ASTAnyTypeDeclaration.java | 2 +- .../pmd/lang/java/ast/ASTCompilationUnit.java | 7 +++--- .../pmd/lang/java/ast/ASTTypeDeclaration.java | 23 ----------------- .../pmd/lang/java/rule/JavaRuleViolation.java | 14 +++++------ .../internal/AnnotationSuppressionUtil.java | 4 --- .../typeresolution/ClassTypeResolver.java | 11 -------- .../typeresolution/ClassTypeResolverTest.java | 25 ++++++------------- 8 files changed, 18 insertions(+), 70 deletions(-) delete mode 100644 pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeDeclaration.java diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 8d7c880a0d..a0139d4349 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -855,7 +855,7 @@ void ModifierList(): /* * Declaration syntax follows. */ -void TypeDeclaration(): +void TypeDeclaration() #void: {} { ModifierList() 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 2e700fdb46..7fb62e5ae7 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 @@ -122,7 +122,7 @@ public interface ASTAnyTypeDeclaration * Returns true if this type is declared at the top-level of a file. */ default boolean isTopLevel() { - return getParent() instanceof ASTTypeDeclaration; + return getParent() instanceof ASTCompilationUnit; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java index 2509be7f69..c4bb4a49f0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java @@ -7,12 +7,12 @@ package net.sourceforge.pmd.lang.java.ast; import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import net.sourceforge.pmd.annotation.InternalApi; +import net.sourceforge.pmd.lang.ast.NodeStream; import net.sourceforge.pmd.lang.ast.RootNode; import net.sourceforge.pmd.lang.java.typeresolution.ClassTypeResolver; @@ -68,9 +68,8 @@ public final class ASTCompilationUnit extends AbstractJavaTypeNode implements Ro * This may be empty if this a package-info.java, or a modular * compilation unit. */ - public List getTypeDeclarations() { - List tds = findChildrenOfType(ASTTypeDeclaration.class); - return tds.stream().map(it -> (ASTAnyTypeDeclaration) it.getFirstChild()).collect(Collectors.toList()); + public NodeStream getTypeDeclarations() { + return children(ASTAnyTypeDeclaration.class); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeDeclaration.java deleted file mode 100644 index 9fac46abb7..0000000000 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeDeclaration.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 ASTTypeDeclaration extends AbstractJavaTypeNode { - - ASTTypeDeclaration(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/rule/JavaRuleViolation.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/JavaRuleViolation.java index 0d9ea64887..121d7346b8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/JavaRuleViolation.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/JavaRuleViolation.java @@ -5,7 +5,6 @@ package net.sourceforge.pmd.lang.java.rule; import java.util.Iterator; -import java.util.List; import org.checkerframework.checker.nullness.qual.Nullable; @@ -13,6 +12,7 @@ import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.RuleViolation; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.NodeStream; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; @@ -67,14 +67,12 @@ public class JavaRuleViolation extends ParametricRuleViolation { : node.getEnclosingType(); if (enclosing == null) { - List tds = node.getRoot().getTypeDeclarations(); - enclosing = tds.stream() - .filter(AccessNode::isPublic) - .findFirst() - .orElseGet( - () -> tds.isEmpty() ? null : tds.get(0) - ); + NodeStream tds = node.getRoot().getTypeDeclarations(); + enclosing = tds.first(AccessNode::isPublic); + if (enclosing == null) { + enclosing = tds.first(); + } } if (enclosing == null) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java index 5316715051..eb210b9f8b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java @@ -20,7 +20,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter; import net.sourceforge.pmd.lang.java.ast.ASTLiteral; import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; -import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.Annotatable; import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper; @@ -95,9 +94,6 @@ final class AnnotationSuppressionUtil { || node instanceof ASTFieldDeclaration || node instanceof ASTFormalParameter) { return (Annotatable) node; - } else if (node instanceof ASTTypeDeclaration) { - // this is just necessary while we have this node in the tree, it will be removed - return (Annotatable) node.getChild(0); } else { return null; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/ClassTypeResolver.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/ClassTypeResolver.java index 9d60d32ef4..b72f83141e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/ClassTypeResolver.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/ClassTypeResolver.java @@ -70,7 +70,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression; import net.sourceforge.pmd.lang.java.ast.ASTType; import net.sourceforge.pmd.lang.java.ast.ASTTypeArgument; import net.sourceforge.pmd.lang.java.ast.ASTTypeArguments; -import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTTypeParameter; import net.sourceforge.pmd.lang.java.ast.ASTTypeParameters; import net.sourceforge.pmd.lang.java.ast.ASTUnaryExpression; @@ -214,16 +213,6 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter { return data; } - - - @Override - public Object visit(ASTTypeDeclaration node, Object data) { - super.visit(node, data); - rollupTypeUnary(node); - return data; - } - - @Override public Object visit(ASTAnonymousClassDeclaration node, Object data) { populateType(node, node.getQualifiedName().toString()); 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 99cc855024..f607dddd9d 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 @@ -37,6 +37,7 @@ import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.JavaParsingHelper; import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; import net.sourceforge.pmd.lang.java.ast.ASTAnonymousClassDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTArgumentList; import net.sourceforge.pmd.lang.java.ast.ASTArrayType; import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration; @@ -60,7 +61,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTReferenceType; import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression; import net.sourceforge.pmd.lang.java.ast.ASTThisExpression; import net.sourceforge.pmd.lang.java.ast.ASTType; -import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.ast.JavaNode; @@ -164,7 +164,6 @@ public class ClassTypeResolverTest { @Test public void acceptanceTest() { ASTCompilationUnit acu = java5.parseClass(ArrayListFound.class); - assertEquals(ArrayListFound.class, acu.getFirstDescendantOfType(ASTTypeDeclaration.class).getType()); assertEquals(ArrayListFound.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType()); ASTImportDeclaration id = acu.getFirstDescendantOfType(ASTImportDeclaration.class); @@ -214,15 +213,11 @@ public class ClassTypeResolverTest { Class theExtraTopLevelClass = Class .forName("net.sourceforge.pmd.typeresolution.testdata.TheExtraTopLevelClass"); // First class - ASTTypeDeclaration typeDeclaration = (ASTTypeDeclaration) acu.getChild(1); + ASTAnyTypeDeclaration typeDeclaration = (ASTAnyTypeDeclaration) acu.getChild(1); assertEquals(ExtraTopLevelClass.class, typeDeclaration.getType()); - assertEquals(ExtraTopLevelClass.class, - typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType()); // Second class - typeDeclaration = (ASTTypeDeclaration) acu.getChild(2); + typeDeclaration = (ASTAnyTypeDeclaration) acu.getChild(2); assertEquals(theExtraTopLevelClass, typeDeclaration.getType()); - assertEquals(theExtraTopLevelClass, - typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType()); } @@ -231,14 +226,11 @@ public class ClassTypeResolverTest { ASTCompilationUnit acu = java5.parseClass(InnerClass.class); Class theInnerClass = Class.forName("net.sourceforge.pmd.typeresolution.testdata.InnerClass$TheInnerClass"); // Outer class - ASTTypeDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTTypeDeclaration.class); + ASTClassOrInterfaceDeclaration typeDeclaration = acu.getFirstChildOfType(ASTClassOrInterfaceDeclaration.class); assertEquals(InnerClass.class, typeDeclaration.getType()); - ASTClassOrInterfaceDeclaration outerClassDeclaration = typeDeclaration - .getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class); - assertEquals(InnerClass.class, outerClassDeclaration.getType()); // Inner class assertEquals(theInnerClass, - outerClassDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType()); + typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType()); // Method parameter as inner class ASTFormalParameter formalParameter = typeDeclaration.getFirstDescendantOfType(ASTFormalParameter.class); assertEquals(theInnerClass, formalParameter.getType()); @@ -301,14 +293,11 @@ public class ClassTypeResolverTest { Class theAnonymousInnerClass = Class .forName("net.sourceforge.pmd.typeresolution.testdata.AnonymousInnerClass$1"); // Outer class - ASTTypeDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTTypeDeclaration.class); + ASTClassOrInterfaceDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class); assertEquals(AnonymousInnerClass.class, typeDeclaration.getType()); - ASTClassOrInterfaceDeclaration outerClassDeclaration = typeDeclaration - .getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class); - assertEquals(AnonymousInnerClass.class, outerClassDeclaration.getType()); // Anonymous Inner class assertEquals(theAnonymousInnerClass, - outerClassDeclaration.getFirstDescendantOfType(ASTAllocationExpression.class).getType()); + typeDeclaration.getFirstDescendantOfType(ASTAnonymousClassDeclaration.class).getType()); } From 32faa6b3f955c9dfbc48ea5cb8bf29de0bd38b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 14 Feb 2020 04:18:10 +0100 Subject: [PATCH 3/7] Update more code --- .../lang/java/ast/ASTAnyTypeDeclaration.java | 4 ++-- .../pmd/lang/java/ast/ASTEnumBody.java | 7 ------- .../pmd/lang/java/ast/ASTTypeBody.java | 4 ---- .../java/metrics/AbstractJavaClassMetric.java | 6 +----- .../pmd/lang/java/metrics/JavaMetrics.java | 11 +---------- .../codestyle/ClassNamingConventionsRule.java | 18 ++++++++---------- .../rule/design/ExcessivePublicCountRule.java | 2 -- 7 files changed, 12 insertions(+), 40 deletions(-) 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 7fb62e5ae7..913f22231d 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 @@ -78,8 +78,8 @@ public interface ASTAnyTypeDeclaration * * @return The member declarations declared in this type declaration */ - default NodeStream getDeclarations() { - return getBody().children(ASTAnyTypeBodyDeclaration.class); + default NodeStream getDeclarations() { + return getBody().getDeclarations(); } 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 ae910a2f97..c069a8baf4 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,8 +4,6 @@ package net.sourceforge.pmd.lang.java.ast; -import net.sourceforge.pmd.lang.ast.NodeStream; - /** * Body of an {@linkplain ASTEnumDeclaration enum declaration}. * @@ -41,11 +39,6 @@ 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/ASTTypeBody.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeBody.java index 8946fe2874..f0b4322841 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 @@ -27,8 +27,4 @@ public interface ASTTypeBody extends JavaNode { } - default NodeStream getEnumConstants() { - return NodeStream.empty(); - } - } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/AbstractJavaClassMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/AbstractJavaClassMetric.java index 0af0c678a2..9573b9d363 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/AbstractJavaClassMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/AbstractJavaClassMetric.java @@ -7,7 +7,6 @@ package net.sourceforge.pmd.lang.java.metrics; import java.util.List; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; @@ -107,10 +106,7 @@ public abstract class AbstractJavaClassMetric extends AbstractJavaMetric List getDeclarationsOfType(ASTAnyTypeDeclaration node, Class tClass) { - return node.getDeclarations() - .map(ASTAnyTypeBodyDeclaration::getDeclarationNode) - .filterIs(tClass) - .toList(); + return node.getDeclarations().filterIs(tClass).toList(); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/JavaMetrics.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/JavaMetrics.java index 800f732a8b..4a7961c1d3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/JavaMetrics.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/JavaMetrics.java @@ -5,10 +5,8 @@ package net.sourceforge.pmd.lang.java.metrics; -import java.util.ArrayList; import java.util.List; -import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.MethodLikeNode; @@ -140,13 +138,6 @@ public final class JavaMetrics { } public static List findOps(ASTAnyTypeDeclaration node) { - List operations = new ArrayList<>(); - - for (ASTAnyTypeBodyDeclaration decl : node.getDeclarations()) { - if (decl.getDeclarationNode() instanceof ASTMethodOrConstructorDeclaration) { - operations.add((MethodLikeNode) decl.getDeclarationNode()); - } - } - return operations; + return node.getDeclarations().filterIs(MethodLikeNode.class).toList(); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/ClassNamingConventionsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/ClassNamingConventionsRule.java index dc431664cd..d3e07eed2b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/ClassNamingConventionsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/ClassNamingConventionsRule.java @@ -7,15 +7,14 @@ package net.sourceforge.pmd.lang.java.rule.codestyle; import java.util.regex.Pattern; import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration; -import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTInitializer; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.AccessNode; -import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.ast.internal.PrettyPrintingUtil; import net.sourceforge.pmd.properties.PropertyDescriptor; @@ -65,12 +64,11 @@ public class ClassNamingConventionsRule extends AbstractNamingConventionRule it.hasModifiers(PUBLIC)) // filter out constants From b4eaeb6cf07e7bd66f94a22f67370cb3d15810d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 14 Feb 2020 04:28:54 +0100 Subject: [PATCH 4/7] Doc --- .../pmd/lang/java/ast/ASTBodyDeclaration.java | 18 ++++++++++++++++-- .../lang/java/ast/ASTTopLevelDeclaration.java | 13 +++++++++++++ .../pmd/lang/java/ast/NodeParsingCtx.kt | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) 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 index f0d3daced5..885ba1e139 100644 --- 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 @@ -5,8 +5,22 @@ package net.sourceforge.pmd.lang.java.ast; /** - * Marker interface for type body declarations, such as annotation members, - * field or method declarations. + * Marker interface for declarations that can occur in a {@linkplain ASTTypeBody type body}, + * such as field or method declarations. Some of those can also appear on the + * {@linkplain ASTTopLevelDeclaration top-level} of a file. + * + *
+ *
+ * BodyDeclaration ::= {@link ASTAnyTypeDeclaration AnyTypeDeclaration}
+ *                   | {@link ASTMethodDeclaration MethodDeclaration}
+ *                   | {@link ASTConstructorDeclaration ConstructorDeclaration}
+ *                   | {@link ASTInitializer Initializer}
+ *                   | {@link ASTFieldDeclaration FieldDeclaration}
+ *                   | {@link ASTEnumConstant EnumConstant}
+ *                   | {@link ASTEmptyDeclaration EmptyDeclaration}
+ *
+ * 
+ * */ public interface ASTBodyDeclaration extends JavaNode { 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 index 3f2c46f531..6b3ff9a639 100644 --- 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 @@ -6,6 +6,19 @@ package net.sourceforge.pmd.lang.java.ast; /** * Marker interface for nodes that can appear on the top-level of a file. + * In these contexts, they are children of the {@link ASTCompilationUnit CompilationUnit} + * node. Note that both {@link ASTAnyTypeDeclaration AnyTypeDeclaration} + * and {@link ASTEmptyDeclaration EmptyDeclaration} can appear also in + * a {@linkplain ASTTypeBody type body}. + * + *
+ *
+ * BodyDeclaration ::= {@link ASTAnyTypeDeclaration AnyTypeDeclaration}
+ *                   | {@link ASTImportDeclaration ImportDeclaration}
+ *                   | {@link ASTPackageDeclaration PackageDeclaration}
+ *                   | {@link ASTEmptyDeclaration EmptyDeclaration}
+ *
+ * 
*/ public interface ASTTopLevelDeclaration extends JavaNode { 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 762ece0840..05f152a64f 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 @@ -112,7 +112,7 @@ $construct } override fun retrieveNode(acu: ASTCompilationUnit): ASTBodyDeclaration = - acu.typeDeclarations.first().getFirstDescendantOfType(ASTBodyDeclaration::class.java) + acu.typeDeclarations.firstOrThrow().getFirstDescendantOfType(ASTBodyDeclaration::class.java) } object TopLevelTypeDeclarationParsingCtx : NodeParsingCtx("top-level declaration") { From 53cc2d0f287fccfa304be4a62b5ecd5f1e2ac5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Wed, 19 Feb 2020 21:53:31 +0100 Subject: [PATCH 5/7] Simplify VariableDeclaratorId --- .../java/ast/ASTVariableDeclaratorId.java | 73 +------------------ 1 file changed, 3 insertions(+), 70 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java index 948cc151d3..e0c0f30439 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java @@ -170,38 +170,6 @@ public final class ASTVariableDeclaratorId extends AbstractJavaTypeNode implemen return getImage(); } - - /** - * Returns true if the variable declared by this node is declared final. - * Doesn't account for the "effectively-final" nuance. Resource - * declarations are implicitly final. - */ - @Override - public boolean isFinal() { - if (isResourceDeclaration()) { - // this is implicit even if "final" is not explicitly declared. - return true; - } else if (getParent() instanceof ASTLambdaParameter) { - return ((ASTLambdaParameter) getParent()).isFinal(); - } - - if (getParent() instanceof ASTFormalParameter) { - // This accounts for exception parameters too for now - return ((ASTFormalParameter) getParent()).isFinal(); - } - - Node grandpa = getNthParent(2); - - if (grandpa instanceof ASTLocalVariableDeclaration) { - return ((ASTLocalVariableDeclaration) grandpa).isFinal(); - } else if (grandpa instanceof ASTFieldDeclaration) { - return ((ASTFieldDeclaration) grandpa).isFinal(); - } - - throw new IllegalStateException("All cases should be handled"); - } - - /** * Returns true if this declarator id declares a resource in a try-with-resources statement. */ @@ -222,27 +190,9 @@ public final class ASTVariableDeclaratorId extends AbstractJavaTypeNode implemen * since the type node is absent. */ public boolean isTypeInferred() { - return isLocalVariableTypeInferred() || isLambdaTypeInferred(); + return getTypeNode() == null; } - - private boolean isLocalVariableTypeInferred() { - if (isResourceDeclaration()) { - // covers "var" in try-with-resources - return getParent().getFirstChildOfType(ASTType.class) == null; - } else if (getNthParent(2) instanceof ASTLocalVariableDeclaration) { - // covers "var" as local variables and in for statements - return getNthParent(2).getFirstChildOfType(ASTType.class) == null; - } - - return false; - } - - private boolean isLambdaTypeInferred() { - return getParent() instanceof ASTLambdaParameter && ((ASTLambdaParameter) getParent()).isTypeInferred(); - } - - /** * Returns the initializer of the variable, or null if it doesn't exist. */ @@ -279,25 +229,8 @@ public final class ASTVariableDeclaratorId extends AbstractJavaTypeNode implemen */ @Nullable public ASTType getTypeNode() { - Node parent = getParent(); - if (parent instanceof ASTFormalParameter) { - // ASTResource is a subclass of ASTFormal parameter for now but this will change - // and this will need to be corrected here, see #998 - return ((ASTFormalParameter) parent).getTypeNode(); - } else if (parent instanceof ASTCatchParameter) { - return ((ASTCatchParameter) parent).getTypeNode(); - } else if (parent instanceof ASTLambdaParameter) { - return ((ASTLambdaParameter) parent).getTypeNode(); - } else if (isTypeInferred()) { - // lambda expression with lax types. The type is inferred... - return null; - } else { - Node n = parent.getParent(); - if (n instanceof ASTLocalVariableDeclaration || n instanceof ASTFieldDeclaration) { - return n.getFirstChildOfType(ASTType.class); - } - } - return null; + AccessNode parent = getModifierOwnerParent(); + return parent.children(ASTType.class).first(); } // @formatter:off From e32589e010ab2b405c605bca955c7918193eb5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 21 Feb 2020 14:56:15 +0100 Subject: [PATCH 6/7] Delete old method --- .../lang/java/ast/ASTAnyTypeBodyDeclaration.java | 13 ------------- 1 file changed, 13 deletions(-) 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 64a94a8329..ad913672e0 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 @@ -15,17 +15,4 @@ package net.sourceforge.pmd.lang.java.ast; public interface ASTAnyTypeBodyDeclaration extends JavaNode { - /** - * Returns the child of this declaration, - * which can be cast to a more specific node - * type using #getKind() as a cue. - * - *

Returns null if this is an empty declaration, - * that is, a single semicolon. - */ - default JavaNode getDeclarationNode() { - return this; - } - - } From b3aad20fca54622383a17e9308ff77076b046959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 21 Feb 2020 14:59:08 +0100 Subject: [PATCH 7/7] Fix merge --- .../pmd/lang/java/ast/ASTModifierList.java | 13 +++---------- .../java/symbols/internal/impl/ast/AstClassSym.java | 7 ++----- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTModifierList.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTModifierList.java index c5f826ff26..5b2f272b9a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTModifierList.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTModifierList.java @@ -16,8 +16,6 @@ import java.util.Collections; import java.util.EnumSet; import java.util.Set; -import net.sourceforge.pmd.lang.ast.NodeStream; - /** * List of modifiers of a declaration. * @@ -227,11 +225,7 @@ public final class ASTModifierList extends AbstractJavaNode { @Override public void visit(ASTAnonymousClassDeclaration node, Set effective) { - JavaNode enclosing = NodeStream.filterIsAny(node.ancestors(), - ASTAnyTypeDeclaration.class, - ASTEnumConstant.class, - ASTAnyTypeBodyDeclaration.class) - .first(); + ASTBodyDeclaration enclosing = node.ancestors(ASTBodyDeclaration.class).first(); assert enclosing != null && !(enclosing instanceof ASTAnyTypeDeclaration) : "Weird position for an anonymous class " + enclosing; @@ -239,9 +233,8 @@ public final class ASTModifierList extends AbstractJavaNode { if (enclosing instanceof ASTEnumConstant) { effective.add(STATIC); } else { - JavaNode decl = ((ASTAnyTypeBodyDeclaration) enclosing).getDeclarationNode(); - if (decl instanceof AccessNode && ((AccessNode) decl).hasModifiers(STATIC) - || decl instanceof ASTInitializer && ((ASTInitializer) decl).isStatic()) { + if (enclosing instanceof AccessNode && ((AccessNode) enclosing).hasModifiers(STATIC) + || enclosing instanceof ASTInitializer && ((ASTInitializer) enclosing).isStatic()) { effective.add(STATIC); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/impl/ast/AstClassSym.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/impl/ast/AstClassSym.java index 11d737c977..517687f962 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/impl/ast/AstClassSym.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/impl/ast/AstClassSym.java @@ -11,15 +11,14 @@ import java.util.List; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; -import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.symbols.JClassSymbol; import net.sourceforge.pmd.lang.java.symbols.JConstructorSymbol; import net.sourceforge.pmd.lang.java.symbols.JExecutableSymbol; @@ -59,9 +58,7 @@ final class AstClassSym node.getEnumConstants().forEach(constant -> myFields.add(new AstFieldSym(constant.getVarId(), factory, this))); } - for (ASTAnyTypeBodyDeclaration decl : node.getDeclarations()) { - - JavaNode dnode = decl.getDeclarationNode(); + for (ASTBodyDeclaration dnode : node.getDeclarations()) { if (dnode instanceof ASTAnyTypeDeclaration) { myClasses.add(new AstClassSym((ASTAnyTypeDeclaration) dnode, factory, this));