aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 8003280 aoqi@0: * @summary Add lambda tests aoqi@0: * This test is for identifying SAM types #4, see Helper.java for SAM types aoqi@0: * @compile LambdaTest2_SAM2.java Helper.java aoqi@0: * @run main LambdaTest2_SAM2 aoqi@0: */ aoqi@0: aoqi@0: import java.util.Collection; aoqi@0: import java.util.List; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.concurrent.TimeoutException; aoqi@0: import java.io.*; aoqi@0: import java.sql.SQLException; aoqi@0: import java.sql.SQLTransientException; aoqi@0: aoqi@0: public class LambdaTest2_SAM2 { aoqi@0: private static List strs = new ArrayList(); aoqi@0: aoqi@0: public static void main(String[] args) { aoqi@0: strs.add("copy"); aoqi@0: strs.add("paste"); aoqi@0: strs.add("delete"); aoqi@0: strs.add("rename"); aoqi@0: aoqi@0: LambdaTest2_SAM2 test = new LambdaTest2_SAM2(); aoqi@0: aoqi@0: //type #4 a): aoqi@0: test.methodAB((List list) -> 100); aoqi@0: aoqi@0: //type #4 b): aoqi@0: test.methodFGHI((String s) -> new Integer(22)); aoqi@0: //type #4 b): aoqi@0: test.methodJK((String s) -> new ArrayList()); aoqi@0: test.methodJK((String s) -> new ArrayList()); aoqi@0: //type #4 b): aoqi@0: test.methodJL((String s) -> new ArrayList()); aoqi@0: test.methodJL((String s) -> new ArrayList()); aoqi@0: //type #4 b): aoqi@0: test.methodJKL((String s) -> new ArrayList()); aoqi@0: test.methodJKL((String s) -> new ArrayList()); aoqi@0: //type #4 b): aoqi@0: test.methodJKLM((String s) -> new ArrayList()); aoqi@0: test.methodJKLM((String s) -> new ArrayList()); aoqi@0: aoqi@0: // tyep #4 c): aoqi@0: test.methodNO((File f) -> { aoqi@0: String temp = null; aoqi@0: StringBuffer sb = new StringBuffer(); aoqi@0: try aoqi@0: { aoqi@0: BufferedReader br = new BufferedReader(new FileReader(f)); aoqi@0: while((temp=br.readLine()) != null) aoqi@0: sb.append(temp).append("\n"); aoqi@0: } aoqi@0: catch(FileNotFoundException fne){throw fne;} aoqi@0: catch(IOException e){e.printStackTrace();} aoqi@0: return sb.toString(); aoqi@0: }); aoqi@0: // tyep #4 c): aoqi@0: test.methodNOP((File f) -> { aoqi@0: String temp = null; aoqi@0: StringBuffer sb = new StringBuffer(); aoqi@0: try aoqi@0: { aoqi@0: BufferedReader br = new BufferedReader(new FileReader(f)); aoqi@0: while((temp=br.readLine()) != null) aoqi@0: sb.append(temp).append("\n"); aoqi@0: } aoqi@0: catch(IOException e){e.printStackTrace();} aoqi@0: return sb.toString(); aoqi@0: }); aoqi@0: // type #4 c): aoqi@0: test.methodBooDoo((String s) -> s.length()); aoqi@0: aoqi@0: //type #4 d): aoqi@0: test.methodQR((Iterable i) -> new ArrayList()); aoqi@0: test.methodQR((Iterable i) -> new ArrayList()); aoqi@0: //type #4 d): aoqi@0: test.methodUV((List list) -> { aoqi@0: test.exceptionMethod1(); aoqi@0: test.exceptionMethod2(); aoqi@0: return new ArrayList(); aoqi@0: }); aoqi@0: test.methodUV((List list) -> { aoqi@0: test.exceptionMethod1(); aoqi@0: test.exceptionMethod2(); aoqi@0: return new ArrayList(); aoqi@0: }); aoqi@0: //type #4 d): aoqi@0: test.methodUVW((List list) -> { aoqi@0: test.exceptionMethod1(); aoqi@0: test.exceptionMethod2(); aoqi@0: return new ArrayList(); aoqi@0: }); aoqi@0: test.methodUVW((List list) -> { aoqi@0: test.exceptionMethod1(); aoqi@0: test.exceptionMethod2(); aoqi@0: return new ArrayList(); aoqi@0: }); aoqi@0: } aoqi@0: aoqi@0: private void exceptionMethod1() throws EOFException{ aoqi@0: } aoqi@0: aoqi@0: private void exceptionMethod2() throws SQLTransientException{ aoqi@0: } aoqi@0: aoqi@0: //type #4 a): SAM type ([List], int, {}) aoqi@0: void methodAB (AB ab) { aoqi@0: System.out.println("methodAB(): SAM type interface AB object instantiated: " + ab); aoqi@0: System.out.println(ab.getOldest(strs)); aoqi@0: } aoqi@0: aoqi@0: //type #4 b): SAM type ([String], Integer, {}) aoqi@0: void methodFGHI(FGHI f) { aoqi@0: System.out.println("methodFGHI(): SAM type interface FGHI object instantiated: " + f); aoqi@0: System.out.println(f.getValue("str")); aoqi@0: } aoqi@0: aoqi@0: //type #4 b): SAM type ([String], List, {}) aoqi@0: void methodJK(JK jk) { aoqi@0: System.out.println("methodJK(): SAM type interface JK object instantiated: " + jk); aoqi@0: for(Number n : jk.getAll("in")) aoqi@0: System.out.println(n); aoqi@0: } aoqi@0: aoqi@0: //type #4 b): SAM type ([String], List, {}) aoqi@0: void methodJL(JL jl) { aoqi@0: System.out.println("methodJL(): SAM type interface JL object instantiated: " + jl); aoqi@0: for(Number n : ((J)jl).getAll("in")) //cast should be redundant - see 7062745 aoqi@0: System.out.println(n); aoqi@0: } aoqi@0: aoqi@0: //type #4 b): SAM type ([String], List, {}) aoqi@0: void methodJKL(JKL jkl) { //commented - see 7062745 aoqi@0: System.out.println("methodJKL(): SAM type interface JKL object instantiated: " + jkl); aoqi@0: for(Number n : ((J)jkl).getAll("in")) aoqi@0: System.out.println(n); aoqi@0: } aoqi@0: aoqi@0: //type #4 b): SAM type ([String], List, {}) aoqi@0: void methodJKLM(JKLM jklm) { //commented - see 7062745 aoqi@0: System.out.println("methodJKLM(): SAM type interface JKLM object instantiated: " + jklm); aoqi@0: for(Number n : ((J)jklm).getAll("in")) aoqi@0: System.out.println(n); aoqi@0: } aoqi@0: aoqi@0: //type #4 c): SAM type ([File], String, {FileNotFoundException}) aoqi@0: void methodNO(NO no) { aoqi@0: System.out.println("methodNO(): SAM type interface \"NO\" object instantiated: " + no); aoqi@0: try { aoqi@0: System.out.println("text=" + no.getText(new File("a.txt"))); aoqi@0: System.out.println("got here, no exception thrown"); aoqi@0: } aoqi@0: catch(FileNotFoundException e){e.printStackTrace();} aoqi@0: } aoqi@0: aoqi@0: //type #4 c): SAM type ([File]), String, {}) aoqi@0: void methodNOP(NOP nop) { aoqi@0: System.out.println("methodNOP(): SAM type interface \"NOP\" object instantiated: " + nop); aoqi@0: System.out.println("text=" + nop.getText(new File("a.txt"))); aoqi@0: } aoqi@0: aoqi@0: //type #4 c): SAM type ([String], int, {}) aoqi@0: void methodBooDoo(BooDoo bd) { aoqi@0: System.out.println("methodBooDoo(): SAM type interface BooDoo object instantiated: " + bd); aoqi@0: System.out.println("result=" + bd.getAge("lambda")); aoqi@0: } aoqi@0: aoqi@0: //type #4 d): SAM type ([Iterable], Iterable, {}) aoqi@0: void methodQR(QR qr) { aoqi@0: System.out.println("methodQR(): SAM type interface QR object instantiated: " + qr); aoqi@0: System.out.println("Iterable returned: " + qr.m(new SQLException())); aoqi@0: } aoqi@0: aoqi@0: //type #4 d): SAM type ([List], List/List, {EOFException, SQLTransientException}) aoqi@0: void methodUV(UV uv) { aoqi@0: System.out.println("methodUV(): SAM type interface UV object instantiated: " + uv); aoqi@0: try{ aoqi@0: System.out.println("result returned: " + uv.foo(strs)); aoqi@0: }catch(EOFException e){ aoqi@0: System.out.println(e.getMessage()); aoqi@0: }catch(SQLTransientException ex){ aoqi@0: System.out.println(ex.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //type #4 d): SAM type ([List], List/List, {EOFException, SQLTransientException}) aoqi@0: void methodUVW(UVW uvw) { aoqi@0: System.out.println("methodUVW(): SAM type interface UVW object instantiated: " + uvw); aoqi@0: try{ aoqi@0: System.out.println("passing List: " + uvw.foo(strs)); aoqi@0: System.out.println("passing List: " + uvw.foo(new ArrayList())); aoqi@0: }catch(EOFException e){ aoqi@0: System.out.println(e.getMessage()); aoqi@0: }catch(SQLTransientException ex){ aoqi@0: System.out.println(ex.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: }