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