1 /*
   2  * Copyright (c) 2011, 2013, 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  */
  23 
  24 /**
  25  * @test
  26  * @bug 8003280
  27  * @summary Add lambda tests
  28  *   Test bridge methods in certain SAM conversion
  29  *   Tests that jdk.internal.lambda.disableEagerInitialization=true creates a
  30  *   get$Lambda method for non-capturing lambdas
  31  * @compile BridgeMethod.java
  32  * @run main BridgeMethod
  33  * @run main/othervm -Djdk.internal.lambda.disableEagerInitialization=true BridgeMethod
  34  */
  35 
  36 import java.lang.reflect.Method;
  37 import java.util.Arrays;
  38 import java.util.HashSet;
  39 import java.util.Set;
  40 
  41 public class BridgeMethod {
  42 
  43     interface H {Object m();}
  44 
  45     interface K<T> {void m(T t);}
  46 
  47     interface L extends K<String> {} //generic substitution
  48 
  49     interface M {void m(String s);}
  50 
  51     interface KM extends K<String>, M{} //generic substitution
  52 
  53     interface N extends H {String m();} //covariant return
  54 
  55     private static void assertTrue(boolean cond) {
  56         if (!cond)
  57             throw new AssertionError();
  58     }
  59 
  60     static void bar(String s) {
  61         System.out.println("BridgeMethod.bar(String) " + s);
  62     }
  63 
  64     String moo() {
  65         return "moo";
  66     }
  67 
  68     private static Set<String> setOfStringObject() {
  69         Set<String> s = new HashSet<>();
  70         s.add("java.lang.String");
  71         s.add("java.lang.Object");
  72         return s;
  73     }
  74 
  75     private static Set<String> allowedMethods() {
  76         Set<String> s = new HashSet<>();
  77         s.add("m");
  78         return s;
  79     }
  80 
  81     private static boolean matchingMethodNames(Method[] methods) {
  82         Set<String> methodNames = new HashSet<>();
  83         for (Method m : methods) {
  84             methodNames.add(m.getName());
  85         }
  86         return methodNames.equals(allowedMethods());
  87     }
  88 
  89     public static void main(String[] args) {
  90         L la = BridgeMethod::bar; //static reference
  91         la.m("hi");
  92         Class<? extends L> c1 = la.getClass();
  93         Method[] methods = c1.getDeclaredMethods();
  94         assertTrue(matchingMethodNames(methods));
  95         Set<String> types = setOfStringObject();
  96         System.out.println("methods in SAM conversion of L:");
  97         for(Method m : methods) {
  98             if (m.getName().equals("m")) {
  99                 System.out.println(m.toGenericString());
 100                 Class[] parameterTypes = m.getParameterTypes();
 101                 assertTrue(parameterTypes.length == 1);
 102                 assertTrue(types.remove(parameterTypes[0].getName()));
 103             }
 104         }
 105         assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
 106 
 107         KM km = BridgeMethod::bar;
 108         //km.m("hi"); //will be uncommented when CR7028808 fixed
 109         Class<? extends KM> c2 = km.getClass();
 110         methods = c2.getDeclaredMethods();
 111         assertTrue(matchingMethodNames(methods));
 112         types = setOfStringObject();
 113         System.out.println("methods in SAM conversion of KM:");
 114         for(Method m : methods) {
 115             if (m.getName().equals("m")) {
 116                 System.out.println(m.toGenericString());
 117                 Class<?>[] parameterTypes = m.getParameterTypes();
 118                 assertTrue(parameterTypes.length == 1);
 119                 assertTrue(types.remove(parameterTypes[0].getName()));
 120             }
 121         }
 122         assertTrue(types.isEmpty());
 123 
 124         N n = new BridgeMethod()::moo; //instance reference
 125         assertTrue( n.m().equals("moo") );
 126         assertTrue( ((H)n).m().equals("moo") );
 127         Class<? extends N> c3 = n.getClass();
 128         methods = c3.getDeclaredMethods();
 129         types = setOfStringObject();
 130         System.out.println("methods in SAM conversion of N:");
 131         for(Method m : methods) {
 132             System.out.println(m.toGenericString());
 133             if (m.getName().equals("m")) {
 134                 Class<?> returnType = m.getReturnType();
 135                 assertTrue(types.remove(returnType.getName()));
 136             }
 137         }
 138         assertTrue(types.size() == 1); //there's a bridge
 139     }
 140 }