diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 790da4170c..675db4e96c 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -409,8 +409,8 @@ class JavaParserImpl { JavaccToken nonToken = getToken(1); JavaccToken minusToken = getToken(2); JavaccToken sealedToken = getToken(3); - return nonToken.getEndColumn() == minusToken.getBeginColumn() - && minusToken.getEndColumn() == sealedToken.getBeginColumn(); + return nonToken.getReportLocation().getEndColumn() == minusToken.getReportLocation().getStartColumn() + && minusToken.getReportLocation().getEndColumn() == sealedToken.getReportLocation().getStartColumn(); } return false; } @@ -1711,7 +1711,7 @@ void ClassOrInterfaceType() #void: Node first = jjtree.peekNode(); if (first instanceof ASTClassType) { // then we saw type arguments, so the last segment is definitely a type name - ASTAmbiguousName name = first.getFirstChildOfType(ASTAmbiguousName.class); + ASTAmbiguousName name = first.firstChild(ASTAmbiguousName.class); name.shrinkOrDeleteInParentSetImage(); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCastExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCastExpression.java index f7bac6965d..1c4ccf07e6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCastExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCastExpression.java @@ -21,7 +21,7 @@ public final class ASTCastExpression extends AbstractJavaExpr implements ASTExpr } public ASTType getCastType() { - return getFirstChildOfType(ASTType.class); + return firstChild(ASTType.class); } public ASTExpression getOperand() { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCatchClause.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCatchClause.java index 675c701a24..e9f6b2f9c5 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCatchClause.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCatchClause.java @@ -32,7 +32,7 @@ public final class ASTCatchClause extends AbstractJavaNode { /** Returns the body of this catch branch. */ public ASTBlock getBody() { - return getFirstChildOfType(ASTBlock.class); + return firstChild(ASTBlock.class); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassDeclaration.java index 90e4fb7729..b8359df585 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassDeclaration.java @@ -80,7 +80,7 @@ public final class ASTClassDeclaration extends AbstractTypeDeclaration { return null; } - ASTExtendsList extendsList = getFirstChildOfType(ASTExtendsList.class); + ASTExtendsList extendsList = firstChild(ASTExtendsList.class); return extendsList == null ? null : extendsList.iterator().next(); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassType.java index ef515e182f..232828f725 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassType.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassType.java @@ -142,7 +142,7 @@ public final class ASTClassType extends AbstractJavaTypeNode implements ASTRefer */ @Nullable public ASTClassType getQualifier() { - return getFirstChildOfType(ASTClassType.class); + return firstChild(ASTClassType.class); } /** @@ -150,7 +150,7 @@ public final class ASTClassType extends AbstractJavaTypeNode implements ASTRefer */ @Nullable public ASTTypeArguments getTypeArguments() { - return getFirstChildOfType(ASTTypeArguments.class); + return firstChild(ASTTypeArguments.class); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompactConstructorDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompactConstructorDeclaration.java index d710452e69..04d2bf3677 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompactConstructorDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompactConstructorDeclaration.java @@ -36,7 +36,7 @@ public final class ASTCompactConstructorDeclaration extends AbstractJavaNode imp } public ASTBlock getBody() { - return getFirstChildOfType(ASTBlock.class); + return firstChild(ASTBlock.class); } public ASTCompactConstructorDeclaration getDeclarationNode() { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorCall.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorCall.java index 8474d7a48a..3258603e23 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorCall.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTConstructorCall.java @@ -63,7 +63,7 @@ public final class ASTConstructorCall extends AbstractInvocationExpr @Override public @Nullable ASTTypeArguments getExplicitTypeArguments() { - return getFirstChildOfType(ASTTypeArguments.class); + return firstChild(ASTTypeArguments.class); } @@ -87,24 +87,24 @@ public final class ASTConstructorCall extends AbstractInvocationExpr * Returns the type node. */ public ASTClassType getTypeNode() { - return getFirstChildOfType(ASTClassType.class); + return firstChild(ASTClassType.class); } /** * Returns true if this expression defines a body, - * which is compiled to an anonymous class. If this + * which is compiled to an anonymous class. Otherwise, this * method returns false. */ public boolean isAnonymousClass() { - return getChild(getNumChildren() - 1) instanceof ASTAnonymousClassDeclaration; + return getLastChild() instanceof ASTAnonymousClassDeclaration; } @Nullable public ASTAnonymousClassDeclaration getAnonymousClassDeclaration() { return isAnonymousClass() - ? (ASTAnonymousClassDeclaration) getChild(getNumChildren() - 1) + ? (ASTAnonymousClassDeclaration) getLastChild() : null; } } 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 98ecf1a42e..ad5817d7f3 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 @@ -46,7 +46,7 @@ public final class ASTEnumConstant extends AbstractJavaTypeNode @Override public ASTVariableId getVarId() { - return getFirstChildOfType(ASTVariableId.class); + return firstChild(ASTVariableId.class); } @Override @@ -57,7 +57,7 @@ public final class ASTEnumConstant extends AbstractJavaTypeNode @Override @Nullable public ASTArgumentList getArguments() { - return getFirstChildOfType(ASTArgumentList.class); + return firstChild(ASTArgumentList.class); } /** diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExecutableDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExecutableDeclaration.java index 359055b7c0..6b0775fca1 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExecutableDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExecutableDeclaration.java @@ -75,7 +75,7 @@ public interface ASTExecutableDeclaration */ @NonNull default ASTFormalParameters getFormalParameters() { - return getFirstChildOfType(ASTFormalParameters.class); + return firstChild(ASTFormalParameters.class); } /** @@ -103,7 +103,7 @@ public interface ASTExecutableDeclaration */ @Nullable default ASTThrowsList getThrowsList() { - return getFirstChildOfType(ASTThrowsList.class); + return firstChild(ASTThrowsList.class); } /** diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExplicitConstructorInvocation.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExplicitConstructorInvocation.java index ad43aa3464..ea3a24c77d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExplicitConstructorInvocation.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExplicitConstructorInvocation.java @@ -87,7 +87,7 @@ public final class ASTExplicitConstructorInvocation extends AbstractJavaTypeNode @Override @Nullable public ASTTypeArguments getExplicitTypeArguments() { - return getFirstChildOfType(ASTTypeArguments.class); + return firstChild(ASTTypeArguments.class); } /** 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 b98bcc40dc..8f0fbc77d7 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 @@ -70,7 +70,7 @@ public final class ASTFieldDeclaration extends AbstractJavaNode */ @Override public ASTType getTypeNode() { - return getFirstChildOfType(ASTType.class); + return firstChild(ASTType.class); } /** diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTForStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTForStatement.java index 007115705c..e2c744efef 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTForStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTForStatement.java @@ -31,7 +31,7 @@ public final class ASTForStatement extends AbstractStatement implements ASTLoopS @Override public ASTExpression getCondition() { - return getFirstChildOfType(ASTExpression.class); + return firstChild(ASTExpression.class); } /** diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTForeachStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTForeachStatement.java index 82f736e4c5..65734e85f6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTForeachStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTForeachStatement.java @@ -45,7 +45,7 @@ public final class ASTForeachStatement extends AbstractStatement implements Inte */ @NonNull public ASTExpression getIterableExpr() { - return getFirstChildOfType(ASTExpression.class); + return firstChild(ASTExpression.class); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLambdaParameter.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLambdaParameter.java index 81ebaec91f..e20ea447a1 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLambdaParameter.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLambdaParameter.java @@ -53,13 +53,13 @@ public final class ASTLambdaParameter extends AbstractJavaTypeNode @Override @NonNull public ASTVariableId getVarId() { - return getFirstChildOfType(ASTVariableId.class); + return firstChild(ASTVariableId.class); } /** Returns the type node of this formal parameter. */ @Nullable public ASTType getTypeNode() { - return getFirstChildOfType(ASTType.class); + return firstChild(ASTType.class); } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLocalVariableDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLocalVariableDeclaration.java index e985e13c2a..a1cac8ac1a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLocalVariableDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLocalVariableDeclaration.java @@ -73,7 +73,7 @@ public final class ASTLocalVariableDeclaration extends AbstractJavaNode */ @Override public ASTType getTypeNode() { - return getFirstChildOfType(ASTType.class); + return firstChild(ASTType.class); } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodCall.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodCall.java index bd06c74068..abde828d20 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodCall.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodCall.java @@ -67,7 +67,7 @@ public final class ASTMethodCall extends AbstractInvocationExpr @Override @Nullable public ASTTypeArguments getExplicitTypeArguments() { - return getFirstChildOfType(ASTTypeArguments.class); + return firstChild(ASTTypeArguments.class); } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodReference.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodReference.java index d712cf33e6..dd5481df67 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodReference.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodReference.java @@ -87,7 +87,7 @@ public final class ASTMethodReference extends AbstractJavaExpr * the {@linkplain #getQualifier() lhs type}. */ public @Nullable ASTTypeArguments getExplicitTypeArguments() { - return getFirstChildOfType(ASTTypeArguments.class); + return firstChild(ASTTypeArguments.class); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponent.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponent.java index 31b93faa0b..faf4d77d1e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponent.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponent.java @@ -56,11 +56,11 @@ public final class ASTRecordComponent extends AbstractJavaNode implements Modifi } public ASTType getTypeNode() { - return getFirstChildOfType(ASTType.class); + return firstChild(ASTType.class); } @Override public ASTVariableId getVarId() { - return getFirstChildOfType(ASTVariableId.class); + return firstChild(ASTVariableId.class); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordDeclaration.java index c26f3bcd40..8b200646fa 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordDeclaration.java @@ -40,12 +40,12 @@ public final class ASTRecordDeclaration extends AbstractTypeDeclaration { @Override public NodeStream getDeclarations() { - return getFirstChildOfType(ASTRecordBody.class).children(ASTBodyDeclaration.class); + return firstChild(ASTRecordBody.class).children(ASTBodyDeclaration.class); } @Override @NonNull public ASTRecordComponentList getRecordComponents() { - return getFirstChildOfType(ASTRecordComponentList.class); + return firstChild(ASTRecordComponentList.class); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordPattern.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordPattern.java index 87419405aa..ee9da2fb40 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordPattern.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordPattern.java @@ -38,12 +38,12 @@ public final class ASTRecordPattern extends AbstractJavaNode implements ASTPatte * Gets the type against which the expression is tested. */ public ASTReferenceType getTypeNode() { - return getFirstChildOfType(ASTReferenceType.class); + return firstChild(ASTReferenceType.class); } /** Returns the declared variable. */ public ASTVariableId getVarId() { - return getFirstChildOfType(ASTVariableId.class); + return firstChild(ASTVariableId.class); } void bumpParenDepth() { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTryStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTryStatement.java index e385c9a809..b2c40a9910 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTryStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTryStatement.java @@ -76,7 +76,7 @@ public final class ASTTryStatement extends AbstractStatement { */ @Nullable public ASTFinallyClause getFinallyClause() { - return getFirstChildOfType(ASTFinallyClause.class); + return firstChild(ASTFinallyClause.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 index 4ed6663ac0..0d37452459 100644 --- 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 @@ -155,7 +155,7 @@ public interface ASTTypeDeclaration * an enum declaration, returns an empty stream. */ default NodeStream getEnumConstants() { - return getFirstChildOfType(ASTEnumBody.class).children(ASTEnumConstant.class); + return firstChild(ASTEnumBody.class).children(ASTEnumConstant.class); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeParameter.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeParameter.java index 52857d7996..6c67feedde 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeParameter.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeParameter.java @@ -56,7 +56,7 @@ public final class ASTTypeParameter extends AbstractTypedSymbolDeclarator { * {@linkplain ASTTypeDeclaration TypeDeclaration}s. */ default ASTTypeDeclaration getEnclosingType() { - return getFirstParentOfType(ASTTypeDeclaration.class); + return ancestors(ASTTypeDeclaration.class).first(); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidBranchingStatementAsLastInLoopRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidBranchingStatementAsLastInLoopRule.java index b2a8980bd0..b5b10de9aa 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidBranchingStatementAsLastInLoopRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidBranchingStatementAsLastInLoopRule.java @@ -79,7 +79,7 @@ public class AvoidBranchingStatementAsLastInLoopRule extends AbstractJavaRulecha if (parent instanceof ASTFinallyClause) { // get the parent of the block, in which the try statement is: ForStatement/Block/TryStatement/Finally // e.g. a ForStatement - parent = parent.ancestors().get(2); + parent = ((ASTFinallyClause) parent).getNthParent(3); } } if (parent instanceof ASTForStatement || parent instanceof ASTForeachStatement) { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTVariableIdTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTVariableIdTest.java index f7c8b4e15e..41269475ef 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTVariableIdTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTVariableIdTest.java @@ -19,14 +19,14 @@ class ASTVariableIdTest extends BaseParserTest { @Test void testIsExceptionBlockParameter() { ASTCompilationUnit acu = java.parse(EXCEPTION_PARAMETER); - ASTVariableId id = acu.getFirstDescendantOfType(ASTVariableId.class); + ASTVariableId id = acu.descendants(ASTVariableId.class).first(); assertTrue(id.isExceptionBlockParameter()); } @Test void testTypeNameNode() { ASTCompilationUnit acu = java.parse(TYPE_NAME_NODE); - ASTVariableId id = acu.findDescendantsOfType(ASTVariableId.class).get(0); + ASTVariableId id = acu.descendants(ASTVariableId.class).first(); ASTClassType name = (ASTClassType) id.getTypeNameNode(); assertEquals("String", name.getSimpleName()); @@ -35,7 +35,7 @@ class ASTVariableIdTest extends BaseParserTest { @Test void testAnnotations() { ASTCompilationUnit acu = java.parse(TEST_ANNOTATIONS); - ASTVariableId id = acu.findDescendantsOfType(ASTVariableId.class).get(0); + ASTVariableId id = acu.descendants(ASTVariableId.class).first(); ASTClassType name = (ASTClassType) id.getTypeNode(); assertEquals("String", name.getSimpleName()); @@ -44,16 +44,16 @@ class ASTVariableIdTest extends BaseParserTest { @Test void testLambdaWithType() throws Exception { ASTCompilationUnit acu = java8.parse(TEST_LAMBDA_WITH_TYPE); - ASTLambdaExpression lambda = acu.getFirstDescendantOfType(ASTLambdaExpression.class); - ASTVariableId f = lambda.getFirstDescendantOfType(ASTVariableId.class); + ASTLambdaExpression lambda = acu.descendants(ASTLambdaExpression.class).first(); + ASTVariableId f = lambda.descendants(ASTVariableId.class).first(); assertEquals("File", PrettyPrintingUtil.prettyPrintType(f.getTypeNode())); } @Test void testLambdaWithoutType() throws Exception { ASTCompilationUnit acu = java8.parse(TEST_LAMBDA_WITHOUT_TYPE); - ASTLambdaExpression lambda = acu.getFirstDescendantOfType(ASTLambdaExpression.class); - ASTVariableId f = lambda.getFirstDescendantOfType(ASTVariableId.class); + ASTLambdaExpression lambda = acu.descendants(ASTLambdaExpression.class).first(); + ASTVariableId f = lambda.descendants(ASTVariableId.class).first(); assertNull(f.getTypeNode()); } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java index 7bf3afb56b..45cb62715a 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java @@ -300,7 +300,7 @@ class JDKVersionTest extends BaseJavaTreeDumpTest { @Test void jdk7PrivateMethodInnerClassInterface1() { ASTCompilationUnit acu = java7.parseResource("private_method_in_inner_class_interface1.java"); - List methods = acu.findDescendantsOfType(ASTMethodDeclaration.class, true); + List methods = acu.descendants(ASTMethodDeclaration.class).crossFindBoundaries().toList(); assertEquals(3, methods.size()); for (ASTMethodDeclaration method : methods) { assertFalse(method.getEnclosingType().isInterface()); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java10Test.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java10Test.java index 4b9637ad6c..04bc2fa574 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java10Test.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java10Test.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.ast; import static net.sourceforge.pmd.util.CollectionUtil.listOf; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -36,13 +37,14 @@ class Java10Test { // note, it can be parsed, but we'll have a ReferenceType of "var" List localVars = java9.parseResource("LocalVariableTypeInference.java") - .findDescendantsOfType(ASTLocalVariableDeclaration.class); + .descendants(ASTLocalVariableDeclaration.class) + .toList(); assertEquals(3, localVars.size()); ASTVariableId varId = localVars.get(0).getVarIds().firstOrThrow(); // first: var list = new ArrayList(); - assertTrue(varId.getTypeNode() instanceof ASTClassType); + assertInstanceOf(ASTClassType.class, varId.getTypeNode()); // in that case, we don't have a class named "var", so the type will be null assertTrue(varId.getTypeMirror().getSymbol().isUnresolved()); @@ -54,7 +56,7 @@ class Java10Test { @Test void testLocalVarInferenceCanBeParsedJava10() { ASTCompilationUnit compilationUnit = java10.parseResource("LocalVariableTypeInference.java"); - List localVars = compilationUnit.findDescendantsOfType(ASTLocalVariableDeclaration.class); + List localVars = compilationUnit.descendants(ASTLocalVariableDeclaration.class).toList(); assertEquals(3, localVars.size()); TypeSystem ts = compilationUnit.getTypeSystem(); @@ -81,7 +83,8 @@ class Java10Test { @Test void testForLoopWithVar() { List localVars = java10.parseResource("LocalVariableTypeInferenceForLoop.java") - .findDescendantsOfType(ASTLocalVariableDeclaration.class); + .descendants(ASTLocalVariableDeclaration.class) + .toList(); assertEquals(1, localVars.size()); assertNull(localVars.get(0).getTypeNode()); @@ -92,7 +95,8 @@ class Java10Test { @Test void testForLoopEnhancedWithVar() { List localVars = java10.parseResource("LocalVariableTypeInferenceForLoopEnhanced.java") - .findDescendantsOfType(ASTLocalVariableDeclaration.class); + .descendants(ASTLocalVariableDeclaration.class) + .toList(); assertEquals(1, localVars.size()); assertNull(localVars.get(0).getTypeNode()); @@ -103,7 +107,8 @@ class Java10Test { @Test void testForLoopEnhancedWithVar2() { List localVars = java10.parseResource("LocalVariableTypeInferenceForLoopEnhanced2.java") - .findDescendantsOfType(ASTLocalVariableDeclaration.class); + .descendants(ASTLocalVariableDeclaration.class) + .toList(); assertEquals(4, localVars.size()); assertNull(localVars.get(1).getTypeNode()); @@ -118,7 +123,8 @@ class Java10Test { @Test void testTryWithResourcesWithVar() { List resources = java10.parseResource("LocalVariableTypeInferenceTryWithResources.java") - .findDescendantsOfType(ASTResource.class); + .descendants(ASTResource.class) + .toList(); assertEquals(1, resources.size()); assertNull(resources.get(0).asLocalVariableDeclaration().getTypeNode()); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JavaQualifiedNameTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JavaQualifiedNameTest.java index b95e06c0dd..8131dc68e2 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JavaQualifiedNameTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JavaQualifiedNameTest.java @@ -55,7 +55,7 @@ class JavaQualifiedNameTest { List nodes = getNodes(ASTClassDeclaration.class, TEST); for (ASTClassDeclaration coid : nodes) { - if ("Foo".equals(coid.getImage())) { + if ("Foo".equals(coid.getSimpleName())) { assertEquals("foo.bar.Bzaz$Bor$Foo", coid.getBinaryName()); } } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/types/TypeTestUtilTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/types/TypeTestUtilTest.java index af0994573a..520962b291 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/types/TypeTestUtilTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/types/TypeTestUtilTest.java @@ -69,7 +69,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTEnumDeclaration klass = java.parse("package org; " + "enum FooBar implements Iterable {}") - .getFirstDescendantOfType(ASTEnumDeclaration.class); + .descendants(ASTEnumDeclaration.class).first(); assertTrue(TypeTestUtil.isA("org.FooBar", klass)); @@ -86,7 +86,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTType arrayT = java.parse("import java.io.ObjectStreamField; " + "class Foo { private static final ObjectStreamField[] serialPersistentFields; }") - .getFirstDescendantOfType(ASTType.class); + .descendants(ASTType.class).first(); assertIsExactlyA(arrayT, ObjectStreamField[].class); @@ -101,7 +101,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTType arrayT = java.parse("class Foo { org.junit.Test field; }") - .getFirstDescendantOfType(ASTType.class); + .descendants(ASTType.class).first(); assertIsExactlyA(arrayT, org.junit.Test.class); @@ -115,7 +115,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTType arrayT = java.parse("import java.io.ObjectStreamField; " + "class Foo { private static final int[] serialPersistentFields; }") - .getFirstDescendantOfType(ASTType.class); + .descendants(ASTType.class).first(); assertIsExactlyA(arrayT, int[].class); @@ -132,7 +132,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTType arrayT = java.parse("import java.io.ObjectStreamField; " + "class Foo { private static final int serialPersistentFields; }") - .getFirstDescendantOfType(ASTType.class); + .descendants(ASTType.class).first(); assertIsExactlyA(arrayT, int.class); @@ -149,7 +149,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTAnnotationTypeDeclaration klass = java.parse("package org; import foo.Stuff;" + "public @interface FooBar {}") - .getFirstDescendantOfType(ASTAnnotationTypeDeclaration.class); + .descendants(ASTAnnotationTypeDeclaration.class).first(); assertTrue(TypeTestUtil.isA("org.FooBar", klass)); @@ -179,7 +179,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTTypeDeclaration klass = java.parse("package org;" + "public class FooBar {}") - .getFirstDescendantOfType(ASTTypeDeclaration.class); + .descendants(ASTTypeDeclaration.class).first(); assertThrows(IllegalArgumentException.class, @@ -192,7 +192,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTTypeDeclaration klass = java.parse("package org;" + "public @interface FooBar {}") - .getFirstDescendantOfType(ASTTypeDeclaration.class); + .descendants(ASTTypeDeclaration.class).first(); assertThrows(IllegalArgumentException.class, () -> @@ -205,7 +205,7 @@ class TypeTestUtilTest extends BaseParserTest { ASTAnonymousClassDeclaration anon = java.parseClass(SomeClassWithAnon.class) - .getFirstDescendantOfType(ASTAnonymousClassDeclaration.class); + .descendants(ASTAnonymousClassDeclaration.class).first(); assertTrue(anon.getSymbol().isAnonymousClass(), "Anon class"); @@ -231,7 +231,7 @@ class TypeTestUtilTest extends BaseParserTest { @Test void testIsAFallbackAnnotationSimpleNameImport() { ASTAnnotation annotation = java.parse("package org; import foo.Stuff; @Stuff public class FooBar {}") - .getFirstDescendantOfType(ASTAnnotation.class); + .descendants(ASTAnnotation.class).first(); assertTrue(TypeTestUtil.isA("foo.Stuff", annotation)); assertFalse(TypeTestUtil.isA("other.Stuff", annotation)); @@ -251,7 +251,7 @@ class TypeTestUtilTest extends BaseParserTest { @Test void testNullClass() { final ASTAnnotation node = java.parse("package org; import foo.Stuff; @Stuff public class FooBar {}") - .getFirstDescendantOfType(ASTAnnotation.class); + .descendants(ASTAnnotation.class).first(); assertNotNull(node); assertThrows(NullPointerException.class, () -> TypeTestUtil.isA((String) null, node)); diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt index 17bdeae73a..b6f0c65ff0 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt @@ -19,6 +19,7 @@ import net.sourceforge.pmd.lang.java.JavaParsingHelper import net.sourceforge.pmd.lang.java.JavaParsingHelper.* import java.beans.PropertyDescriptor import java.io.PrintStream +import java.util.* /** * Represents the different Java language versions. @@ -105,7 +106,7 @@ object CustomTreePrinter : KotlintestBeanTreePrinter(NodeTreeLikeAdapter) return when { // boolean getter ktPropName matches Regex("is[A-Z].*") -> ktPropName - else -> "get" + ktPropName.capitalize() + else -> "get" + ktPropName.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } } } @@ -266,9 +267,12 @@ open class ParserTestCtx(testScope: TestScope, Pair(false, e) } - return MatcherResult(pass, - "Expected '$value' to parse in $nodeParsingCtx, got $e", + return MatcherResult( + pass, + { "Expected '$value' to parse in $nodeParsingCtx, got $e" }, + { "Expected '$value' not to parse in ${nodeParsingCtx.toString().addArticle()}" + } ) } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt index 8ce199cee0..5428d015c1 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt @@ -30,24 +30,30 @@ fun > C?.shouldContainAtMostOneOf(vararg expected: T) { fun haveModifier(mod: JModifier): Matcher = object : Matcher { override fun test(value: ModifierOwner): MatcherResult = - MatcherResult(value.hasModifiers(mod), "Expected $value to have modifier $mod", "Expected $value to not have modifier $mod") + MatcherResult(value.hasModifiers(mod), + { "Expected $value to have modifier $mod" }, + { "Expected $value to not have modifier $mod" }) } fun haveExplicitModifier(mod: JModifier): Matcher = object : Matcher { override fun test(value: ModifierOwner): MatcherResult { - return MatcherResult(value.hasExplicitModifiers(mod), "Expected $value to have modifier $mod", "Expected $value to not have modifier $mod") + return MatcherResult(value.hasExplicitModifiers(mod), + { "Expected $value to have modifier $mod" }, + { "Expected $value to not have modifier $mod" }) } } fun haveVisibility(vis: ModifierOwner.Visibility): Matcher = object : Matcher { override fun test(value: ModifierOwner): MatcherResult = - MatcherResult(value.visibility == vis, "Expected $value to have visibility $vis", "Expected $value to not have visibility $vis") + MatcherResult(value.visibility == vis, + { "Expected $value to have visibility $vis" }, + { "Expected $value to not have visibility $vis" }) } fun JavaNode.tokenList(): List = tokens().toList() -fun String.addArticle() = when (this[0].toLowerCase()) { +fun String.addArticle() = when (this[0].lowercaseChar()) { 'a', 'e', 'i', 'o', 'u' -> "an $this" else -> "a $this" } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/StressTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/StressTest.kt index 558d55b30d..587e564fd7 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/StressTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/StressTest.kt @@ -17,6 +17,7 @@ import net.sourceforge.pmd.lang.java.types.shouldHaveType import net.sourceforge.pmd.lang.java.types.testdata.BoolLogic import net.sourceforge.pmd.lang.java.types.testdata.TypeInferenceTestCases import net.sourceforge.pmd.lang.java.types.typeDsl +import java.util.* import kotlin.system.measureTimeMillis import kotlin.test.assertFalse @@ -37,7 +38,8 @@ class StressTest : ProcessorTestSpec({ fun TreeNodeWrapper.typeIs(value: Boolean) { it.typeMirror.shouldBeA { - it.symbol.binaryName shouldBe "net.sourceforge.pmd.lang.java.types.testdata.BoolLogic\$${value.toString().capitalize()}" + it.symbol.binaryName shouldBe "net.sourceforge.pmd.lang.java.types.testdata.BoolLogic\$${value.toString() + .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }}" } }