From 6590a358e6ffe9506b47fcff914fbb597adaecab Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 23 Sep 2017 00:15:08 +0200 Subject: [PATCH] Support concise try-with-resources with java9 --- pmd-java/etc/grammar/Java.jjt | 14 +++++++++----- .../pmd/lang/java/ast/JDKVersionTest.java | 10 ++++++++++ .../jdkversiontests/jdk9_try_with_resources.java | 7 +++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk9_try_with_resources.java diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 77b608a17e..5f15749f5e 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -4,6 +4,7 @@ * A single underscore "_" is an invalid identifier in java9. * Diamond operator for anonymous classes is only allowed with java9. * Add support for module-info.java. + * Allow more concise try-with-resources statements with java9. * Andreas Dangel 09/2017 *==================================================================== * Add support for new Java 8 annotation locations. @@ -356,6 +357,11 @@ public class JavaParser { throwParseException("Cannot use module declaration when running in JDK inferior to 9 mode!"); } } + private void checkForBadConciseTryWithResourcesUsage() { + if (jdkVersion < 9) { + throwParseException("Cannot use concise try-with-resources when running in JDK inferior to 9 mode!"); + } + } // This is a semantic LOOKAHEAD to determine if we're dealing with an assert // Note that this can't be replaced with a syntactic lookahead @@ -2247,11 +2253,9 @@ void Resources() : void Resource() : {} { - ( "final" | Annotation() )* - Type() - VariableDeclaratorId() - "=" - Expression() + LOOKAHEAD(2) ( ( "final" | Annotation() )* Type() VariableDeclaratorId() "=" Expression() ) + | + Name() {checkForBadConciseTryWithResourcesUsage();} } void CatchStatement() : diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java index da519826be..aef4882b70 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java @@ -257,4 +257,14 @@ public class JDKVersionTest { public final void jdk9ModuleInfo() { parseJava9(loadSource("jdk9_module_info.java")); } + + @Test(expected = ParseException.class) + public final void jdk9TryWithResourcesInJava8() { + parseJava18(loadSource("jdk9_try_with_resources.java")); + } + + @Test + public final void jdk9TryWithResources() { + parseJava9(loadSource("jdk9_try_with_resources.java")); + } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk9_try_with_resources.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk9_try_with_resources.java new file mode 100644 index 0000000000..888c79a3d6 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/jdk9_try_with_resources.java @@ -0,0 +1,7 @@ +public class InputJava9TryWithResources { + public static void main() { + MyResource resource1 = new MyResource(); + MyResource resource2 = new MyResource(); + try (resource1; resource2) { } + } +} \ No newline at end of file