Remove ASTClassOrInterfaceBodyDeclaration

This commit is contained in:
Clément Fournier
2020-02-14 04:02:12 +01:00
parent 9685ba5999
commit 297b177b3a
29 changed files with 96 additions and 100 deletions

View File

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

View File

@ -15,7 +15,6 @@ public final class ASTAnnotationTypeBody extends AbstractJavaNode implements AST
return visitor.visit(this, data);
}
@Override
public <T> void jjtAccept(SideEffectingVisitor<T> visitor, T data) {
visitor.visit(this, data);

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 ASTAnnotationTypeMemberDeclaration extends AbstractTypeBodyDeclaration {
ASTAnnotationTypeMemberDeclaration(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

@ -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 {
* <p>Returns null if this is an empty declaration,
* that is, a single semicolon.
*/
JavaNode getDeclarationNode();
default JavaNode getDeclarationNode() {
return this;
}
}

View File

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

View File

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

View File

@ -10,7 +10,7 @@ package net.sourceforge.pmd.lang.java.ast;
*
* <pre class="grammar">
*
* ClassOrInterfaceBody ::= "{" {@linkplain ASTClassOrInterfaceBodyDeclaration ClassOrInterfaceBodyDeclaration}* "}"
* ClassOrInterfaceBody ::= "{" {@linkplain ASTBodyDeclaration ClassOrInterfaceBodyDeclaration}* "}"
*
* </pre>
*/
@ -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;
}
}

View File

@ -14,7 +14,8 @@ package net.sourceforge.pmd.lang.java.ast;
*
* </pre>
*/
public final class ASTEmptyDeclaration extends AbstractJavaNode {
public final class ASTEmptyDeclaration extends AbstractJavaNode
implements ASTBodyDeclaration, ASTTopLevelDeclaration {
ASTEmptyDeclaration(int id) {
super(id);

View File

@ -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} )* ]
* "}"
*
* </pre>
@ -39,6 +41,11 @@ public final class ASTEnumBody extends AbstractJavaNode implements ASTTypeBody {
visitor.visit(this, data);
}
@Override
public NodeStream<ASTEnumConstant> getEnumConstants() {
return children(ASTEnumConstant.class);
}
void setTrailingComma() {
this.trailingComma = true;
}

View File

@ -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) {

View File

@ -26,6 +26,7 @@ public final class ASTFieldDeclaration extends AbstractJavaNode
Iterable<ASTVariableDeclaratorId>,
LeftRecursiveNode,
AccessNode,
ASTBodyDeclaration,
InternalInterfaces.MultiVariableIdOwner {
private JavaFieldSignature signature;

View File

@ -15,7 +15,7 @@ package net.sourceforge.pmd.lang.java.ast;
*
* @see <a href="https://docs.oracle.com/javase/specs/jls/se9/html/jls-7.html#jls-7.5">JLS 7.5</a>
*/
public final class ASTImportDeclaration extends AbstractJavaNode {
public final class ASTImportDeclaration extends AbstractJavaNode implements ASTTopLevelDeclaration {
private boolean isImportOnDemand;
private boolean isStatic;

View File

@ -14,7 +14,7 @@ package net.sourceforge.pmd.lang.java.ast;
* </pre>
*
*/
public final class ASTInitializer extends AbstractJavaNode {
public final class ASTInitializer extends AbstractJavaNode implements ASTBodyDeclaration {
private boolean isStatic;

View File

@ -30,6 +30,7 @@ public interface ASTMethodOrConstructorDeclaration
extends MethodLikeNode,
AccessNode,
SignedNode<ASTMethodOrConstructorDeclaration>,
ASTBodyDeclaration,
TypeParamOwnerNode {

View File

@ -17,7 +17,7 @@ package net.sourceforge.pmd.lang.java.ast;
* </pre>
*
*/
public final class ASTPackageDeclaration extends AbstractJavaNode implements Annotatable {
public final class ASTPackageDeclaration extends AbstractJavaNode implements Annotatable, ASTTopLevelDeclaration {
ASTPackageDeclaration(int id) {
super(id);

View File

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

View File

@ -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<ASTBodyDeclaration> getDeclarations() {
return children(ASTBodyDeclaration.class);
}
default NodeStream<ASTEnumConstant> getEnumConstants() {
return NodeStream.empty();
}
}

View File

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

View File

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

View File

@ -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) {

View File

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

View File

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

View File

@ -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<ASTClassOrInterfaceBodyDeclaration> classOrInterfaceBodyDeclarations = body
.findChildrenOfType(ASTClassOrInterfaceBodyDeclaration.class);
List<ASTEnumConstant> enumConstants = body.findChildrenOfType(ASTEnumConstant.class);
List<JavaNode> 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

View File

@ -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<ASTConstructorDeclaration> findAllConstructors(ASTClassOrInterfaceDeclaration node) {

View File

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

View File

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

View File

@ -46,7 +46,7 @@ class ASTAnonymousClassTest : ParserTestSpec({
val anon = it
child<ASTClassOrInterfaceBody> {
child<ASTClassOrInterfaceBodyDeclaration>(ignoreChildren = true) {
child<ASTMethodDeclaration>(ignoreChildren = true) {
it::getEnclosingType shouldBe anon
}
}

View File

@ -114,9 +114,7 @@ class ASTEnumConstantTest : ParserTestSpec({
enumBody {
it::hasTrailingComma shouldBe false
it::hasSeparatorSemi shouldBe true
child<ASTClassOrInterfaceBodyDeclaration> {
child<ASTEmptyDeclaration> {}
}
child<ASTEmptyDeclaration> {}
}
}
}

View File

@ -96,7 +96,7 @@ object StatementParsingCtx : NodeParsingCtx<ASTStatement>("statement") {
.getFirstDescendantOfType(ASTBlock::class.java).getChild(0)
}
object TypeBodyParsingCtx : NodeParsingCtx<JavaNode>("body declaration") {
object TypeBodyParsingCtx : NodeParsingCtx<ASTBodyDeclaration>("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<ASTAnyTypeDeclaration>("top-level declaration") {