8144673: Suspect message regarding suitable enclosing instance not being in scope
authorsadayapalam
Tue, 08 Dec 2015 04:59:19 +0530
changeset 34561 79c436257611
parent 34560 b6a567b677f7
child 34562 019cd319c62f
8144673: Suspect message regarding suitable enclosing instance not being in scope Summary: javac incorrectly complains about missing enclosing instance while handling method references. Reviewed-by: vromero
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
langtools/test/tools/javac/lambda/methodReference/MethodRefToLocalClassMethodTest.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java	Mon Dec 07 14:02:55 2015 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java	Tue Dec 08 04:59:19 2015 +0530
@@ -424,6 +424,9 @@
     }
 
     /** The closest enclosing class of this symbol's declaration.
+     *  Warning: this (misnamed) method returns the receiver itself
+     *  when the receiver is a class (as opposed to its enclosing
+     *  class as one may be misled to believe.)
      */
     public ClassSymbol enclClass() {
         Symbol c = this;
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Mon Dec 07 14:02:55 2015 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Tue Dec 08 04:59:19 2015 +0530
@@ -3365,7 +3365,7 @@
         if (env1 != null) {
             while (env1 != null && env1.outer != null) {
                 if (isStatic(env1)) staticOnly = true;
-                if (env1.enclClass.sym.isSubClass(member.owner, types)) {
+                if (env1.enclClass.sym.isSubClass(member.owner.enclClass(), types)) {
                     Symbol sym = env1.info.scope.findFirst(name);
                     if (sym != null) {
                         if (staticOnly) sym = new StaticError(sym);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/methodReference/MethodRefToLocalClassMethodTest.java	Tue Dec 08 04:59:19 2015 +0530
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2015, 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 8144673
+ * @summary Suspect message regarding suitable enclosing instance not being in scope
+ * @run main MethodRefToLocalClassMethodTest
+ */
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class MethodRefToLocalClassMethodTest {
+
+    public static void main(String[] args) {
+        new MethodRefToLocalClassMethodTest().foo();
+    }
+
+    public void foo() {
+        class LocalFoo {
+            LocalFoo(String in) {
+                if (!in.equals("Hello"))
+                    throw new AssertionError("Unexpected data: " + in);
+            }
+        }
+        List<String> ls = new ArrayList<>();
+        ls.add("Hello");
+        ls.stream().map(LocalFoo::new).forEach(x->{});
+    }
+}