diff options
author | Manoj Palat | 2016-08-30 07:16:28 +0000 |
---|---|---|
committer | Manoj Palat | 2016-08-30 07:16:28 +0000 |
commit | 1fdc17fa0b57914583904c063de629ee82a8f4a8 (patch) | |
tree | 6b070652f195a7c4ced31076d43053c7b04b8f45 | |
parent | a26acb5f5e4ca1e211437d5a7456e6084c8c5b2d (diff) | |
download | eclipse.jdt.core-1fdc17fa0b57914583904c063de629ee82a8f4a8.tar.gz eclipse.jdt.core-1fdc17fa0b57914583904c063de629ee82a8f4a8.tar.xz eclipse.jdt.core-1fdc17fa0b57914583904c063de629ee82a8f4a8.zip |
Fix for comment 3 of bug 488659 [1.9] Allow effectively-final variables
to be used as resources in the try-with-resources
3 files changed, 67 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryStatement17Test.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryStatement17Test.java index c1d9a36243..5b7e91214b 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryStatement17Test.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryStatement17Test.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2003, 2014 IBM Corporation and others. + * Copyright (c) 2003, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -11,6 +11,8 @@ *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; +import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; + import junit.framework.Test; @SuppressWarnings({ "rawtypes" }) public class TryStatement17Test extends AbstractRegressionTest { @@ -1261,6 +1263,61 @@ public void testBug404146() { "}\n" }); } +public void testBug488569_001() { + if (this.complianceLevel < ClassFileConstants.JDK9) { + this.runNegativeTest( + new String[] { + "X.java", + "public class X {\n" + + " public static void main(String [] args) throws Exception {\n" + + " Z z1 = new Z();\n" + + " try (Y y1 = new Y(); z1;) {\n" + + " } \n" + + " } \n" + + "}\n" + + "class Y implements AutoCloseable {\n" + + " public void close() throws Exception {\n" + + " }\n" + + "}\n" + + "\n" + + "class Z implements AutoCloseable {\n" + + " public void close() throws Exception {\n" + + " } \n" + + "}\n" + + "\n" + }, + "----------\n" + + "1. ERROR in X.java (at line 4)\n" + + " try (Y y1 = new Y(); z1;) {\n" + + " ^^\n" + + "Variable Resource not allowed here for source level below 9\n" + + "----------\n"); + } else { + this.runConformTest( + new String[] { + "X.java", + "public class X {\n" + + " public static void main(String [] args) throws Exception {\n" + + " Z z1 = new Z();\n" + + " try (Y y1 = new Y(); z1;) {\n" + + " } \n" + + " } \n" + + "}\n" + + "class Y implements AutoCloseable {\n" + + " public void close() throws Exception {\n" + + " }\n" + + "}\n" + + "\n" + + "class Z implements AutoCloseable {\n" + + " public void close() throws Exception {\n" + + " } \n" + + "}\n" + + "\n" + }, + ""); + + } +} public static Class testClass() { return TryStatement17Test.class; diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java index 96d3065522..de003a5dca 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java @@ -9157,6 +9157,14 @@ protected void consumeStatementTry(boolean withFinally, boolean hasResources) { if (this.options.sourceLevel < ClassFileConstants.JDK1_7) { problemReporter().autoManagedResourcesNotBelow17(stmts); } + if (this.options.sourceLevel < ClassFileConstants.JDK9) { + for (int i = 0, l = stmts.length; i < l; ++i) { + Statement stmt = stmts[i]; + if (stmt instanceof FieldReference || stmt instanceof NameReference) { + problemReporter().autoManagedVariableResourcesNotBelow9((Expression) stmt); + } + } + } } //positions tryStmt.sourceEnd = this.endStatementPosition; diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties index c10b605fed..9d1b2562b0 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties @@ -766,7 +766,7 @@ 896 = Static methods are allowed in interfaces only at source level 1.8 or above #### Java 9 -876 = Variable Resource specification not allowed here for source level below 9 +876 = Variable Resource not allowed here for source level below 9 897 = Duplicate annotation of non-repeatable type @{0}. Only annotation types marked @Repeatable can be used multiple times at one target. 898 = The annotation @{0} cannot be repeated at this location since its container annotation type @{1} is disallowed at this location |