[java] Avoid usage of deprecated methods

- firstChild() instead of getFirstChildOfType()
- descendants() instead of findDescendantsOfType()
- ancestors() instead of getFirstParentOfType()
This commit is contained in:
Andreas Dangel
2023-12-14 09:40:26 +01:00
parent 0fb4593234
commit 7a0f7316b7
33 changed files with 91 additions and 73 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -87,7 +87,7 @@ public final class ASTExplicitConstructorInvocation extends AbstractJavaTypeNode
@Override
@Nullable
public ASTTypeArguments getExplicitTypeArguments() {
return getFirstChildOfType(ASTTypeArguments.class);
return firstChild(ASTTypeArguments.class);
}
/**

View File

@ -70,7 +70,7 @@ public final class ASTFieldDeclaration extends AbstractJavaNode
*/
@Override
public ASTType getTypeNode() {
return getFirstChildOfType(ASTType.class);
return firstChild(ASTType.class);
}
/**

View File

@ -31,7 +31,7 @@ public final class ASTForStatement extends AbstractStatement implements ASTLoopS
@Override
public ASTExpression getCondition() {
return getFirstChildOfType(ASTExpression.class);
return firstChild(ASTExpression.class);
}
/**

View File

@ -45,7 +45,7 @@ public final class ASTForeachStatement extends AbstractStatement implements Inte
*/
@NonNull
public ASTExpression getIterableExpr() {
return getFirstChildOfType(ASTExpression.class);
return firstChild(ASTExpression.class);
}

View File

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

View File

@ -73,7 +73,7 @@ public final class ASTLocalVariableDeclaration extends AbstractJavaNode
*/
@Override
public ASTType getTypeNode() {
return getFirstChildOfType(ASTType.class);
return firstChild(ASTType.class);
}
@Override

View File

@ -67,7 +67,7 @@ public final class ASTMethodCall extends AbstractInvocationExpr
@Override
@Nullable
public ASTTypeArguments getExplicitTypeArguments() {
return getFirstChildOfType(ASTTypeArguments.class);
return firstChild(ASTTypeArguments.class);
}
@Override

View File

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

View File

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

View File

@ -40,12 +40,12 @@ public final class ASTRecordDeclaration extends AbstractTypeDeclaration {
@Override
public NodeStream<ASTBodyDeclaration> 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);
}
}

View File

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

View File

@ -76,7 +76,7 @@ public final class ASTTryStatement extends AbstractStatement {
*/
@Nullable
public ASTFinallyClause getFinallyClause() {
return getFirstChildOfType(ASTFinallyClause.class);
return firstChild(ASTFinallyClause.class);
}
}

View File

@ -155,7 +155,7 @@ public interface ASTTypeDeclaration
* an enum declaration, returns an empty stream.
*/
default NodeStream<ASTEnumConstant> getEnumConstants() {
return getFirstChildOfType(ASTEnumBody.class).children(ASTEnumConstant.class);
return firstChild(ASTEnumBody.class).children(ASTEnumConstant.class);
}

View File

@ -56,7 +56,7 @@ public final class ASTTypeParameter extends AbstractTypedSymbolDeclarator<JTypeP
*/
@Nullable
public ASTType getTypeBoundNode() {
return getFirstChildOfType(ASTType.class);
return firstChild(ASTType.class);
}
/**

View File

@ -47,7 +47,7 @@ public interface JavaNode extends JjtreeNode<JavaNode> {
* {@linkplain ASTTypeDeclaration TypeDeclaration}s.
*/
default ASTTypeDeclaration getEnclosingType() {
return getFirstParentOfType(ASTTypeDeclaration.class);
return ancestors(ASTTypeDeclaration.class).first();
}

View File

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

View File

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

View File

@ -300,7 +300,7 @@ class JDKVersionTest extends BaseJavaTreeDumpTest {
@Test
void jdk7PrivateMethodInnerClassInterface1() {
ASTCompilationUnit acu = java7.parseResource("private_method_in_inner_class_interface1.java");
List<ASTMethodDeclaration> methods = acu.findDescendantsOfType(ASTMethodDeclaration.class, true);
List<ASTMethodDeclaration> methods = acu.descendants(ASTMethodDeclaration.class).crossFindBoundaries().toList();
assertEquals(3, methods.size());
for (ASTMethodDeclaration method : methods) {
assertFalse(method.getEnclosingType().isInterface());

View File

@ -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<ASTLocalVariableDeclaration> 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<String>();
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<ASTLocalVariableDeclaration> localVars = compilationUnit.findDescendantsOfType(ASTLocalVariableDeclaration.class);
List<ASTLocalVariableDeclaration> localVars = compilationUnit.descendants(ASTLocalVariableDeclaration.class).toList();
assertEquals(3, localVars.size());
TypeSystem ts = compilationUnit.getTypeSystem();
@ -81,7 +83,8 @@ class Java10Test {
@Test
void testForLoopWithVar() {
List<ASTLocalVariableDeclaration> 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<ASTLocalVariableDeclaration> 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<ASTLocalVariableDeclaration> 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<ASTResource> resources = java10.parseResource("LocalVariableTypeInferenceTryWithResources.java")
.findDescendantsOfType(ASTResource.class);
.descendants(ASTResource.class)
.toList();
assertEquals(1, resources.size());
assertNull(resources.get(0).asLocalVariableDeclaration().getTypeNode());

View File

@ -55,7 +55,7 @@ class JavaQualifiedNameTest {
List<ASTClassDeclaration> 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());
}
}

View File

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

Some files were not shown because too many files have changed in this diff Show More