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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial