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 if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) { 79 s.add("get$Lambda"); 80 } 81 return s; 82 } 83 84 private static boolean matchingMethodNames(Method[] methods) { 85 Set<String> methodNames = new HashSet<>(); 86 for (Method m : methods) { 87 methodNames.add(m.getName()); 88 } 89 return methodNames.equals(allowedMethods()); 90 } 91 92 public static void main(String[] args) { 93 L la = BridgeMethod::bar; //static reference 94 la.m("hi"); 95 Class<? extends L> c1 = la.getClass(); 96 Method[] methods = c1.getDeclaredMethods(); 97 assertTrue(matchingMethodNames(methods)); 98 Set<String> types = setOfStringObject(); 99 System.out.println("methods in SAM conversion of L:"); 100 for(Method m : methods) { 101 if (m.getName().equals("m")) { 102 System.out.println(m.toGenericString()); 103 Class[] parameterTypes = m.getParameterTypes(); 104 assertTrue(parameterTypes.length == 1); 105 assertTrue(types.remove(parameterTypes[0].getName())); 106 } 107 } 108 assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String"))); 109 110 KM km = BridgeMethod::bar; 111 //km.m("hi"); //will be uncommented when CR7028808 fixed 112 Class<? extends KM> c2 = km.getClass(); 113 methods = c2.getDeclaredMethods(); 114 assertTrue(matchingMethodNames(methods)); 115 types = setOfStringObject(); 116 System.out.println("methods in SAM conversion of KM:"); 117 for(Method m : methods) { 118 if (m.getName().equals("m")) { 119 System.out.println(m.toGenericString()); 120 Class<?>[] parameterTypes = m.getParameterTypes(); 121 assertTrue(parameterTypes.length == 1); 122 assertTrue(types.remove(parameterTypes[0].getName())); 123 } 124 } 125 assertTrue(types.isEmpty()); 126 127 N n = new BridgeMethod()::moo; //instance reference 128 assertTrue( n.m().equals("moo") ); 129 assertTrue( ((H)n).m().equals("moo") ); 130 Class<? extends N> c3 = n.getClass(); 131 methods = c3.getDeclaredMethods(); 132 types = setOfStringObject(); 133 System.out.println("methods in SAM conversion of N:"); 134 for(Method m : methods) { 135 System.out.println(m.toGenericString()); 136 if (m.getName().equals("m")) { 137 Class<?> returnType = m.getReturnType(); 138 assertTrue(types.remove(returnType.getName())); 139 } 140 } 141 assertTrue(types.size() == 1); //there's a bridge 142 } 143 }