test/tools/javac/lambda/methodReference/SamConversion.java

Sat, 17 Nov 2012 19:01:03 +0000

author
mcimadamore
date
Sat, 17 Nov 2012 19:01:03 +0000
changeset 1415
01c9d4161882
child 1435
9b26c96f5138
permissions
-rw-r--r--

8003280: Add lambda tests
Summary: Turn on lambda expression, method reference and default method support
Reviewed-by: jjg

mcimadamore@1415 1 /*
mcimadamore@1415 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1415 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1415 4 *
mcimadamore@1415 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1415 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1415 7 * published by the Free Software Foundation.
mcimadamore@1415 8 *
mcimadamore@1415 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1415 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1415 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1415 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1415 13 * accompanied this code).
mcimadamore@1415 14 *
mcimadamore@1415 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1415 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1415 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1415 18 *
mcimadamore@1415 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1415 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1415 21 * questions.
mcimadamore@1415 22 */
mcimadamore@1415 23
mcimadamore@1415 24 /**
mcimadamore@1415 25 * @test
mcimadamore@1415 26 * @bug 8003280
mcimadamore@1415 27 * @summary Add lambda tests
mcimadamore@1415 28 * Test SAM conversion of method references in contexts of assignment, method/constructor argument,
mcimadamore@1415 29 * return statement, array initializer, lambda expression body, conditional expression and cast.
mcimadamore@1415 30 * @compile SamConversion.java
mcimadamore@1415 31 * @run main SamConversion
mcimadamore@1415 32 */
mcimadamore@1415 33
mcimadamore@1415 34 public class SamConversion {
mcimadamore@1415 35
mcimadamore@1415 36 static int assertionCount = 0;
mcimadamore@1415 37
mcimadamore@1415 38 static interface Foo {
mcimadamore@1415 39 Integer m(int i);
mcimadamore@1415 40 }
mcimadamore@1415 41
mcimadamore@1415 42 static interface Bar {
mcimadamore@1415 43 int m(Integer i) throws MyException;
mcimadamore@1415 44 }
mcimadamore@1415 45
mcimadamore@1415 46 private static void assertTrue(boolean b) {
mcimadamore@1415 47 assertionCount++;
mcimadamore@1415 48 if(!b)
mcimadamore@1415 49 throw new AssertionError();
mcimadamore@1415 50 }
mcimadamore@1415 51
mcimadamore@1415 52 private static int test1(Foo foo) {
mcimadamore@1415 53 return foo.m(1);
mcimadamore@1415 54 }
mcimadamore@1415 55
mcimadamore@1415 56 private static void test2(Bar bar, int result) {
mcimadamore@1415 57 try {
mcimadamore@1415 58 assertTrue(bar.m(1) == result);
mcimadamore@1415 59 } catch (Exception e){
mcimadamore@1415 60 assertTrue(false);
mcimadamore@1415 61 }
mcimadamore@1415 62 }
mcimadamore@1415 63
mcimadamore@1415 64 private static Bar test3(int i) {
mcimadamore@1415 65 switch (i) {
mcimadamore@1415 66 case 0:
mcimadamore@1415 67 return A::method1;
mcimadamore@1415 68 case 1:
mcimadamore@1415 69 return new A()::method2;
mcimadamore@1415 70 case 2:
mcimadamore@1415 71 return A::method3;
mcimadamore@1415 72 case 3:
mcimadamore@1415 73 return new A()::method4;
mcimadamore@1415 74 case 4:
mcimadamore@1415 75 return new A()::method5;
mcimadamore@1415 76 case 5:
mcimadamore@1415 77 return A::method6;
mcimadamore@1415 78 default:
mcimadamore@1415 79 return null;
mcimadamore@1415 80 }
mcimadamore@1415 81 }
mcimadamore@1415 82
mcimadamore@1415 83 /**
mcimadamore@1415 84 * Test SAM conversion of method reference in assignment context
mcimadamore@1415 85 */
mcimadamore@1415 86 private static void testAssignment() {
mcimadamore@1415 87 Foo foo = A::method1; //static reference, parameter type matching and return type matching
mcimadamore@1415 88 assertTrue(foo.m(1) == 2);
mcimadamore@1415 89
mcimadamore@1415 90 foo = new A()::method2; //instance reference, parameter type unboxing and return type boxing
mcimadamore@1415 91 assertTrue(foo.m(1) == 3);
mcimadamore@1415 92
mcimadamore@1415 93 foo = A::method3; //static reference, parameter type matching and return type boxing
mcimadamore@1415 94 assertTrue(foo.m(1) == 4);
mcimadamore@1415 95
mcimadamore@1415 96 foo = new A()::method4; //instance reference, parameter type unboxing and return type matching
mcimadamore@1415 97 assertTrue(foo.m(1) == 5);
mcimadamore@1415 98
mcimadamore@1415 99 foo = new A()::method5; //instance reference, parameter type unboxing and return type matching
mcimadamore@1415 100 assertTrue(foo.m(1) == 6);
mcimadamore@1415 101
mcimadamore@1415 102 Bar bar = A::method1;
mcimadamore@1415 103 try {
mcimadamore@1415 104 assertTrue(bar.m(1) == 2);
mcimadamore@1415 105 } catch (Exception e) {
mcimadamore@1415 106 assertTrue(false);
mcimadamore@1415 107 }
mcimadamore@1415 108
mcimadamore@1415 109 bar = new A()::method2;
mcimadamore@1415 110 try {
mcimadamore@1415 111 assertTrue(bar.m(1) == 3);
mcimadamore@1415 112 } catch (Exception e) {
mcimadamore@1415 113 assertTrue(false);
mcimadamore@1415 114 }
mcimadamore@1415 115
mcimadamore@1415 116 bar = A::method3;
mcimadamore@1415 117 try {
mcimadamore@1415 118 assertTrue(bar.m(1) == 4);
mcimadamore@1415 119 } catch (Exception e) {
mcimadamore@1415 120 assertTrue(false);
mcimadamore@1415 121 }
mcimadamore@1415 122
mcimadamore@1415 123 bar = new A()::method4;
mcimadamore@1415 124 try {
mcimadamore@1415 125 assertTrue(bar.m(1) == 5);
mcimadamore@1415 126 } catch (Exception e) {
mcimadamore@1415 127 assertTrue(false);
mcimadamore@1415 128 }
mcimadamore@1415 129
mcimadamore@1415 130 bar = new A()::method5;
mcimadamore@1415 131 try {
mcimadamore@1415 132 assertTrue(bar.m(1) == 6);
mcimadamore@1415 133 } catch (Exception e) {
mcimadamore@1415 134 assertTrue(false);
mcimadamore@1415 135 }
mcimadamore@1415 136
mcimadamore@1415 137 bar = new A()::method6;
mcimadamore@1415 138 try {
mcimadamore@1415 139 bar.m(1);
mcimadamore@1415 140 assertTrue(false);
mcimadamore@1415 141 } catch (MyException e) {
mcimadamore@1415 142 } catch (Exception e) {
mcimadamore@1415 143 assertTrue(false);
mcimadamore@1415 144 }
mcimadamore@1415 145 }
mcimadamore@1415 146
mcimadamore@1415 147 /**
mcimadamore@1415 148 * Test SAM conversion of method reference in method/constructor argument context
mcimadamore@1415 149 */
mcimadamore@1415 150 private static void testMethodArgument() {
mcimadamore@1415 151 assertTrue(test1(A::method1) == 2);
mcimadamore@1415 152 assertTrue(test1(new A()::method2) == 3);
mcimadamore@1415 153 assertTrue(test1(A::method3) == 4);
mcimadamore@1415 154 assertTrue(test1(new A()::method4) == 5);
mcimadamore@1415 155 assertTrue(test1(new A()::method5) == 6);
mcimadamore@1415 156 test2(A::method1, 2);
mcimadamore@1415 157 test2(new A()::method2, 3);
mcimadamore@1415 158 test2(A::method3, 4);
mcimadamore@1415 159 test2(new A()::method4, 5);
mcimadamore@1415 160 test2(new A()::method5, 6);
mcimadamore@1415 161 A a = new A(A::method1); //A(Foo f) called
mcimadamore@1415 162 assertTrue(a.method2(1) == 11);
mcimadamore@1415 163 assertTrue(a.method4(1) == 11);
mcimadamore@1415 164 assertTrue(a.method5(1) == 11);
mcimadamore@1415 165 A a2 = new A(new A()::method2); //A(Bar b) called
mcimadamore@1415 166 assertTrue(a2.method2(1) == 12);
mcimadamore@1415 167 assertTrue(a2.method4(1) == 12);
mcimadamore@1415 168 assertTrue(a2.method5(1) == 12);
mcimadamore@1415 169 }
mcimadamore@1415 170
mcimadamore@1415 171 /**
mcimadamore@1415 172 * Test SAM conversion of method reference in return statement context
mcimadamore@1415 173 */
mcimadamore@1415 174 private static void testReturnStatement() {
mcimadamore@1415 175 Bar bar = test3(0);
mcimadamore@1415 176 try {
mcimadamore@1415 177 assertTrue(bar.m(1) == 2);
mcimadamore@1415 178 } catch (Exception e) {
mcimadamore@1415 179 assertTrue(false);
mcimadamore@1415 180 }
mcimadamore@1415 181 bar = test3(1);
mcimadamore@1415 182 try {
mcimadamore@1415 183 assertTrue(bar.m(1) == 3);
mcimadamore@1415 184 } catch (Exception e) {
mcimadamore@1415 185 assertTrue(false);
mcimadamore@1415 186 }
mcimadamore@1415 187 bar = test3(2);
mcimadamore@1415 188 try {
mcimadamore@1415 189 assertTrue(bar.m(1) == 4);
mcimadamore@1415 190 } catch (Exception e) {
mcimadamore@1415 191 assertTrue(false);
mcimadamore@1415 192 }
mcimadamore@1415 193 bar = test3(3);
mcimadamore@1415 194 try {
mcimadamore@1415 195 assertTrue(bar.m(1) == 5);
mcimadamore@1415 196 } catch (Exception e) {
mcimadamore@1415 197 assertTrue(false);
mcimadamore@1415 198 }
mcimadamore@1415 199 bar = test3(4);
mcimadamore@1415 200 try {
mcimadamore@1415 201 assertTrue(bar.m(1) == 6);
mcimadamore@1415 202 } catch (Exception e) {
mcimadamore@1415 203 assertTrue(false);
mcimadamore@1415 204 }
mcimadamore@1415 205 bar = test3(5);
mcimadamore@1415 206 try {
mcimadamore@1415 207 bar.m(1);
mcimadamore@1415 208 assertTrue(false);
mcimadamore@1415 209 } catch (MyException e) {
mcimadamore@1415 210 } catch (Exception e) {
mcimadamore@1415 211 assertTrue(false);
mcimadamore@1415 212 }
mcimadamore@1415 213 }
mcimadamore@1415 214
mcimadamore@1415 215 /**
mcimadamore@1415 216 * Test SAM conversion of method reference in cast context
mcimadamore@1415 217 */
mcimadamore@1415 218 private static void testCast() {
mcimadamore@1415 219 assertTrue(((Foo)A::method1).m(1) == 2);
mcimadamore@1415 220 try {
mcimadamore@1415 221 assertTrue(((Bar)new A()::method2).m(1) == 3);
mcimadamore@1415 222 } catch (Exception e) {
mcimadamore@1415 223 assertTrue(false);
mcimadamore@1415 224 }
mcimadamore@1415 225 }
mcimadamore@1415 226
mcimadamore@1415 227 /**
mcimadamore@1415 228 * Test SAM conversion of method reference in array initializer context
mcimadamore@1415 229 */
mcimadamore@1415 230 private static void testArrayInitializer() {
mcimadamore@1415 231 Object[] oarray = {"a", 1, (Foo)A::method3}; //last element need a cast
mcimadamore@1415 232 Object[] oarray2 = {"a", 1, (Bar)new A()::method4}; //last element need a cast
mcimadamore@1415 233 Foo[] farray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5};
mcimadamore@1415 234 Bar[] barray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5, A::method6};
mcimadamore@1415 235 }
mcimadamore@1415 236
mcimadamore@1415 237 /**
mcimadamore@1415 238 * Test SAM conversion of method reference in conditional expression context
mcimadamore@1415 239 */
mcimadamore@1415 240 private static void testConditionalExpression(boolean b) {
mcimadamore@1415 241 Foo f = b ? A::method3 : new A()::method5;
mcimadamore@1415 242 if(b)
mcimadamore@1415 243 assertTrue(f.m(1) == 4);
mcimadamore@1415 244 else
mcimadamore@1415 245 assertTrue(f.m(1) == 6);
mcimadamore@1415 246
mcimadamore@1415 247 Bar bar = b ? A::method1 : A::method6;
mcimadamore@1415 248 if(b) {
mcimadamore@1415 249 try {
mcimadamore@1415 250 assertTrue(bar.m(1) == 2);
mcimadamore@1415 251 } catch (Exception e) {
mcimadamore@1415 252 assertTrue(false);
mcimadamore@1415 253 }
mcimadamore@1415 254 }
mcimadamore@1415 255 else {
mcimadamore@1415 256 try {
mcimadamore@1415 257 bar.m(1);
mcimadamore@1415 258 assertTrue(false);
mcimadamore@1415 259 } catch (MyException e) {
mcimadamore@1415 260 } catch (Exception e) {
mcimadamore@1415 261 assertTrue(false);
mcimadamore@1415 262 }
mcimadamore@1415 263 }
mcimadamore@1415 264 }
mcimadamore@1415 265
mcimadamore@1415 266 /**
mcimadamore@1415 267 * Test SAM conversion of method reference in lambda expression body
mcimadamore@1415 268 */
mcimadamore@1415 269 private static void testLambdaExpressionBody() {
mcimadamore@1415 270 Foo f = n -> ((Foo)A::method3).m(n);
mcimadamore@1415 271 assertTrue(f.m(1) == 4);
mcimadamore@1415 272
mcimadamore@1415 273 Bar b = n -> { return ((Foo)new A()::method5).m(n); };
mcimadamore@1415 274 try {
mcimadamore@1415 275 assertTrue(b.m(1) == 6);
mcimadamore@1415 276 } catch (Exception e) {
mcimadamore@1415 277 assertTrue(false);
mcimadamore@1415 278 }
mcimadamore@1415 279 }
mcimadamore@1415 280
mcimadamore@1415 281 public static void main(String[] args) {
mcimadamore@1415 282 testAssignment();
mcimadamore@1415 283 testMethodArgument();
mcimadamore@1415 284 testReturnStatement();
mcimadamore@1415 285 testCast();
mcimadamore@1415 286 testArrayInitializer();
mcimadamore@1415 287 testConditionalExpression(true);
mcimadamore@1415 288 testConditionalExpression(false);
mcimadamore@1415 289 testLambdaExpressionBody();
mcimadamore@1415 290
mcimadamore@1415 291 assertTrue(assertionCount == 38);
mcimadamore@1415 292 }
mcimadamore@1415 293
mcimadamore@1415 294 static class MyException extends Exception {}
mcimadamore@1415 295
mcimadamore@1415 296 static class A {
mcimadamore@1415 297
mcimadamore@1415 298 int value = 0;
mcimadamore@1415 299
mcimadamore@1415 300 A() {
mcimadamore@1415 301 }
mcimadamore@1415 302
mcimadamore@1415 303 A(Foo f) {
mcimadamore@1415 304 value = f.m(9);
mcimadamore@1415 305 }
mcimadamore@1415 306
mcimadamore@1415 307 A(Bar b) {
mcimadamore@1415 308 try {
mcimadamore@1415 309 value = b.m(9);
mcimadamore@1415 310 } catch (MyException e){}
mcimadamore@1415 311 }
mcimadamore@1415 312
mcimadamore@1415 313 static Integer method1(int n) {
mcimadamore@1415 314 return n + 1;
mcimadamore@1415 315 }
mcimadamore@1415 316
mcimadamore@1415 317 int method2(Integer n) {
mcimadamore@1415 318 return value == 0 ? n + 2 : n + value;
mcimadamore@1415 319 }
mcimadamore@1415 320
mcimadamore@1415 321 static int method3(int n) {
mcimadamore@1415 322 return n + 3;
mcimadamore@1415 323 }
mcimadamore@1415 324
mcimadamore@1415 325 Integer method4(Integer n) {
mcimadamore@1415 326 return value == 0 ? n + 4 : n + value;
mcimadamore@1415 327 }
mcimadamore@1415 328
mcimadamore@1415 329 Integer method5(Integer n) {
mcimadamore@1415 330 return value == 0 ? new Integer(n + 5) : new Integer(n + value);
mcimadamore@1415 331 }
mcimadamore@1415 332
mcimadamore@1415 333 static int method6(Integer n) throws MyException{
mcimadamore@1415 334 throw new MyException();
mcimadamore@1415 335 }
mcimadamore@1415 336 }
mcimadamore@1415 337 }

mercurial