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 return s; 70 } 71 72 private static boolean matchingMethodNames(Method[] methods) { 73 Set<String> methodNames = new HashSet<>(); 74 for (Method m : methods) { 75 methodNames.add(m.getName()); 76 } 77 return methodNames.equals(allowedMethods()); 78 } 79 80 private void test1() 81 { 82 L la = s -> { }; 83 la.m("hi"); 84 Class<? extends L> c1 = la.getClass(); 85 Method[] methods = c1.getDeclaredMethods(); 86 assertTrue(matchingMethodNames(methods)); 87 Set<String> types = setOfStringObject(); 88 for(Method m : methods) { 89 if ("m".equals(m.getName())) { 90 Class[] parameterTypes = m.getParameterTypes(); 91 assertTrue(parameterTypes.length == 1); 92 assertTrue(types.remove(parameterTypes[0].getName())); 93 } 94 } 95 assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String"))); 96 } 97 98 private void test2() 99 { 100 KM km = s -> { }; 101 //km.m("hi"); 102 Class<? extends KM> c2 = km.getClass(); 103 Method[] methods = c2.getDeclaredMethods(); 104 assertTrue(matchingMethodNames(methods)); 105 Set<String> types = setOfStringObject(); 106 for(Method m : methods) { 107 if ("m".equals(m.getName())) { 108 Class[] parameterTypes = m.getParameterTypes(); 109 assertTrue(parameterTypes.length == 1); 110 assertTrue(types.remove(parameterTypes[0].getName())); 111 } 112 } 113 assertTrue(types.isEmpty()); 114 } 115 116 private void test3() 117 { 118 N na = ()-> "hi"; 119 assertTrue( na.m().equals("hi") ); 120 assertTrue( ((H)na).m().equals("hi") ); 121 Class<? extends N> c3 = na.getClass(); 122 Method[] methods = c3.getDeclaredMethods(); 123 assertTrue(matchingMethodNames(methods)); 124 Set<String> types = setOfStringObject(); 125 for(Method m : methods) { 126 if ("m".equals(m.getName())) { 127 Class returnType = m.getReturnType(); 128 assertTrue(types.remove(returnType.getName())); 129 } 130 } 131 assertTrue(types.size() == 1); //there's a bridge 132 } 133 134 135 public static void main(String[] args) { 136 LambdaTest6 test = new LambdaTest6(); 137 test.test1(); 138 test.test2(); 139 test.test3(); 140 } 141 }