Remove TypeDeclaration

This commit is contained in:
Clément Fournier
2020-02-14 04:14:15 +01:00
parent 297b177b3a
commit 05e1ba0c10
8 changed files with 18 additions and 70 deletions

View File

@ -855,7 +855,7 @@ void ModifierList():
/*
* Declaration syntax follows.
*/
void TypeDeclaration():
void TypeDeclaration() #void:
{}
{
ModifierList()

View File

@ -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;
}

View File

@ -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<ASTAnyTypeDeclaration> getTypeDeclarations() {
List<ASTTypeDeclaration> tds = findChildrenOfType(ASTTypeDeclaration.class);
return tds.stream().map(it -> (ASTAnyTypeDeclaration) it.getFirstChild()).collect(Collectors.toList());
public NodeStream<ASTAnyTypeDeclaration> getTypeDeclarations() {
return children(ASTAnyTypeDeclaration.class);
}

View File

@ -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 <T> void jjtAccept(SideEffectingVisitor<T> visitor, T data) {
visitor.visit(this, data);
}
}

View File

@ -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<JavaNode> {
: node.getEnclosingType();
if (enclosing == null) {
List<ASTAnyTypeDeclaration> tds = node.getRoot().getTypeDeclarations();
enclosing = tds.stream()
.filter(AccessNode::isPublic)
.findFirst()
.orElseGet(
() -> tds.isEmpty() ? null : tds.get(0)
);
NodeStream<ASTAnyTypeDeclaration> tds = node.getRoot().getTypeDeclarations();
enclosing = tds.first(AccessNode::isPublic);
if (enclosing == null) {
enclosing = tds.first();
}
}
if (enclosing == null) {

View File

@ -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;
}

View File

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

View File

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