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

     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  */
    24 /**
    25  * @test
    26  * @bug 8003280
    27  * @summary Add lambda tests
    28  *   Test bridge methods for certain SAM conversions
    29  * @compile LambdaTest6.java
    30  * @run main LambdaTest6
    31  */
    33 import java.lang.reflect.Method;
    34 import java.util.HashSet;
    35 import java.util.Set;
    37 public class LambdaTest6<T> {
    39     interface H {Object m();}
    41     interface K<U> {void m(U element);}
    43     interface L extends K<String> {} //generic substitution
    45     interface M {void m(String s);}
    47     interface KM extends K<String>, M{} //generic substitution
    49     interface N extends H {String m();} //covariant return
    51     private static void assertTrue(boolean b) {
    52         if(!b)
    53             throw new AssertionError();
    54     }
    56     private Set<String> setOfStringObject() {
    57         Set<String> s = new HashSet<>();
    58         s.add("java.lang.String");
    59         s.add("java.lang.Object");
    60         return s;
    61     }
    63     private void test1()
    64     {
    65         L la = s -> { };
    66         la.m("hi");
    67         Class<? extends L> c1 = la.getClass();
    68         Method[] methods = c1.getDeclaredMethods();
    69         Set<String> types = setOfStringObject();
    70         for(Method m : methods) {
    71             assertTrue(m.getName().equals("m"));
    72             Class[] parameterTypes = m.getParameterTypes();
    73             assertTrue(parameterTypes.length == 1);
    74             assertTrue(types.remove(parameterTypes[0].getName()));
    75         }
    76         assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
    77     }
    79     private void test2()
    80     {
    81         KM km = s -> { };
    82         //km.m("hi");
    83         Class<? extends KM> c2 = km.getClass();
    84         Method[] methods = c2.getDeclaredMethods();
    85         Set<String> types = setOfStringObject();
    86         for(Method m : methods) {
    87             assertTrue(m.getName().equals("m"));
    88             Class[] parameterTypes = m.getParameterTypes();
    89             assertTrue(parameterTypes.length == 1);
    90             assertTrue(types.remove(parameterTypes[0].getName()));
    91         }
    92         assertTrue(types.isEmpty());
    93     }
    95     private void test3()
    96     {
    97         N na = ()-> "hi";
    98         assertTrue( na.m().equals("hi") );
    99         assertTrue( ((H)na).m().equals("hi") );
   100         Class<? extends N> c3 = na.getClass();
   101         Method[] methods = c3.getDeclaredMethods();
   102         Set<String> types = setOfStringObject();
   103         for(Method m : methods) {
   104             assertTrue(m.getName().equals("m"));
   105             Class returnType = m.getReturnType();
   106             assertTrue(types.remove(returnType.getName()));
   107         }
   108         assertTrue(types.size() == 1); //there's a bridge
   109     }
   112     public static void main(String[] args) {
   113         LambdaTest6 test = new LambdaTest6();
   114         test.test1();
   115         test.test2();
   116         test.test3();
   117     }
   118 }

mercurial