6970833: Try-with-resource implementation throws an NPE during Flow analysis
authormcimadamore
Thu, 29 Jul 2010 15:57:43 +0100
changeset 6156 e15c221efaac
parent 6155 214f78bb8006
child 6157 218670cd9b1c
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
langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java
langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java
langtools/test/tools/javac/TryWithResources/ResourceInterface.java
langtools/test/tools/javac/TryWithResources/ResourceInterface.out
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Jul 29 15:57:18 2010 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Jul 29 15:57:43 2010 +0100
@@ -37,6 +37,7 @@
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
 
 import com.sun.tools.javac.code.Symbol.*;
+import com.sun.tools.javac.comp.Resolve;
 import com.sun.tools.javac.tree.JCTree.*;
 
 import static com.sun.tools.javac.code.Flags.*;
@@ -187,6 +188,8 @@
     private final Types types;
     private final Check chk;
     private       TreeMaker make;
+    private final Resolve rs;
+    private Env<AttrContext> attrEnv;
     private       Lint lint;
     private final boolean allowRethrowAnalysis;
 
@@ -205,6 +208,7 @@
         types = Types.instance(context);
         chk = Check.instance(context);
         lint = Lint.instance(context);
+        rs = Resolve.instance(context);
         Source source = Source.instance(context);
         allowRethrowAnalysis = source.allowMulticatch();
     }
@@ -998,15 +1002,21 @@
             }
         }
         for (JCTree resource : tree.resources) {
-            MethodSymbol topCloseMethod = (MethodSymbol)syms.autoCloseableType.tsym.members().lookup(names.close).sym;
             List<Type> closeableSupertypes = resource.type.isCompound() ?
                 types.interfaces(resource.type).prepend(types.supertype(resource.type)) :
                 List.of(resource.type);
             for (Type sup : closeableSupertypes) {
                 if (types.asSuper(sup, syms.autoCloseableType.tsym) != null) {
-                    MethodSymbol closeMethod = types.implementation(topCloseMethod, sup.tsym, types, true);
-                    for (Type t : closeMethod.getThrownTypes()) {
-                        markThrown(tree.body, t);
+                    Symbol closeMethod = rs.resolveInternalMethod(tree,
+                            attrEnv,
+                            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 @@
 
     /** Perform definite assignment/unassignment analysis on a tree.
      */
-    public void analyzeTree(JCTree tree, TreeMaker make) {
+    public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
         try {
+            attrEnv = env;
+            JCTree tree = env.tree;
             this.make = make;
             inits = new Bits();
             uninits = new Bits();
--- a/langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Thu Jul 29 15:57:18 2010 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Thu Jul 29 15:57:43 2010 +0100
@@ -1174,7 +1174,7 @@
             try {
                 make.at(Position.FIRSTPOS);
                 TreeMaker localMake = make.forToplevel(env.toplevel);
-                flow.analyzeTree(env.tree, localMake);
+                flow.analyzeTree(env, localMake);
                 compileStates.put(env, CompileState.FLOW);
 
                 if (shouldStop(CompileState.FLOW))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/TryWithResources/ResourceInterface.java	Thu Jul 29 15:57:43 2010 +0100
@@ -0,0 +1,61 @@
+/*
+ * 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 {}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/TryWithResources/ResourceInterface.out	Thu Jul 29 15:57:43 2010 +0100
@@ -0,0 +1,2 @@
+ResourceInterface.java:38:34: compiler.err.unreported.exception.need.to.catch.or.throw: ResourceInterface.E1
+1 error