提交 20958648 编写于 作者: M mcimadamore

6970833: Try-with-resource implementation throws an NPE during Flow analysis

Summary: Updated logic not to rely upon Symbol.implementation (which check in superinterfaces)
Reviewed-by: jjg
上级 0c800b37
...@@ -37,6 +37,7 @@ import com.sun.tools.javac.util.*; ...@@ -37,6 +37,7 @@ import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.comp.Resolve;
import com.sun.tools.javac.tree.JCTree.*; import com.sun.tools.javac.tree.JCTree.*;
import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.code.Flags.*;
...@@ -187,6 +188,8 @@ public class Flow extends TreeScanner { ...@@ -187,6 +188,8 @@ public class Flow extends TreeScanner {
private final Types types; private final Types types;
private final Check chk; private final Check chk;
private TreeMaker make; private TreeMaker make;
private final Resolve rs;
private Env<AttrContext> attrEnv;
private Lint lint; private Lint lint;
private final boolean allowRethrowAnalysis; private final boolean allowRethrowAnalysis;
...@@ -205,6 +208,7 @@ public class Flow extends TreeScanner { ...@@ -205,6 +208,7 @@ public class Flow extends TreeScanner {
types = Types.instance(context); types = Types.instance(context);
chk = Check.instance(context); chk = Check.instance(context);
lint = Lint.instance(context); lint = Lint.instance(context);
rs = Resolve.instance(context);
Source source = Source.instance(context); Source source = Source.instance(context);
allowRethrowAnalysis = source.allowMulticatch(); allowRethrowAnalysis = source.allowMulticatch();
} }
...@@ -998,15 +1002,21 @@ public class Flow extends TreeScanner { ...@@ -998,15 +1002,21 @@ public class Flow extends TreeScanner {
} }
} }
for (JCTree resource : tree.resources) { for (JCTree resource : tree.resources) {
MethodSymbol topCloseMethod = (MethodSymbol)syms.autoCloseableType.tsym.members().lookup(names.close).sym;
List<Type> closeableSupertypes = resource.type.isCompound() ? List<Type> closeableSupertypes = resource.type.isCompound() ?
types.interfaces(resource.type).prepend(types.supertype(resource.type)) : types.interfaces(resource.type).prepend(types.supertype(resource.type)) :
List.of(resource.type); List.of(resource.type);
for (Type sup : closeableSupertypes) { for (Type sup : closeableSupertypes) {
if (types.asSuper(sup, syms.autoCloseableType.tsym) != null) { if (types.asSuper(sup, syms.autoCloseableType.tsym) != null) {
MethodSymbol closeMethod = types.implementation(topCloseMethod, sup.tsym, types, true); Symbol closeMethod = rs.resolveInternalMethod(tree,
for (Type t : closeMethod.getThrownTypes()) { attrEnv,
markThrown(tree.body, t); sup,
names.close,
List.<Type>nil(),
List.<Type>nil());
if (closeMethod.kind == MTH) {
for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) {
markThrown(tree.body, t);
}
} }
} }
} }
...@@ -1387,8 +1397,10 @@ public class Flow extends TreeScanner { ...@@ -1387,8 +1397,10 @@ public class Flow extends TreeScanner {
/** Perform definite assignment/unassignment analysis on a tree. /** Perform definite assignment/unassignment analysis on a tree.
*/ */
public void analyzeTree(JCTree tree, TreeMaker make) { public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
try { try {
attrEnv = env;
JCTree tree = env.tree;
this.make = make; this.make = make;
inits = new Bits(); inits = new Bits();
uninits = new Bits(); uninits = new Bits();
......
...@@ -1174,7 +1174,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter { ...@@ -1174,7 +1174,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
try { try {
make.at(Position.FIRSTPOS); make.at(Position.FIRSTPOS);
TreeMaker localMake = make.forToplevel(env.toplevel); TreeMaker localMake = make.forToplevel(env.toplevel);
flow.analyzeTree(env.tree, localMake); flow.analyzeTree(env, localMake);
compileStates.put(env, CompileState.FLOW); compileStates.put(env, CompileState.FLOW);
if (shouldStop(CompileState.FLOW)) if (shouldStop(CompileState.FLOW))
......
/*
* 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 6970833
* @author Maurizio Cimadamore
* @summary Try-with-resource implementation throws an NPE during Flow analysis
* @compile/fail/ref=ResourceInterface.out -XDrawDiagnostics ResourceInterface.java
*/
class ResourceInterface {
public void test1() {
try(Resource1 r1 = null) { }
}
public void test2() {
try(Resource2 r2 = null) { }
}
static class E1 extends Exception {}
static class E2 extends Exception {}
interface C1 extends AutoCloseable {
void close() throws E1;
}
interface C2 extends AutoCloseable {
void close() throws E2;
}
interface C3 extends AutoCloseable {
void close() throws E2, E1;
}
static interface Resource1 extends C1, C2 {}
static interface Resource2 extends C1, C3 {}
}
ResourceInterface.java:38:34: compiler.err.unreported.exception.need.to.catch.or.throw: ResourceInterface.E1
1 error
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册