Skip to content

Commit

Permalink
Merge pull request #3774 from oowekyala:issue3697-parse-twr
Browse files Browse the repository at this point in the history
[java] Fix #3697 - lookahead error in concise resource spec #3774
  • Loading branch information
adangel committed Feb 19, 2022
2 parents 4894c28 + f981445 commit b6dae35
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ This is a {{ site.pmd.release_type }} release.

* misc
* [#3759](https://github.com/pmd/pmd/issues/3759): \[lang-test] Upgrade dokka maven plugin to 1.4.32
* java
* [#3698](https://github.com/pmd/pmd/issues/3697): \[java] Parsing error with try-with-resources and qualified resource
* java-codestyle
* [#278](https://github.com/pmd/pmd/issues/278): \[java] ConfusingTernary should treat `!= null` as positive condition

Expand Down
12 changes: 9 additions & 3 deletions pmd-java/etc/grammar/Java.jjt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ public class InputJava9TryWithResources {
public static void main() {
MyResource resource1 = new MyResource();
MyResource resource2 = new MyResource();
try (resource1) { }
try (resource1;) { }
try (resource1; resource2) { }
try (resource1.foo) { }
try (resource1.foo.a) { }
try (resource1.foo.Type v = null) { }
try (this.foo.aa) { }
try (this.foo) { }
}
}
}

0 comments on commit b6dae35

Please sign in to comment.