1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/methodReference/BridgeMethod.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,118 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * @test 1.29 + * @bug 8003280 1.30 + * @summary Add lambda tests 1.31 + * Test bridge methods in certain SAM conversion 1.32 + * @compile BridgeMethod.java 1.33 + * @run main BridgeMethod 1.34 + */ 1.35 + 1.36 +import java.lang.reflect.Method; 1.37 +import java.util.HashSet; 1.38 +import java.util.Set; 1.39 + 1.40 +public class BridgeMethod { 1.41 + 1.42 + interface H {Object m();} 1.43 + 1.44 + interface K<T> {void m(T t);} 1.45 + 1.46 + interface L extends K<String> {} //generic substitution 1.47 + 1.48 + interface M {void m(String s);} 1.49 + 1.50 + interface KM extends K<String>, M{} //generic substitution 1.51 + 1.52 + interface N extends H {String m();} //covariant return 1.53 + 1.54 + private static void assertTrue(boolean cond) { 1.55 + if (!cond) 1.56 + throw new AssertionError(); 1.57 + } 1.58 + 1.59 + static void bar(String s) { 1.60 + System.out.println("BridgeMethod.bar(String) " + s); 1.61 + } 1.62 + 1.63 + String moo() { 1.64 + return "moo"; 1.65 + } 1.66 + 1.67 + private static Set<String> setOfStringObject() { 1.68 + Set<String> s = new HashSet<>(); 1.69 + s.add("java.lang.String"); 1.70 + s.add("java.lang.Object"); 1.71 + return s; 1.72 + } 1.73 + 1.74 + public static void main(String[] args) { 1.75 + L la = BridgeMethod::bar; //static reference 1.76 + la.m("hi"); 1.77 + Class<? extends L> c1 = la.getClass(); 1.78 + Method[] methods = c1.getDeclaredMethods(); 1.79 + Set<String> types = setOfStringObject(); 1.80 + System.out.println("methods in SAM conversion of L:"); 1.81 + for(Method m : methods) { 1.82 + System.out.println(m.toGenericString()); 1.83 + assertTrue(m.getName().equals("m")); 1.84 + Class[] parameterTypes = m.getParameterTypes(); 1.85 + assertTrue(parameterTypes.length == 1); 1.86 + assertTrue(types.remove(parameterTypes[0].getName())); 1.87 + } 1.88 + assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String"))); 1.89 + 1.90 + KM km = BridgeMethod::bar; 1.91 + //km.m("hi"); //will be uncommented when CR7028808 fixed 1.92 + Class<? extends KM> c2 = km.getClass(); 1.93 + methods = c2.getDeclaredMethods(); 1.94 + types = setOfStringObject(); 1.95 + System.out.println("methods in SAM conversion of KM:"); 1.96 + for(Method m : methods) { 1.97 + System.out.println(m.toGenericString()); 1.98 + assertTrue(m.getName().equals("m")); 1.99 + Class<?>[] parameterTypes = m.getParameterTypes(); 1.100 + assertTrue(parameterTypes.length == 1); 1.101 + assertTrue(types.remove(parameterTypes[0].getName())); 1.102 + } 1.103 + assertTrue(types.isEmpty()); 1.104 + 1.105 + N n = new BridgeMethod()::moo; //instance reference 1.106 + assertTrue( n.m().equals("moo") ); 1.107 + assertTrue( ((H)n).m().equals("moo") ); 1.108 + Class<? extends N> c3 = n.getClass(); 1.109 + methods = c3.getDeclaredMethods(); 1.110 + types = setOfStringObject(); 1.111 + System.out.println("methods in SAM conversion of N:"); 1.112 + for(Method m : methods) { 1.113 + System.out.println(m.toGenericString()); 1.114 + if (m.getName().equals("m")) { 1.115 + Class<?> returnType = m.getReturnType(); 1.116 + assertTrue(types.remove(returnType.getName())); 1.117 + } 1.118 + } 1.119 + assertTrue(types.size() == 1); //there's a bridge 1.120 + } 1.121 +}