1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/funcInterfaces/LambdaTest2_SAM2.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,225 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * @test 1.29 + * @bug 8003280 1.30 + * @summary Add lambda tests 1.31 + * This test is for identifying SAM types #4, see Helper.java for SAM types 1.32 + * @compile LambdaTest2_SAM2.java Helper.java 1.33 + * @run main LambdaTest2_SAM2 1.34 + */ 1.35 + 1.36 +import java.util.Collection; 1.37 +import java.util.List; 1.38 +import java.util.ArrayList; 1.39 +import java.util.concurrent.TimeoutException; 1.40 +import java.io.*; 1.41 +import java.sql.SQLException; 1.42 +import java.sql.SQLTransientException; 1.43 + 1.44 +public class LambdaTest2_SAM2 { 1.45 + private static List<String> strs = new ArrayList<String>(); 1.46 + 1.47 + public static void main(String[] args) { 1.48 + strs.add("copy"); 1.49 + strs.add("paste"); 1.50 + strs.add("delete"); 1.51 + strs.add("rename"); 1.52 + 1.53 + LambdaTest2_SAM2 test = new LambdaTest2_SAM2(); 1.54 + 1.55 + //type #4 a): 1.56 + test.methodAB((List list) -> 100); 1.57 + 1.58 + //type #4 b): 1.59 + test.methodFGHI((String s) -> new Integer(22)); 1.60 + //type #4 b): 1.61 + test.methodJK((String s) -> new ArrayList<Number>()); 1.62 + test.methodJK((String s) -> new ArrayList()); 1.63 + //type #4 b): 1.64 + test.methodJL((String s) -> new ArrayList<Number>()); 1.65 + test.methodJL((String s) -> new ArrayList()); 1.66 + //type #4 b): 1.67 + test.methodJKL((String s) -> new ArrayList<Number>()); 1.68 + test.methodJKL((String s) -> new ArrayList()); 1.69 + //type #4 b): 1.70 + test.methodJKLM((String s) -> new ArrayList<Number>()); 1.71 + test.methodJKLM((String s) -> new ArrayList()); 1.72 + 1.73 + // tyep #4 c): 1.74 + test.methodNO((File f) -> { 1.75 + String temp = null; 1.76 + StringBuffer sb = new StringBuffer(); 1.77 + try 1.78 + { 1.79 + BufferedReader br = new BufferedReader(new FileReader(f)); 1.80 + while((temp=br.readLine()) != null) 1.81 + sb.append(temp).append("\n"); 1.82 + } 1.83 + catch(FileNotFoundException fne){throw fne;} 1.84 + catch(IOException e){e.printStackTrace();} 1.85 + return sb.toString(); 1.86 + }); 1.87 + // tyep #4 c): 1.88 + test.methodNOP((File f) -> { 1.89 + String temp = null; 1.90 + StringBuffer sb = new StringBuffer(); 1.91 + try 1.92 + { 1.93 + BufferedReader br = new BufferedReader(new FileReader(f)); 1.94 + while((temp=br.readLine()) != null) 1.95 + sb.append(temp).append("\n"); 1.96 + } 1.97 + catch(IOException e){e.printStackTrace();} 1.98 + return sb.toString(); 1.99 + }); 1.100 + // type #4 c): 1.101 + test.methodBooDoo((String s) -> s.length()); 1.102 + 1.103 + //type #4 d): 1.104 + test.methodQR((Iterable i) -> new ArrayList<String>()); 1.105 + test.methodQR((Iterable i) -> new ArrayList()); 1.106 + //type #4 d): 1.107 + test.methodUV((List<String> list) -> { 1.108 + test.exceptionMethod1(); 1.109 + test.exceptionMethod2(); 1.110 + return new ArrayList<String>(); 1.111 + }); 1.112 + test.methodUV((List<String> list) -> { 1.113 + test.exceptionMethod1(); 1.114 + test.exceptionMethod2(); 1.115 + return new ArrayList(); 1.116 + }); 1.117 + //type #4 d): 1.118 + test.methodUVW((List list) -> { 1.119 + test.exceptionMethod1(); 1.120 + test.exceptionMethod2(); 1.121 + return new ArrayList<String>(); 1.122 + }); 1.123 + test.methodUVW((List list) -> { 1.124 + test.exceptionMethod1(); 1.125 + test.exceptionMethod2(); 1.126 + return new ArrayList(); 1.127 + }); 1.128 + } 1.129 + 1.130 + private void exceptionMethod1() throws EOFException{ 1.131 + } 1.132 + 1.133 + private void exceptionMethod2() throws SQLTransientException{ 1.134 + } 1.135 + 1.136 + //type #4 a): SAM type ([List], int, {}) 1.137 + void methodAB (AB ab) { 1.138 + System.out.println("methodAB(): SAM type interface AB object instantiated: " + ab); 1.139 + System.out.println(ab.getOldest(strs)); 1.140 + } 1.141 + 1.142 + //type #4 b): SAM type ([String], Integer, {}) 1.143 + void methodFGHI(FGHI f) { 1.144 + System.out.println("methodFGHI(): SAM type interface FGHI object instantiated: " + f); 1.145 + System.out.println(f.getValue("str")); 1.146 + } 1.147 + 1.148 + //type #4 b): SAM type ([String], List<Number>, {}) 1.149 + void methodJK(JK jk) { 1.150 + System.out.println("methodJK(): SAM type interface JK object instantiated: " + jk); 1.151 + for(Number n : jk.getAll("in")) 1.152 + System.out.println(n); 1.153 + } 1.154 + 1.155 + //type #4 b): SAM type ([String], List<Number>, {}) 1.156 + void methodJL(JL jl) { 1.157 + System.out.println("methodJL(): SAM type interface JL object instantiated: " + jl); 1.158 + for(Number n : ((J)jl).getAll("in")) //cast should be redundant - see 7062745 1.159 + System.out.println(n); 1.160 + } 1.161 + 1.162 + //type #4 b): SAM type ([String], List<Number>, {}) 1.163 + void methodJKL(JKL jkl) { //commented - see 7062745 1.164 + System.out.println("methodJKL(): SAM type interface JKL object instantiated: " + jkl); 1.165 + for(Number n : ((J)jkl).getAll("in")) 1.166 + System.out.println(n); 1.167 + } 1.168 + 1.169 + //type #4 b): SAM type ([String], List<Number>, {}) 1.170 + void methodJKLM(JKLM jklm) { //commented - see 7062745 1.171 + System.out.println("methodJKLM(): SAM type interface JKLM object instantiated: " + jklm); 1.172 + for(Number n : ((J)jklm).getAll("in")) 1.173 + System.out.println(n); 1.174 + } 1.175 + 1.176 + //type #4 c): SAM type ([File], String, {FileNotFoundException}) 1.177 + void methodNO(NO no) { 1.178 + System.out.println("methodNO(): SAM type interface \"NO\" object instantiated: " + no); 1.179 + try { 1.180 + System.out.println("text=" + no.getText(new File("a.txt"))); 1.181 + System.out.println("got here, no exception thrown"); 1.182 + } 1.183 + catch(FileNotFoundException e){e.printStackTrace();} 1.184 + } 1.185 + 1.186 + //type #4 c): SAM type ([File]), String, {}) 1.187 + void methodNOP(NOP nop) { 1.188 + System.out.println("methodNOP(): SAM type interface \"NOP\" object instantiated: " + nop); 1.189 + System.out.println("text=" + nop.getText(new File("a.txt"))); 1.190 + } 1.191 + 1.192 + //type #4 c): SAM type ([String], int, {}) 1.193 + void methodBooDoo(BooDoo bd) { 1.194 + System.out.println("methodBooDoo(): SAM type interface BooDoo object instantiated: " + bd); 1.195 + System.out.println("result=" + bd.getAge("lambda")); 1.196 + } 1.197 + 1.198 + //type #4 d): SAM type ([Iterable], Iterable<String>, {}) 1.199 + void methodQR(QR qr) { 1.200 + System.out.println("methodQR(): SAM type interface QR object instantiated: " + qr); 1.201 + System.out.println("Iterable returned: " + qr.m(new SQLException())); 1.202 + } 1.203 + 1.204 + //type #4 d): SAM type ([List<String>], List<String>/List, {EOFException, SQLTransientException}) 1.205 + void methodUV(UV uv) { 1.206 + System.out.println("methodUV(): SAM type interface UV object instantiated: " + uv); 1.207 + try{ 1.208 + System.out.println("result returned: " + uv.foo(strs)); 1.209 + }catch(EOFException e){ 1.210 + System.out.println(e.getMessage()); 1.211 + }catch(SQLTransientException ex){ 1.212 + System.out.println(ex.getMessage()); 1.213 + } 1.214 + } 1.215 + 1.216 + //type #4 d): SAM type ([List], List<String>/List, {EOFException, SQLTransientException}) 1.217 + void methodUVW(UVW uvw) { 1.218 + System.out.println("methodUVW(): SAM type interface UVW object instantiated: " + uvw); 1.219 + try{ 1.220 + System.out.println("passing List<String>: " + uvw.foo(strs)); 1.221 + System.out.println("passing List: " + uvw.foo(new ArrayList())); 1.222 + }catch(EOFException e){ 1.223 + System.out.println(e.getMessage()); 1.224 + }catch(SQLTransientException ex){ 1.225 + System.out.println(ex.getMessage()); 1.226 + } 1.227 + } 1.228 +}