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 */ 23 24import java.util.List; 25import java.util.ArrayList; 26import java.util.Arrays; 27import java.io.Serializable; 28import java.io.File; 29 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 */ 38 39public class InferenceTest2 { 40 41 private static void assertTrue(boolean cond) { 42 if (!cond) 43 throw new AssertionError(); 44 } 45 46 public static void main(String[] args) { 47 48 InferenceTest2 test = new InferenceTest2(); 49 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")); 58 59 test.method1(para -> para.get(0)); 60 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 }; 67 68 //test SAM3<T> 69 SAM3<String> sam3 = para -> para[0].substring(0, para[0].length()-1); 70 assertTrue(sam3.m3("hello+").equals("hello")); 71 72 SAM3<Integer> sam3_2 = para -> para[0] - para[1]; 73 assertTrue(sam3_2.m3(1, -1) == 2); 74 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); 77 78 test.method3(para -> para[0] + 1); 79 80 //test SAM6<T> 81 SAM6<String> sam6 = (para1, para2) -> para1.concat(para2); 82 assertTrue(sam6.m6("hello", "world").equals("helloworld")); 83 84 test.method6((para1, para2) -> para1 >= para2? para1 : para2); 85 } 86 87 void method1(SAM1<Integer> sam1) { 88 List<Integer> list = Arrays.asList(3,2,1); 89 assertTrue(sam1.m1(list) == 3); 90 } 91 92 void method3(SAM3<Double> sam3) { 93 assertTrue(sam3.m3(2.5) == 3.5); 94 } 95 96 void method6(SAM6<Long> sam6) { 97 assertTrue(sam6.m6(5L, -5L) == 5); 98 } 99 100 interface SAM1<T> { 101 T m1(List<T> x); 102 } 103 104 interface SAM2<T extends Serializable> { 105 void m2(T x); 106 } 107 108 interface SAM3<T> { 109 T m3(T... x); 110 } 111 112 interface SAM6<T> { 113 T m6(T a, T b); 114 } 115} 116