Support concise try-with-resources with java9

This commit is contained in:
Andreas Dangel
2017-09-23 00:15:08 +02:00
parent 280a656f87
commit 6590a358e6
3 changed files with 26 additions and 5 deletions

View File

@ -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() :

View File

@ -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"));
}
}

View File

@ -0,0 +1,7 @@
public class InputJava9TryWithResources {
public static void main() {
MyResource resource1 = new MyResource();
MyResource resource2 = new MyResource();
try (resource1; resource2) { }
}
}