test/tools/javac/lambda/methodReference/BridgeMethod.java

Thu, 11 Jul 2013 14:07:39 +0100

author
mcimadamore
date
Thu, 11 Jul 2013 14:07:39 +0100
changeset 1882
39ec5d8a691b
parent 1843
be62183f938a
child 2074
dee28dd47e12
permissions
-rw-r--r--

8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
8020010: Move lambda bridge creation from metafactory and VM to compiler
Summary: langtools/javac component of the bridge support and MethodType vs. MethodHandle changes.
Reviewed-by: jjg, vromero, briangoetz, forax
Contributed-by: robert.field@oracle.com

     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /**
    25  * @test
    26  * @bug 8003280
    27  * @summary Add lambda tests
    28  *   Test bridge methods in certain SAM conversion
    29  * @compile BridgeMethod.java
    30  * @run main BridgeMethod
    31  */
    33 import java.lang.reflect.Method;
    34 import java.util.HashSet;
    35 import java.util.Set;
    37 public class BridgeMethod {
    39     interface H {Object m();}
    41     interface K<T> {void m(T t);}
    43     interface L extends K<String> {} //generic substitution
    45     interface M {void m(String s);}
    47     interface KM extends K<String>, M{} //generic substitution
    49     interface N extends H {String m();} //covariant return
    51     private static void assertTrue(boolean cond) {
    52         if (!cond)
    53             throw new AssertionError();
    54     }
    56     static void bar(String s) {
    57         System.out.println("BridgeMethod.bar(String) " + s);
    58     }
    60     String moo() {
    61         return "moo";
    62     }
    64     private static Set<String> setOfStringObject() {
    65         Set<String> s = new HashSet<>();
    66         s.add("java.lang.String");
    67         s.add("java.lang.Object");
    68         return s;
    69     }
    71     public static void main(String[] args) {
    72         L la = BridgeMethod::bar; //static reference
    73         la.m("hi");
    74         Class<? extends L> c1 = la.getClass();
    75         Method[] methods = c1.getDeclaredMethods();
    76         Set<String> types = setOfStringObject();
    77         System.out.println("methods in SAM conversion of L:");
    78         for(Method m : methods) {
    79             System.out.println(m.toGenericString());
    80             assertTrue(m.getName().equals("m"));
    81             Class[] parameterTypes = m.getParameterTypes();
    82             assertTrue(parameterTypes.length == 1);
    83             assertTrue(types.remove(parameterTypes[0].getName()));
    84         }
    85         assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
    87         KM km = BridgeMethod::bar;
    88         //km.m("hi"); //will be uncommented when CR7028808 fixed
    89         Class<? extends KM> c2 = km.getClass();
    90         methods = c2.getDeclaredMethods();
    91         types = setOfStringObject();
    92         System.out.println("methods in SAM conversion of KM:");
    93         for(Method m : methods) {
    94             System.out.println(m.toGenericString());
    95             assertTrue(m.getName().equals("m"));
    96             Class<?>[] parameterTypes = m.getParameterTypes();
    97             assertTrue(parameterTypes.length == 1);
    98             assertTrue(types.remove(parameterTypes[0].getName()));
    99         }
   100         assertTrue(types.isEmpty());
   102         N n = new BridgeMethod()::moo; //instance reference
   103         assertTrue( n.m().equals("moo") );
   104         assertTrue( ((H)n).m().equals("moo") );
   105         Class<? extends N> c3 = n.getClass();
   106         methods = c3.getDeclaredMethods();
   107         types = setOfStringObject();
   108         System.out.println("methods in SAM conversion of N:");
   109         for(Method m : methods) {
   110             System.out.println(m.toGenericString());
   111             assertTrue(m.getName().equals("m"));
   112             Class<?> returnType = m.getReturnType();
   113             assertTrue(types.remove(returnType.getName()));
   114         }
   115         assertTrue(types.size() == 1); //there's a bridge
   116     }
   117 }

mercurial