Skip to content

Commit

Permalink
Fix #3697 - lookahead error in concise resource spec
Browse files Browse the repository at this point in the history
  • Loading branch information
oowekyala committed Feb 11, 2022
1 parent 1290999 commit c5f4f3c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/pages/release_notes.md
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions pmd-java/etc/grammar/Java.jjt
Expand Up @@ -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() :
Expand Down
Expand Up @@ -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");
Expand Down Expand Up @@ -90,6 +91,33 @@ public final void testCastLookaheadProblem() {
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
Expand Down

0 comments on commit c5f4f3c

Please sign in to comment.