test/tools/javac/lambda/methodReference/BridgeMethod.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 2074
dee28dd47e12
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

mcimadamore@1415 1 /*
ksrini@2227 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1415 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1415 4 *
mcimadamore@1415 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1415 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1415 7 * published by the Free Software Foundation.
mcimadamore@1415 8 *
mcimadamore@1415 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1415 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1415 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1415 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1415 13 * accompanied this code).
mcimadamore@1415 14 *
mcimadamore@1415 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1415 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1415 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1415 18 *
mcimadamore@1415 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1415 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1415 21 * questions.
mcimadamore@1415 22 */
mcimadamore@1415 23
mcimadamore@1415 24 /**
mcimadamore@1415 25 * @test
mcimadamore@1415 26 * @bug 8003280
mcimadamore@1415 27 * @summary Add lambda tests
mcimadamore@1415 28 * Test bridge methods in certain SAM conversion
mcimadamore@1415 29 * @compile BridgeMethod.java
mcimadamore@1415 30 * @run main BridgeMethod
mcimadamore@1415 31 */
mcimadamore@1415 32
mcimadamore@1415 33 import java.lang.reflect.Method;
mcimadamore@1415 34 import java.util.HashSet;
mcimadamore@1415 35 import java.util.Set;
mcimadamore@1415 36
mcimadamore@1415 37 public class BridgeMethod {
mcimadamore@1415 38
mcimadamore@1415 39 interface H {Object m();}
mcimadamore@1415 40
mcimadamore@1415 41 interface K<T> {void m(T t);}
mcimadamore@1415 42
mcimadamore@1415 43 interface L extends K<String> {} //generic substitution
mcimadamore@1415 44
mcimadamore@1415 45 interface M {void m(String s);}
mcimadamore@1415 46
mcimadamore@1415 47 interface KM extends K<String>, M{} //generic substitution
mcimadamore@1415 48
mcimadamore@1415 49 interface N extends H {String m();} //covariant return
mcimadamore@1415 50
mcimadamore@1415 51 private static void assertTrue(boolean cond) {
mcimadamore@1415 52 if (!cond)
mcimadamore@1415 53 throw new AssertionError();
mcimadamore@1415 54 }
mcimadamore@1415 55
mcimadamore@1415 56 static void bar(String s) {
mcimadamore@1415 57 System.out.println("BridgeMethod.bar(String) " + s);
mcimadamore@1415 58 }
mcimadamore@1415 59
mcimadamore@1415 60 String moo() {
mcimadamore@1415 61 return "moo";
mcimadamore@1415 62 }
mcimadamore@1415 63
mcimadamore@1415 64 private static Set<String> setOfStringObject() {
mcimadamore@1415 65 Set<String> s = new HashSet<>();
mcimadamore@1415 66 s.add("java.lang.String");
mcimadamore@1415 67 s.add("java.lang.Object");
mcimadamore@1415 68 return s;
mcimadamore@1415 69 }
mcimadamore@1415 70
mcimadamore@1415 71 public static void main(String[] args) {
mcimadamore@1415 72 L la = BridgeMethod::bar; //static reference
mcimadamore@1415 73 la.m("hi");
mcimadamore@1415 74 Class<? extends L> c1 = la.getClass();
mcimadamore@1415 75 Method[] methods = c1.getDeclaredMethods();
mcimadamore@1415 76 Set<String> types = setOfStringObject();
mcimadamore@1415 77 System.out.println("methods in SAM conversion of L:");
mcimadamore@1415 78 for(Method m : methods) {
mcimadamore@1415 79 System.out.println(m.toGenericString());
mcimadamore@1415 80 assertTrue(m.getName().equals("m"));
mcimadamore@1415 81 Class[] parameterTypes = m.getParameterTypes();
mcimadamore@1415 82 assertTrue(parameterTypes.length == 1);
mcimadamore@1415 83 assertTrue(types.remove(parameterTypes[0].getName()));
mcimadamore@1415 84 }
mcimadamore@1415 85 assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
mcimadamore@1415 86
mcimadamore@1415 87 KM km = BridgeMethod::bar;
mcimadamore@1415 88 //km.m("hi"); //will be uncommented when CR7028808 fixed
mcimadamore@1415 89 Class<? extends KM> c2 = km.getClass();
mcimadamore@1415 90 methods = c2.getDeclaredMethods();
mcimadamore@1415 91 types = setOfStringObject();
mcimadamore@1415 92 System.out.println("methods in SAM conversion of KM:");
mcimadamore@1415 93 for(Method m : methods) {
mcimadamore@1415 94 System.out.println(m.toGenericString());
mcimadamore@1415 95 assertTrue(m.getName().equals("m"));
mcimadamore@1415 96 Class<?>[] parameterTypes = m.getParameterTypes();
mcimadamore@1415 97 assertTrue(parameterTypes.length == 1);
mcimadamore@1415 98 assertTrue(types.remove(parameterTypes[0].getName()));
mcimadamore@1415 99 }
mcimadamore@1415 100 assertTrue(types.isEmpty());
mcimadamore@1415 101
mcimadamore@1415 102 N n = new BridgeMethod()::moo; //instance reference
mcimadamore@1415 103 assertTrue( n.m().equals("moo") );
mcimadamore@1415 104 assertTrue( ((H)n).m().equals("moo") );
mcimadamore@1415 105 Class<? extends N> c3 = n.getClass();
mcimadamore@1415 106 methods = c3.getDeclaredMethods();
mcimadamore@1415 107 types = setOfStringObject();
mcimadamore@1415 108 System.out.println("methods in SAM conversion of N:");
mcimadamore@1415 109 for(Method m : methods) {
mcimadamore@1415 110 System.out.println(m.toGenericString());
rfield@2074 111 if (m.getName().equals("m")) {
rfield@2074 112 Class<?> returnType = m.getReturnType();
rfield@2074 113 assertTrue(types.remove(returnType.getName()));
rfield@2074 114 }
mcimadamore@1415 115 }
mcimadamore@1882 116 assertTrue(types.size() == 1); //there's a bridge
mcimadamore@1415 117 }
mcimadamore@1415 118 }

mercurial