mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 02:54:35 +02:00
7013420: Project Coin: remove general expression support from try-with-resources statement
Reviewed-by: mcimadamore, jjg
This commit is contained in:
parent
bcf09ec04f
commit
ec50779b72
16 changed files with 118 additions and 172 deletions
|
@ -142,7 +142,7 @@ public class JavacParser implements Parser {
|
|||
*/
|
||||
boolean allowAnnotations;
|
||||
|
||||
/** Switch: should we recognize automatic resource management?
|
||||
/** Switch: should we recognize try-with-resources?
|
||||
*/
|
||||
boolean allowTWR;
|
||||
|
||||
|
@ -2184,29 +2184,23 @@ public class JavacParser implements Parser {
|
|||
while (S.token() == SEMI) {
|
||||
// All but last of multiple declarators subsume a semicolon
|
||||
storeEnd(defs.elems.last(), S.endPos());
|
||||
int semiColonPos = S.pos();
|
||||
S.nextToken();
|
||||
if (S.token() == RPAREN) { // Illegal trailing semicolon
|
||||
// after last resource
|
||||
error(semiColonPos, "try.resource.trailing.semi");
|
||||
break;
|
||||
}
|
||||
defs.append(resource());
|
||||
}
|
||||
return defs.toList();
|
||||
}
|
||||
|
||||
/** Resource =
|
||||
* VariableModifiers Type VariableDeclaratorId = Expression
|
||||
* | Expression
|
||||
/** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression
|
||||
*/
|
||||
JCTree resource() {
|
||||
int pos = S.pos();
|
||||
if (S.token() == FINAL || S.token() == MONKEYS_AT) {
|
||||
return variableDeclaratorRest(pos, optFinal(0), parseType(),
|
||||
ident(), true, null);
|
||||
} else {
|
||||
JCExpression t = term(EXPR | TYPE);
|
||||
if ((lastmode & TYPE) != 0 && S.token() == IDENTIFIER)
|
||||
return variableDeclaratorRest(pos, toP(F.at(pos).Modifiers(Flags.FINAL)), t,
|
||||
ident(), true, null);
|
||||
else
|
||||
return t;
|
||||
}
|
||||
return variableDeclaratorRest(S.pos(), optFinal(Flags.FINAL),
|
||||
parseType(), ident(), true, null);
|
||||
}
|
||||
|
||||
/** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
|
||||
|
|
|
@ -177,6 +177,8 @@ compiler.err.final.parameter.may.not.be.assigned=\
|
|||
final parameter {0} may not be assigned
|
||||
compiler.err.try.resource.may.not.be.assigned=\
|
||||
auto-closeable resource {0} may not be assigned
|
||||
compiler.err.try.resource.trailing.semi=\
|
||||
illegal trailing semicolon in resources declaration
|
||||
compiler.err.multicatch.parameter.may.not.be.assigned=\
|
||||
multi-catch parameter {0} may not be assigned
|
||||
compiler.err.finally.without.try=\
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
BadTwrSyntax.java:14:43: compiler.err.illegal.start.of.expr
|
||||
BadTwrSyntax.java:14:42: compiler.err.try.resource.trailing.semi
|
||||
1 error
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2010, 2011 Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -23,9 +23,9 @@
|
|||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6911256 6964740 6965277
|
||||
* @bug 6911256 6964740 6965277 7013420
|
||||
* @author Maurizio Cimadamore
|
||||
* @summary Check that lowered arm block does not end up creating resource twice
|
||||
* @summary Check that lowered try-with-resources block does not end up creating resource twice
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -45,7 +45,7 @@ public class DuplicateResource {
|
|||
static ArrayList<TestResource> resources = new ArrayList<TestResource>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
try(new TestResource()) {
|
||||
try(TestResource tr = new TestResource()) {
|
||||
//do something
|
||||
} catch (Exception e) {
|
||||
throw new AssertionError("Shouldn't reach here", e);
|
||||
|
@ -59,7 +59,7 @@ public class DuplicateResource {
|
|||
}
|
||||
TestResource resource = resources.get(0);
|
||||
if (!resource.isClosed) {
|
||||
throw new AssertionError("Resource used in ARM block has not been automatically closed");
|
||||
throw new AssertionError("Resource used in try-with-resources block has not been automatically closed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7013420
|
||||
* @author Joseph D. Darcy
|
||||
* @summary Test that resource variables are accepted as explicitly final.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ExplicitFinal implements AutoCloseable {
|
||||
public static void main(String... args) {
|
||||
try(final ExplicitFinal r2 = new ExplicitFinal()) {
|
||||
r2.toString();
|
||||
} catch (IOException ioe) {
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
|
||||
try(final @SuppressWarnings("unchecked") ExplicitFinal r3 = new ExplicitFinal()) {
|
||||
r3.toString();
|
||||
} catch (IOException ioe) {
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
|
||||
try(@SuppressWarnings("unchecked") ExplicitFinal r4 = new ExplicitFinal()) {
|
||||
r4.toString();
|
||||
} catch (IOException ioe) {
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
}
|
||||
public void close() throws IOException {
|
||||
System.out.println("Calling close on " + this);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 6911256 6964740 6965277
|
||||
* @bug 6911256 6964740 6965277 7013420
|
||||
* @author Maurizio Cimadamore
|
||||
* @summary Test that resource variables are implicitly final
|
||||
* @compile/fail/ref=ImplicitFinal.out -XDrawDiagnostics ImplicitFinal.java
|
||||
|
@ -15,12 +15,25 @@ class ImplicitFinal implements AutoCloseable {
|
|||
} catch (IOException ioe) { // Not reachable
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
|
||||
try(@SuppressWarnings("unchecked") ImplicitFinal r1 = new ImplicitFinal()) {
|
||||
r1 = null; //disallowed
|
||||
} catch (IOException ioe) { // Not reachable
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
|
||||
try(final ImplicitFinal r2 = new ImplicitFinal()) {
|
||||
r2 = null; //disallowed
|
||||
} catch (IOException ioe) { // Not reachable
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
|
||||
// A close method, but the class is <em>not</em> Closeable or
|
||||
// AutoCloseable.
|
||||
|
||||
try(final @SuppressWarnings("unchecked") ImplicitFinal r3 = new ImplicitFinal()) {
|
||||
r3 = null; //disallowed
|
||||
} catch (IOException ioe) { // Not reachable
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
}
|
||||
public void close() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
ImplicitFinal.java:14:13: compiler.err.try.resource.may.not.be.assigned: r
|
||||
1 error
|
||||
ImplicitFinal.java:20:13: compiler.err.try.resource.may.not.be.assigned: r1
|
||||
ImplicitFinal.java:26:13: compiler.err.try.resource.may.not.be.assigned: r2
|
||||
ImplicitFinal.java:32:13: compiler.err.try.resource.may.not.be.assigned: r3
|
||||
4 errors
|
||||
|
|
|
@ -1,26 +1,16 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 6911256 6964740
|
||||
* @bug 6911256 6964740 7013420
|
||||
* @author Joseph D. Darcy
|
||||
* @summary Test exception analysis of ARM blocks
|
||||
* @summary Test exception analysis of try-with-resources blocks
|
||||
* @compile/fail/ref=TwrFlow.out -XDrawDiagnostics TwrFlow.java
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
public class TwrFlow implements AutoCloseable {
|
||||
public static void main(String... args) {
|
||||
try(TwrFlow armflow = new TwrFlow()) {
|
||||
System.out.println(armflow.toString());
|
||||
} catch (IOException ioe) { // Not reachable
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
// CustomCloseException should be caught or added to throws clause
|
||||
|
||||
// Also check behavior on a resource expression rather than a
|
||||
// declaration.
|
||||
TwrFlow armflowexpr = new TwrFlow();
|
||||
try(armflowexpr) {
|
||||
System.out.println(armflowexpr.toString());
|
||||
try(TwrFlow twrFlow = new TwrFlow()) {
|
||||
System.out.println(twrFlow.toString());
|
||||
} catch (IOException ioe) { // Not reachable
|
||||
throw new AssertionError("Shouldn't reach here", ioe);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
TwrFlow.java:14:11: compiler.err.except.never.thrown.in.try: java.io.IOException
|
||||
TwrFlow.java:24:11: compiler.err.except.never.thrown.in.try: java.io.IOException
|
||||
TwrFlow.java:12:46: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException
|
||||
TwrFlow.java:22:26: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException
|
||||
4 errors
|
||||
2 errors
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6911256 6964740 6965277
|
||||
* @author Maurizio Cimadamore
|
||||
* @summary Resource of an intersection type crashes Flow
|
||||
* @compile TwrIntersection.java
|
||||
*/
|
||||
|
||||
interface MyCloseable extends AutoCloseable {
|
||||
void close() throws java.io.IOException;
|
||||
}
|
||||
|
||||
class ResourceTypeVar {
|
||||
|
||||
public void test() {
|
||||
try(getX()) {
|
||||
//do something
|
||||
} catch (java.io.IOException e) { // Not reachable
|
||||
throw new AssertionError("Shouldn't reach here", e);
|
||||
}
|
||||
}
|
||||
|
||||
<X extends Number & MyCloseable> X getX() { return null; }
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 6911256 6964740 6965277
|
||||
* @author Maurizio Cimadamore
|
||||
* @summary Check that resources of an intersection type forces union of exception types
|
||||
* to be caught outside twr block
|
||||
* @compile/fail/ref=TwrIntersection02.out -XDrawDiagnostics TwrIntersection02.java
|
||||
*/
|
||||
|
||||
class TwrIntersection02 {
|
||||
|
||||
static class Exception1 extends Exception {}
|
||||
static class Exception2 extends Exception {}
|
||||
|
||||
|
||||
interface MyResource1 extends AutoCloseable {
|
||||
void close() throws Exception1;
|
||||
}
|
||||
|
||||
interface MyResource2 extends AutoCloseable {
|
||||
void close() throws Exception2;
|
||||
}
|
||||
|
||||
public void test1() throws Exception1 {
|
||||
try(getX()) {
|
||||
//do something
|
||||
}
|
||||
}
|
||||
|
||||
public void test2() throws Exception2 {
|
||||
try(getX()) {
|
||||
//do something
|
||||
}
|
||||
}
|
||||
|
||||
<X extends MyResource1 & MyResource2> X getX() { return null; }
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
TwrIntersection02.java:25:21: compiler.err.unreported.exception.need.to.catch.or.throw: TwrIntersection02.Exception2
|
||||
TwrIntersection02.java:31:21: compiler.err.unreported.exception.need.to.catch.or.throw: TwrIntersection02.Exception1
|
||||
2 errors
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -23,7 +23,7 @@
|
|||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6911256 6964740
|
||||
* @bug 6911256 6964740 7013420
|
||||
* @author Joseph D. Darcy
|
||||
* @summary Test that TWR and multi-catch play well together
|
||||
* @compile TwrMultiCatch.java
|
||||
|
@ -48,9 +48,9 @@ public class TwrMultiCatch implements AutoCloseable {
|
|||
|
||||
private static void test(TwrMultiCatch twrMultiCatch,
|
||||
Class<? extends Exception> expected) {
|
||||
try(twrMultiCatch) {
|
||||
System.out.println(twrMultiCatch.toString());
|
||||
} catch (final CustomCloseException1 |
|
||||
try(TwrMultiCatch tmc = twrMultiCatch) {
|
||||
System.out.println(tmc.toString());
|
||||
} catch (CustomCloseException1 |
|
||||
CustomCloseException2 exception) {
|
||||
if (!exception.getClass().equals(expected) ) {
|
||||
throw new RuntimeException("Unexpected catch!");
|
||||
|
@ -68,7 +68,7 @@ public class TwrMultiCatch implements AutoCloseable {
|
|||
|
||||
try {
|
||||
throw t;
|
||||
} catch (final CustomCloseException1 |
|
||||
} catch (CustomCloseException1 |
|
||||
CustomCloseException2 exception) {
|
||||
throw exception;
|
||||
} catch (Throwable throwable) {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 6911256 6964740
|
||||
* @bug 6911256 6964740 7013420
|
||||
* @author Joseph D. Darcy
|
||||
* @summary Verify invalid TWR block is not accepted.
|
||||
* @compile/fail -source 6 TwrOnNonResource.java
|
||||
|
@ -18,18 +18,6 @@ class TwrOnNonResource {
|
|||
try(TwrOnNonResource aonr = new TwrOnNonResource()) {
|
||||
System.out.println(aonr.toString());
|
||||
} catch (Exception e) {;}
|
||||
|
||||
// Also check expression form
|
||||
TwrOnNonResource aonr = new TwrOnNonResource();
|
||||
try(aonr) {
|
||||
System.out.println(aonr.toString());
|
||||
}
|
||||
try(aonr) {
|
||||
System.out.println(aonr.toString());
|
||||
} finally {;}
|
||||
try(aonr) {
|
||||
System.out.println(aonr.toString());
|
||||
} catch (Exception e) {;}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
TwrOnNonResource.java:12:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
|
||||
TwrOnNonResource.java:15:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
|
||||
TwrOnNonResource.java:18:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
|
||||
TwrOnNonResource.java:24:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
|
||||
TwrOnNonResource.java:27:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
|
||||
TwrOnNonResource.java:30:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
|
||||
6 errors
|
||||
3 errors
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -21,23 +21,15 @@
|
|||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6911256 6964740 6965277
|
||||
* @author Maurizio Cimadamore
|
||||
* @summary Verify that method type-inference works as expected in TWR context
|
||||
* @compile TwrInference.java
|
||||
*/
|
||||
// key: compiler.err.try.resource.trailing.semi
|
||||
|
||||
class TwrInference {
|
||||
|
||||
public void test() {
|
||||
try(getX()) {
|
||||
//do something
|
||||
} catch (Exception e) { // Not reachable
|
||||
throw new AssertionError("Shouldn't reach here", e);
|
||||
class TryResoureTrailingSemi implements AutoCloseable {
|
||||
public static void main(String... args) {
|
||||
try(TryResoureTrailingSemi r = new TryResoureTrailingSemi();) {
|
||||
System.out.println(r.toString());
|
||||
}
|
||||
}
|
||||
|
||||
<X> X getX() { return null; }
|
||||
@Override
|
||||
public void close() {return;}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue