Fix #3697 - lookahead error in concise resource spec

This commit is contained in:
Clément Fournier
2022-02-11 21:25:36 +01:00
parent 1290999ec2
commit c5f4f3cd49
3 changed files with 40 additions and 3 deletions

View File

@ -16,6 +16,9 @@ This is a {{ site.pmd.release_type }} release.
### Fixed Issues
* java
* [#3698](https://github.com/pmd/pmd/issues/3697): \[java] Parsing error with try-with-resources and qualified resource
### API Changes
### External Contributions

View File

@ -2406,9 +2406,15 @@ void Resources() :
void Resource() :
{}
{
LOOKAHEAD(2) ( ( "final" {jjtThis.setFinal(true);} | Annotation() )* LocalVariableType() VariableDeclaratorId() "=" Expression() )
|
Name() {checkForBadConciseTryWithResourcesUsage();}
LOOKAHEAD("this" | Name() ")") (
{checkForBadConciseTryWithResourcesUsage();}
Name()
// replaced with Expression in PMD 7, do the bare minimum
| "this" "." Name() // possible pmd6 improvement: add a isThisModifier() or so
)
| ( "final" {jjtThis.setFinal(true);} | Annotation() )*
LocalVariableType() VariableDeclaratorId() "=" Expression()
}
void CatchStatement() :

View File

@ -25,6 +25,7 @@ public class ParserCornersTest {
private static final String MULTICATCH = "public class Foo { public void bar() { "
+ "try { System.out.println(); } catch (RuntimeException | IOException e) {} } }";
private final JavaParsingHelper java = JavaParsingHelper.WITH_PROCESSING.withResourceContext(ParserCornersTest.class);
private final JavaParsingHelper java9 = java.withDefaultVersion("9");
private final JavaParsingHelper java8 = java.withDefaultVersion("1.8");
private final JavaParsingHelper java4 = java.withDefaultVersion("1.4");
private final JavaParsingHelper java5 = java.withDefaultVersion("1.5");
@ -90,6 +91,33 @@ public class ParserCornersTest {
java4.parse(CAST_LOOKAHEAD_PROBLEM);
}
@Test
public final void testTryWithResourcesConcise() {
// https://github.com/pmd/pmd/issues/3697
java9.parse("import java.io.InputStream;\n"
+ "public class Foo {\n"
+ " public InputStream in;\n"
+ " public void bar() {\n"
+ " Foo f = this;\n"
+ " try (f.in) {\n"
+ " }\n"
+ " }\n"
+ "}");
}
@Test
public final void testTryWithResourcesThis() {
// https://github.com/pmd/pmd/issues/3697
java9.parse("import java.io.InputStream;\n"
+ "public class Foo {\n"
+ " public InputStream in;\n"
+ " public void bar() {\n"
+ " try (this.in) {\n"
+ " }\n"
+ " }\n"
+ "}");
}
/**
* Tests a specific generic notation for calling methods. See:
* https://jira.codehaus.org/browse/MPMD-139