diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 7b42758705..a65f7f4eb0 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -838,7 +838,7 @@ void ModifierList(): /* * Declaration syntax follows. */ -void TypeDeclaration(): +void TypeDeclaration() #void: {} { ModifierList() @@ -987,7 +987,7 @@ void ClassOrInterfaceBody(): "{" ( ClassOrInterfaceBodyDeclaration() )* "}" } -void ClassOrInterfaceBodyDeclaration(): +void ClassOrInterfaceBodyDeclaration() #void: {} { LOOKAHEAD(["static"] "{" ) Initializer() | ModifierList() @@ -2411,7 +2411,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..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 @@ -8,19 +8,11 @@ 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 { - /** - * 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. - */ - JavaNode getDeclarationNode(); - - } 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 b98d5920c4..2ec7989a9a 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 @@ -27,6 +27,8 @@ public interface ASTAnyTypeDeclaration JavaQualifiableNode, AccessNode, TypeParamOwnerNode, + ASTBodyDeclaration, + ASTTopLevelDeclaration, FinalizableNode { @Override @@ -130,8 +132,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(); } @@ -154,7 +156,7 @@ public interface ASTAnyTypeDeclaration * class or annotation. */ default boolean isNested() { - return getParent() instanceof ASTAnyTypeBodyDeclaration; + return getParent() instanceof ASTTypeBody; } @@ -174,7 +176,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/ASTBodyDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBodyDeclaration.java new file mode 100644 index 0000000000..885ba1e139 --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBodyDeclaration.java @@ -0,0 +1,28 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.ast; + +/** + * 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/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/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/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..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 @@ -12,7 +12,7 @@ package net.sourceforge.pmd.lang.java.ast; * EnumBody ::= "{" * [ {@link ASTEnumConstant EnumConstant} ( "," ( {@link ASTEnumConstant EnumConstant} )* ] * [ "," ] - * [ ";" ( {@link ASTClassOrInterfaceBodyDeclaration ClassOrInterfaceBodyDeclaration} )* ] + * [ ";" ( {@link ASTBodyDeclaration ClassOrInterfaceBodyDeclaration} )* ] * "}" * * 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 7b30459c18..d9c1fc81b8 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 @@ -31,6 +31,7 @@ public interface ASTMethodOrConstructorDeclaration extends MethodLikeNode, AccessNode, SignedNode, + ASTBodyDeclaration, TypeParamOwnerNode { 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 fff367dde2..b51da15f5d 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. * @@ -241,11 +239,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; @@ -253,9 +247,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/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..6b3ff9a639 --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTopLevelDeclaration.java @@ -0,0 +1,25 @@ +/** + * 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. + * 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/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 1d44cc852e..36aff6f930 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. * @@ -17,4 +19,11 @@ package net.sourceforge.pmd.lang.java.ast; * */ public interface ASTTypeBody extends JavaNode { + + + default NodeStream getDeclarations() { + return children(ASTBodyDeclaration.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/ast/ASTVariableDeclaratorId.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java index ca5ddd0bfe..d8a1308ab6 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 @@ -182,41 +182,6 @@ public final class ASTVariableDeclaratorId extends AbstractTypedSymbolDeclarator 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(); - } else if (isPatternBinding()) { - // implicitly like final, assignment of a pattern binding is not allowed - return true; - } - - 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. */ @@ -237,7 +202,7 @@ public final class ASTVariableDeclaratorId extends AbstractTypedSymbolDeclarator * since the type node is absent. */ public boolean isTypeInferred() { - return isLocalVariableTypeInferred() || isLambdaTypeInferred(); + return getTypeNode() == null; } /** @@ -250,23 +215,6 @@ public final class ASTVariableDeclaratorId extends AbstractTypedSymbolDeclarator } - 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. */ @@ -303,29 +251,8 @@ public final class ASTVariableDeclaratorId extends AbstractTypedSymbolDeclarator */ @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 if (getParent() instanceof ASTTypeTestPattern) { - return ((ASTTypeTestPattern) getParent()).getTypeNode(); - } else if (getParent() instanceof ASTRecordComponent) { - return ((ASTRecordComponent) getParent()).getTypeNode(); - } 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 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 16b5fbfbd2..a2b20dfafa 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 @@ -444,6 +444,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/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/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 8c7512bf5c..63ff42aaaf 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 @@ -18,6 +18,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; import net.sourceforge.pmd.lang.java.ast.ASTAndExpression; 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; @@ -230,4 +231,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/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/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/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 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/rule/internal/AnnotationSuppressionUtil.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/AnnotationSuppressionUtil.java index 91a5be5411..30ebdd4b95 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/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)); 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/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 cdb77bfdcf..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,10 +37,11 @@ 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; 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; @@ -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()); @@ -278,7 +270,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())); @@ -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()); } 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 2ab7282651..2fbab99481 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..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 @@ -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.firstOrThrow().getFirstDescendantOfType(ASTBodyDeclaration::class.java) } object TopLevelTypeDeclarationParsingCtx : NodeParsingCtx("top-level declaration") {