From c5f4f3cd499bc122bc730ff21373166ac984e2f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 11 Feb 2022 21:25:36 +0100 Subject: [PATCH] Fix #3697 - lookahead error in concise resource spec --- docs/pages/release_notes.md | 3 ++ pmd-java/etc/grammar/Java.jjt | 12 ++++++-- .../pmd/lang/java/ast/ParserCornersTest.java | 28 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index b8f8783555..7f301e6a55 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -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 diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index ad91264de5..aee40b0ff7 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -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() : diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java index c11e66423e..ef40440835 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java @@ -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