1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/funcInterfaces/LambdaTest1.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,119 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, 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 lambda expressions 1.32 + * @compile LambdaTest1.java 1.33 + * @run main LambdaTest1 1.34 + */ 1.35 + 1.36 +import java.util.Collections; 1.37 +import java.util.List; 1.38 +import java.util.ArrayList; 1.39 + 1.40 +public class LambdaTest1 { 1.41 + public static void main(String[] args) { 1.42 + 1.43 + LambdaTest1 test = new LambdaTest1(); 1.44 + 1.45 + test.method2((int n) -> { }); 1.46 + test.method2((int n) -> { }); 1.47 + test.method2((int n) -> { return; }); // ";" is mandatory here 1.48 + test.method2((int n) -> { System.out.println(n); }); // ";" is optional here 1.49 + test.method2(n -> { System.out.println(n); }); //warning, explict type required for n? 1.50 + 1.51 + test.method3(()-> { System.out.println("implementing VoidVoid.vvMethod()"); }); 1.52 + test.method3(() -> {}); 1.53 + 1.54 + test.method4(()-> 42); 1.55 + test.method4(()-> { return 42; });//";" is mandatory here 1.56 + 1.57 + test.method5((int n)-> n+1); 1.58 + test.method5((int n) -> 42); 1.59 + test.method5((int n) -> { return 42; }); 1.60 + test.method5( 1.61 + (int n) -> { //"{" optional here 1.62 + if(n > 0) 1.63 + return n++; 1.64 + else 1.65 + return n--; 1.66 + } 1.67 + ); 1.68 + 1.69 + Runnable r = ()-> { System.out.println("Runnable.run() method implemented"); }; 1.70 + r.run(); 1.71 + ((Runnable)()-> { System.out.println("Runnable.run() method implemented"); }).run(); 1.72 + } 1.73 + 1.74 + void method2(VoidInt a) { 1.75 + System.out.println("method2()"); 1.76 + final int N = 1; 1.77 + int n = 2; //effectively final variable 1.78 + System.out.println("method2() \"this\":" + this); 1.79 + ((Runnable) 1.80 + ()->{ 1.81 + System.out.println("inside lambda \"this\":" + this); 1.82 + System.out.println("inside lambda accessing final variable N:" + N); 1.83 + System.out.println("inside lambda accessing effectively final variable n:" + n); 1.84 + } 1.85 + ).run(); 1.86 + //n++; //compile error if n is modified 1.87 + a.viMethod(2); 1.88 + } 1.89 + 1.90 + void method3(VoidVoid a) { 1.91 + System.out.println("method3()"); 1.92 + a.vvMethod(); 1.93 + } 1.94 + 1.95 + void method4(IntVoid a) { 1.96 + System.out.println("method4()"); 1.97 + System.out.println(a.ivMethod()); 1.98 + } 1.99 + 1.100 + void method5(IntInt a) { 1.101 + System.out.println("method5()"); 1.102 + System.out.println(a.iiMethod(5)); 1.103 + } 1.104 + 1.105 + 1.106 + //SAM type interfaces 1.107 + interface VoidInt { 1.108 + void viMethod(int n); 1.109 + } 1.110 + 1.111 + interface VoidVoid { 1.112 + void vvMethod(); 1.113 + } 1.114 + 1.115 + interface IntVoid { 1.116 + int ivMethod(); 1.117 + } 1.118 + 1.119 + interface IntInt { 1.120 + int iiMethod(int n); 1.121 + } 1.122 +}