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()); }