test/tools/javac/lambda/typeInference/InferenceTest2.java

Tue, 14 May 2013 11:11:09 -0700

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 0
959103a6100f
permissions
-rw-r--r--

8012556: Implement lambda methods on interfaces as static
8006140: Javac NPE compiling Lambda expression on initialization expression of static field in interface
Summary: Lambdas occurring in static contexts or those not needing instance information should be generated into static methods. This has long been the case for classes. However, as a work-around to the lack of support for statics on interfaces, interface lambda methods have been generated into default methods. For lambdas in interface static contexts (fields and static methods) this causes an NPE in javac because there is no 'this'. MethodHandles now support static methods on interfaces. This changeset allows lambda methods to be generated as static interface methods. An existing bug in Hotspot (8013875) is exposed in a test when the "-esa" flag is used. This test and another test that already exposed this bug have been marked with @ignore.
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 2011, 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 import java.util.List;
    25 import java.util.ArrayList;
    26 import java.util.Arrays;
    27 import java.io.Serializable;
    28 import java.io.File;
    30 /**
    31  * @test
    32  * @bug 8003280
    33  * @summary Add lambda tests
    34  *   Parameter types inferred from target type in generics without wildcard
    35  * @compile InferenceTest2.java
    36  * @run main InferenceTest2
    37  */
    39 public class InferenceTest2 {
    41     private static void assertTrue(boolean cond) {
    42         if (!cond)
    43             throw new AssertionError();
    44     }
    46     public static void main(String[] args) {
    48         InferenceTest2 test = new InferenceTest2();
    50         //test SAM1<T>
    51         SAM1<String> sam1 = para -> { String result = "";
    52                                       for(String s : para)
    53                                           if(s.compareTo(result) > 0)
    54                                               result = s;
    55                                       return result; };
    56         List<String> list = Arrays.asList("a", "b", "c");
    57         assertTrue(sam1.m1(list).equals("c"));
    59         test.method1(para -> para.get(0));
    61         //test SAM2<T>
    62         SAM2<String> sam2 = para -> {para = para.substring(0);};
    63         SAM2<Double> sam2_2 = para -> {};
    64         SAM2<File> sam2_3 = para -> { if(para.isDirectory())
    65                                           System.out.println("directory");
    66                                     };
    68         //test SAM3<T>
    69         SAM3<String> sam3 = para -> para[0].substring(0, para[0].length()-1);
    70         assertTrue(sam3.m3("hello+").equals("hello"));
    72         SAM3<Integer> sam3_2 = para -> para[0] - para[1];
    73         assertTrue(sam3_2.m3(1, -1) == 2);
    75         SAM3<Double> sam3_3 = para -> para[0] + para[1] + para[2] + para[3];
    76         assertTrue(sam3_3.m3(1.0, 2.0, 3.0, 4.0) == 10.0);
    78         test.method3(para -> para[0] + 1);
    80         //test SAM6<T>
    81         SAM6<String> sam6 = (para1, para2) -> para1.concat(para2);
    82         assertTrue(sam6.m6("hello", "world").equals("helloworld"));
    84         test.method6((para1, para2) -> para1 >= para2? para1 : para2);
    85     }
    87     void method1(SAM1<Integer> sam1) {
    88         List<Integer> list = Arrays.asList(3,2,1);
    89         assertTrue(sam1.m1(list) == 3);
    90     }
    92     void method3(SAM3<Double> sam3) {
    93         assertTrue(sam3.m3(2.5) == 3.5);
    94     }
    96     void method6(SAM6<Long> sam6) {
    97         assertTrue(sam6.m6(5L, -5L) == 5);
    98     }
   100     interface SAM1<T> {
   101         T m1(List<T> x);
   102     }
   104     interface SAM2<T extends Serializable> {
   105         void m2(T x);
   106     }
   108     interface SAM3<T> {
   109         T m3(T... x);
   110     }
   112     interface SAM6<T> {
   113         T m6(T a, T b);
   114     }
   115 }

mercurial