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