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: * Test lambda expressions for different method signatures (parameter and return type) aoqi@0: * @compile LambdaTest2.java aoqi@0: * @run main LambdaTest2 aoqi@0: */ aoqi@0: aoqi@0: public class LambdaTest2 { aoqi@0: aoqi@0: private static int count = 0; aoqi@0: aoqi@0: private static void assertTrue(boolean b) { aoqi@0: if(!b) aoqi@0: throw new AssertionError(); aoqi@0: } aoqi@0: aoqi@0: public static void main(String[] args) { aoqi@0: LambdaTest2 test = new LambdaTest2(); aoqi@0: aoqi@0: test.method2((int n) -> { ; }); aoqi@0: test.method2(n -> { }); // "int" is optional here aoqi@0: test.method2((int n) -> { }); // ";" is optional here aoqi@0: test.method2((int n) -> { return; }); // ";" is mandatory here aoqi@0: test.method2((int n) -> { count += n; }); aoqi@0: assertTrue(count == 10); aoqi@0: aoqi@0: VoidInt vi = (int i) -> { aoqi@0: switch (i) { aoqi@0: case 0: aoqi@0: System.out.println("normal"); aoqi@0: break; aoqi@0: default: aoqi@0: System.out.println("invalid"); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: test.method3(()-> { count++; }); aoqi@0: test.method3(() -> {}); aoqi@0: assertTrue(count == 11); aoqi@0: aoqi@0: VoidVoid vv = ()-> { while(true) aoqi@0: if(false) aoqi@0: break; aoqi@0: else aoqi@0: continue; aoqi@0: }; aoqi@0: aoqi@0: IntVoid iv1 = () -> 42; aoqi@0: IntVoid iv2 = () -> { return 43; };//";" is mandatory here aoqi@0: assertTrue(iv1.ivMethod() == 42); aoqi@0: assertTrue(iv2.ivMethod() == 43); aoqi@0: aoqi@0: IntInt ii1 = (int n) -> n+1; aoqi@0: IntInt ii2 = n -> 42; aoqi@0: IntInt ii3 = n -> { return 43; }; aoqi@0: IntInt ii4 = aoqi@0: (int n) -> { aoqi@0: if(n > 0) aoqi@0: return n+1; aoqi@0: else aoqi@0: return n-1; aoqi@0: }; aoqi@0: assertTrue(ii1.iiMethod(1) == 2); aoqi@0: assertTrue(ii2.iiMethod(1) == 42); aoqi@0: assertTrue(ii3.iiMethod(1) == 43); aoqi@0: assertTrue(ii4.iiMethod(-1) == -2); aoqi@0: } aoqi@0: aoqi@0: void method2(VoidInt a) { aoqi@0: a.viMethod(10); aoqi@0: } aoqi@0: aoqi@0: void method3(VoidVoid a) { aoqi@0: a.vvMethod(); aoqi@0: } aoqi@0: aoqi@0: //SAM type interfaces aoqi@0: interface VoidInt { aoqi@0: void viMethod(int n); aoqi@0: } aoqi@0: aoqi@0: interface VoidVoid { aoqi@0: void vvMethod(); aoqi@0: } aoqi@0: aoqi@0: interface IntVoid { aoqi@0: int ivMethod(); aoqi@0: } aoqi@0: aoqi@0: interface IntInt { aoqi@0: int iiMethod(int n); aoqi@0: } aoqi@0: }