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 24/** 25 * @test 26 * @bug 8003280 27 * @summary Add lambda tests 28 * Test SAM conversion of lambda expressions in context of assignment, method call, return statement and cast. 29 * @compile SamConversion.java 30 * @run main SamConversion 31 */ 32 33public class SamConversion { 34 35 static interface Foo { 36 Integer m(int i); 37 } 38 39 static interface Bar { 40 int m(Integer i) throws Exception; 41 } 42 43 private static String assertionStr = ""; 44 45 private static void assertTrue(boolean b) { 46 if(!b) 47 throw new AssertionError(); 48 } 49 50 private static void test1(Foo foo) { 51 assertTrue(foo.m(1) == 2); 52 } 53 54 private static void test2(Bar bar) { 55 try { 56 assertTrue(bar.m(1) == 2); 57 } catch (Exception e){ 58 assertTrue(false); 59 } 60 } 61 62 private static Bar test3(int i) { 63 switch (i) { 64 case 0: 65 return n -> n + 1; 66 case 1: 67 return (Integer n) -> 2 * n; 68 case 2: 69 return (Integer n) -> {return new Integer(n-1);}; 70 case 3: 71 return n -> {throw new Exception();}; 72 default: 73 return null; 74 } 75 } 76 77 public static void main(String[] args) { 78 79 //assign: 80 Foo foo = (int n) -> n + 1; //explicit type and boxing 81 assertTrue(foo.m(1) == 2); 82 83 foo = n -> n + 1; //type inferrred and boxing 84 assertTrue(foo.m(1) == 2); 85 86 Bar bar = (Integer n) -> n + 1; //explicit type and unboxing 87 try { 88 assertTrue(bar.m(1) == 2); 89 } catch (Exception e) { 90 assertTrue(false); 91 } 92 93 bar = (Integer n) -> new Integer(n+1); //explicit type and unboxing twice 94 try { 95 assertTrue(bar.m(1) == 2); 96 } catch (Exception e) { 97 assertTrue(false); 98 } 99 100 bar = n -> n.intValue() + 1; //type inferred 101 try { 102 assertTrue(bar.m(1) == 2); 103 } catch (Exception e) { 104 assertTrue(false); 105 } 106 107 bar = n -> n + 1; // type inferred and unboxing 108 try { 109 assertTrue(bar.m(1) == 2); 110 } catch (Exception e) { 111 assertTrue(false); 112 } 113 114 //cast: 115 assertTrue(((Foo)n -> {return n+1;}).m(1) == 2); //statement (instead of expression) in lambda body 116 try { 117 assertTrue(((Bar)n -> {return n+1;}).m(1) == 2); //statement in lambda body 118 } catch (Exception e) { 119 assertTrue(false); 120 } 121 122 //method parameter: 123 test1((int n) -> new Integer(n+1)); //explicit type 124 test2((Integer n) -> n.intValue() + 1); //explicit type 125 126 //return statement: 127 bar = test3(0); 128 try { 129 assertTrue(bar.m(1) == 2); 130 } catch (Exception e) { 131 assertTrue(false); 132 } 133 bar = test3(1); 134 try { 135 assertTrue(bar.m(3) == 6); 136 } catch (Exception e) { 137 assertTrue(false); 138 } 139 bar = test3(2); 140 try { 141 assertTrue(bar.m(10) == 9); 142 } catch (Exception e) { 143 assertTrue(false); 144 } 145 bar = test3(3); 146 try { 147 bar.m(10); 148 assertTrue(false); 149 } catch (Exception e) {} 150 } 151} 152