changeset 33711:2c47dffb2e18

8142476: Call site initialization exception caused by LambdaConversionException: Invalid receiver type Summary: Incorrect handling of intersection typed receiver in method references results in call site initialization exception Reviewed-by: mcimadamore
author sadayapalam
date Thu, 12 Nov 2015 05:59:47 +0530
parents acb12d30a5ac
children fd1ce6d7ac63
files langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransTypes.java langtools/test/tools/javac/lambda/methodReference/IntersectionTypeReceiverTest2.java
diffstat 2 files changed, 60 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransTypes.java	Wed Nov 11 18:46:03 2015 +0530
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransTypes.java	Thu Nov 12 05:59:47 2015 +0530
@@ -32,6 +32,7 @@
 import com.sun.tools.javac.code.Symbol.*;
 import com.sun.tools.javac.tree.*;
 import com.sun.tools.javac.tree.JCTree.*;
+import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
 import com.sun.tools.javac.util.*;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
 import com.sun.tools.javac.util.List;
@@ -855,7 +856,14 @@
     }
 
     public void visitReference(JCMemberReference tree) {
-        tree.expr = translate(tree.expr, erasure(tree.expr.type));
+        Type t = types.skipTypeVars(tree.expr.type, false);
+        Type receiverTarget = t.isCompound() ? erasure(tree.sym.owner.type) : erasure(t);
+        if (tree.kind == ReferenceKind.UNBOUND) {
+            tree.expr = make.Type(receiverTarget);
+        } else {
+            tree.expr = translate(tree.expr, receiverTarget);
+        }
+
         tree.type = erasure(tree.type);
         if (tree.varargsElement != null)
             tree.varargsElement = erasure(tree.varargsElement);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/methodReference/IntersectionTypeReceiverTest2.java	Thu Nov 12 05:59:47 2015 +0530
@@ -0,0 +1,51 @@
+/*
+ * 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 8142476
+ * @summary Test that Call site initialization exception is not thrown when the method
+   reference receiver is of intersection type.
+ * @run main IntersectionTypeReceiverTest2
+ */
+
+public class IntersectionTypeReceiverTest2 {
+    interface I {
+    }
+
+    interface J {
+        void foo();
+    }
+
+    static <T extends I & J> void bar(T t) {
+        Runnable r = t::foo;
+    }
+
+    public static void main(String[] args) {
+        class A implements I, J {
+            public void foo() {
+            }
+        }
+        bar(new A());
+    }
+}