changeset 554:317bac39b8a4

*) Fixed lambda conversion bug involving handling of void type *) Added regression test showing usage of function types (LambdaConv02.java)
author mcimadamore
date Fri, 28 May 2010 10:45:38 +0100
parents 7704dcd17e0b
children 7586844b51d3
files src/share/classes/com/sun/tools/javac/code/Types.java src/share/classes/com/sun/tools/javac/comp/Lower.java test/tools/javac/lambda/LambdaConv02.java
diffstat 3 files changed, 66 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/Types.java	Thu May 27 18:11:12 2010 +0100
+++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Fri May 28 10:45:38 2010 +0100
@@ -270,9 +270,11 @@
         if (isFunctionType(t) && findSAM(s) != null) {
             Type mtype = findSAM(s);
             boolean isReturnOk = t.getReturnType() == syms.voidType ?
-                mtype.getReturnType() == syms.voidType :
+                (mtype.getReturnType() == syms.voidType ||
+                mtype.getReturnType() == boxedClass(syms.voidType).type) :
                 isConvertible(t.getReturnType(), mtype.getReturnType(), warn);
-            boolean argsOk = isSameTypes(t.getParameterTypes(), mtype.getParameterTypes());
+            boolean argsOk = t.getParameterTypes().size() == mtype.getParameterTypes().size() &&
+                    isSameTypes(t.getParameterTypes(), mtype.getParameterTypes());
             boolean thrownOk = chk.unhandled(t.getThrownTypes(), mtype.getThrownTypes()).isEmpty();
             return isReturnOk && argsOk && thrownOk;
         }
@@ -493,7 +495,8 @@
                 else if (isFunctionType(s)) {
                     FunctionType that = (FunctionType)s;
                     boolean isReturnOk = t.getReturnType() == syms.voidType ?
-                    that.getReturnType() == syms.voidType :
+                        (that.getReturnType() == syms.voidType ||
+                        that.getReturnType() == boxedClass(syms.voidType).type):
                     isSubtype(t.getReturnType(), that.getReturnType());
                     boolean argsOk = t.getParameterTypes().size() == that.getParameterTypes().size() &&
                             isSubtypes(that.getParameterTypes(), t.getParameterTypes());
--- a/src/share/classes/com/sun/tools/javac/comp/Lower.java	Thu May 27 18:11:12 2010 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java	Fri May 28 10:45:38 2010 +0100
@@ -2642,10 +2642,12 @@
                 names.invokeVarargs,
                 List.<JCExpression>of(makeArray(syms.objectType, tree.args)));
         Type expectedType = meth.type.getReturnType();
-        translatedCall = (JCExpression)convert(translatedCall,
-                expectedType.isPrimitive() ?
-                    types.boxedClass(expectedType).type :
-                    expectedType);
+        if (expectedType != syms.voidType) {
+            translatedCall = (JCExpression)convert(translatedCall,
+                    expectedType.isPrimitive() ?
+                        types.boxedClass(expectedType).type :
+                        expectedType);
+        }
         result = translate(translatedCall, expectedType);
         return;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/LambdaConv02.java	Fri May 28 10:45:38 2010 +0100
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @summary basic test for lambda conversion and function types
+ * @author  Maurizio Cimadamore
+ * @compile -XDallowFunctionTypes LambdaConv02.java
+ * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles LambdaConv02
+ */
+
+public class LambdaConv02 {
+
+    interface A { void f(int i1, int i2, int i3) throws Exception; }
+    interface B { void f(int i1, int i2, int i3); }
+    interface C extends A, B {}
+
+    static int assertionCount = 0;
+
+    static void assertTrue(boolean cond) {
+        assertionCount++;
+        if (!cond)
+            throw new AssertionError();
+    }
+    public static void main(String[] args) throws Exception {
+        #void(int,int,int)(throws Exception) lambda = #(int i1, int i2, int i3) {
+            assertTrue(i1 + i2 == i3); };
+        lambda.(1,2,3);
+        C foo1 = lambda;
+        foo1.f(3,4,7);
+
+        assertTrue(assertionCount == 2);
+    }
+}