test/tools/javac/lambda/lambdaExpression/LambdaTest6.java

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

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1882
39ec5d8a691b
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 for certain SAM conversions
mcimadamore@1415 29 * @compile LambdaTest6.java
mcimadamore@1415 30 * @run main LambdaTest6
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 LambdaTest6<T> {
mcimadamore@1415 38
mcimadamore@1415 39 interface H {Object m();}
mcimadamore@1415 40
mcimadamore@1415 41 interface K<U> {void m(U element);}
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 b) {
mcimadamore@1415 52 if(!b)
mcimadamore@1415 53 throw new AssertionError();
mcimadamore@1415 54 }
mcimadamore@1415 55
mcimadamore@1415 56 private Set<String> setOfStringObject() {
mcimadamore@1415 57 Set<String> s = new HashSet<>();
mcimadamore@1415 58 s.add("java.lang.String");
mcimadamore@1415 59 s.add("java.lang.Object");
mcimadamore@1415 60 return s;
mcimadamore@1415 61 }
mcimadamore@1415 62
mcimadamore@1415 63 private void test1()
mcimadamore@1415 64 {
mcimadamore@1415 65 L la = s -> { };
mcimadamore@1415 66 la.m("hi");
mcimadamore@1415 67 Class<? extends L> c1 = la.getClass();
mcimadamore@1415 68 Method[] methods = c1.getDeclaredMethods();
mcimadamore@1415 69 Set<String> types = setOfStringObject();
mcimadamore@1415 70 for(Method m : methods) {
mcimadamore@1415 71 assertTrue(m.getName().equals("m"));
mcimadamore@1415 72 Class[] parameterTypes = m.getParameterTypes();
mcimadamore@1415 73 assertTrue(parameterTypes.length == 1);
mcimadamore@1415 74 assertTrue(types.remove(parameterTypes[0].getName()));
mcimadamore@1415 75 }
mcimadamore@1415 76 assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
mcimadamore@1415 77 }
mcimadamore@1415 78
mcimadamore@1415 79 private void test2()
mcimadamore@1415 80 {
mcimadamore@1415 81 KM km = s -> { };
mcimadamore@1415 82 //km.m("hi");
mcimadamore@1415 83 Class<? extends KM> c2 = km.getClass();
mcimadamore@1415 84 Method[] methods = c2.getDeclaredMethods();
mcimadamore@1415 85 Set<String> types = setOfStringObject();
mcimadamore@1415 86 for(Method m : methods) {
mcimadamore@1415 87 assertTrue(m.getName().equals("m"));
mcimadamore@1415 88 Class[] parameterTypes = m.getParameterTypes();
mcimadamore@1415 89 assertTrue(parameterTypes.length == 1);
mcimadamore@1415 90 assertTrue(types.remove(parameterTypes[0].getName()));
mcimadamore@1415 91 }
mcimadamore@1415 92 assertTrue(types.isEmpty());
mcimadamore@1415 93 }
mcimadamore@1415 94
mcimadamore@1415 95 private void test3()
mcimadamore@1415 96 {
mcimadamore@1415 97 N na = ()-> "hi";
mcimadamore@1415 98 assertTrue( na.m().equals("hi") );
mcimadamore@1415 99 assertTrue( ((H)na).m().equals("hi") );
mcimadamore@1415 100 Class<? extends N> c3 = na.getClass();
mcimadamore@1415 101 Method[] methods = c3.getDeclaredMethods();
mcimadamore@1415 102 Set<String> types = setOfStringObject();
mcimadamore@1415 103 for(Method m : methods) {
mcimadamore@1415 104 assertTrue(m.getName().equals("m"));
mcimadamore@1415 105 Class returnType = m.getReturnType();
mcimadamore@1415 106 assertTrue(types.remove(returnType.getName()));
mcimadamore@1415 107 }
mcimadamore@1882 108 assertTrue(types.size() == 1); //there's a bridge
mcimadamore@1415 109 }
mcimadamore@1415 110
mcimadamore@1415 111
mcimadamore@1415 112 public static void main(String[] args) {
mcimadamore@1415 113 LambdaTest6 test = new LambdaTest6();
mcimadamore@1415 114 test.test1();
mcimadamore@1415 115 test.test2();
mcimadamore@1415 116 test.test3();
mcimadamore@1415 117 }
mcimadamore@1415 118 }

mercurial