Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Fix #3697 - lookahead error in concise resource spec #3774

Merged
merged 2 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
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
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) { }
}
}
}