[java] Fix impl of ASTVariableId::isResourceDeclaration / VariableId/@ResourceDeclaration (#4988)

Merge pull request #4988 from oowekyala:java-fix-isResourceDeclaration
This commit is contained in:
Andreas Dangel
2024-05-02 12:44:55 +02:00
6 changed files with 14 additions and 8 deletions

View File

@ -22,6 +22,7 @@ This is a {{ site.pmd.release_type }} release.
* java
* [#4912](https://github.com/pmd/pmd/issues/4912): \[java] Unable to parse some Java9+ resource references
* [#4973](https://github.com/pmd/pmd/pull/4973): \[java] Stop parsing Java for CPD
* [#4988](https://github.com/pmd/pmd/pull/4988): \[java] Fix impl of ASTVariableId::isResourceDeclaration / VariableId/@<!-- -->ResourceDeclaration
* java-bestpractices
* [#4278](https://github.com/pmd/pmd/issues/4278): \[java] UnusedPrivateMethod FP with Junit 5 @MethodSource and default factory method name
* [#4852](https://github.com/pmd/pmd/issues/4852): \[java] ReplaceVectorWithList false-positive (neither Vector nor List usage)

View File

@ -232,7 +232,8 @@ public final class ASTVariableId extends AbstractTypedSymbolDeclarator<JVariable
* Returns true if this declarator id declares a resource in a try-with-resources statement.
*/
public boolean isResourceDeclaration() {
return getParent() instanceof ASTResource;
// Resource/LocalVariableDeclaration/VariableDeclarator
return getParent().getParent().getParent() instanceof ASTResource;
}

View File

@ -32,7 +32,9 @@ class ASTTryStatementTest : ParserTestSpec({
classType("Foo")
fromChild<ASTVariableDeclarator, ASTExpression> {
variableId("a")
variableId("a") {
it::isResourceDeclaration shouldBe true
}
int(2)
}
}
@ -57,7 +59,9 @@ class ASTTryStatementTest : ParserTestSpec({
}
classType("Foo")
fromChild<ASTVariableDeclarator, ASTExpression> {
variableId("a")
variableId("a") {
it::isResourceDeclaration shouldBe true
}
int(2)
}
}

View File

@ -408,7 +408,7 @@
| | | +- ModifierList[@EffectiveModifiers = "{final}", @ExplicitModifiers = "{}"]
| | | +- ClassType[@FullyQualified = false, @SimpleName = "BufferedReader"]
| | | +- VariableDeclarator[@Initializer = true, @Name = "br"]
| | | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = true, @Name = "br", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @Static = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL]
| | | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = false, @Name = "br", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = true, @Static = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL]
| | | +- ConstructorCall[@AnonymousClass = false, @CompileTimeConstant = false, @DiamondTypeArgs = false, @MethodName = "new", @ParenthesisDepth = 0, @Parenthesized = false, @QualifiedInstanceCreation = false]
| | | +- ClassType[@FullyQualified = false, @SimpleName = "BufferedReader"]
| | | +- ArgumentList[@Empty = false, @Size = 1]
@ -464,7 +464,7 @@
| | | +- ModifierList[@EffectiveModifiers = "{final}", @ExplicitModifiers = "{}"]
| | | +- ClassType[@FullyQualified = true, @SimpleName = "ZipFile"]
| | | +- VariableDeclarator[@Initializer = true, @Name = "zf"]
| | | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = true, @Name = "zf", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @Static = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL]
| | | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = false, @Name = "zf", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = true, @Static = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL]
| | | +- ConstructorCall[@AnonymousClass = false, @CompileTimeConstant = false, @DiamondTypeArgs = false, @MethodName = "new", @ParenthesisDepth = 0, @Parenthesized = false, @QualifiedInstanceCreation = false]
| | | +- ClassType[@FullyQualified = true, @SimpleName = "ZipFile"]
| | | +- ArgumentList[@Empty = false, @Size = 1]
@ -474,7 +474,7 @@
| | +- ModifierList[@EffectiveModifiers = "{final}", @ExplicitModifiers = "{}"]
| | +- ClassType[@FullyQualified = true, @SimpleName = "BufferedWriter"]
| | +- VariableDeclarator[@Initializer = true, @Name = "writer"]
| | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = true, @Name = "writer", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @Static = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL]
| | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = false, @Name = "writer", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = true, @Static = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL]
| | +- MethodCall[@CompileTimeConstant = false, @Image = "newBufferedWriter", @MethodName = "newBufferedWriter", @ParenthesisDepth = 0, @Parenthesized = false]
| | +- TypeExpression[@CompileTimeConstant = false, @ParenthesisDepth = 0, @Parenthesized = false]
| | | +- ClassType[@FullyQualified = true, @SimpleName = "Files"]

View File

@ -523,7 +523,7 @@
| | +- LocalVariableDeclaration[@EffectiveVisibility = Visibility.V_LOCAL, @Final = true, @TypeInferred = true, @Visibility = Visibility.V_LOCAL]
| | +- ModifierList[@EffectiveModifiers = "{final}", @ExplicitModifiers = "{}"]
| | +- VariableDeclarator[@Initializer = true, @Name = "_"]
| | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = true, @Name = "_", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @Static = false, @TypeInferred = true, @Visibility = Visibility.V_LOCAL]
| | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = false, @Name = "_", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = true, @Static = false, @TypeInferred = true, @Visibility = Visibility.V_LOCAL]
| | +- MethodCall[@CompileTimeConstant = false, @Image = "acquire", @MethodName = "acquire", @ParenthesisDepth = 0, @Parenthesized = false]
| | +- TypeExpression[@CompileTimeConstant = false, @ParenthesisDepth = 0, @Parenthesized = false]
| | | +- ClassType[@FullyQualified = false, @SimpleName = "ScopedContext"]

View File

@ -523,7 +523,7 @@
| | +- LocalVariableDeclaration[@EffectiveVisibility = Visibility.V_LOCAL, @Final = true, @TypeInferred = true, @Visibility = Visibility.V_LOCAL]
| | +- ModifierList[@EffectiveModifiers = "{final}", @ExplicitModifiers = "{}"]
| | +- VariableDeclarator[@Initializer = true, @Name = "_"]
| | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = true, @Name = "_", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @Static = false, @TypeInferred = true, @Visibility = Visibility.V_LOCAL]
| | +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = false, @Name = "_", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = true, @Static = false, @TypeInferred = true, @Visibility = Visibility.V_LOCAL]
| | +- MethodCall[@CompileTimeConstant = false, @Image = "acquire", @MethodName = "acquire", @ParenthesisDepth = 0, @Parenthesized = false]
| | +- TypeExpression[@CompileTimeConstant = false, @ParenthesisDepth = 0, @Parenthesized = false]
| | | +- ClassType[@FullyQualified = false, @SimpleName = "ScopedContext"]