Lambda.java revision 2454:f434ca8aface
1/* 2 * Copyright (c) 2013, 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 8008077 8029721 8042451 8043974 27 * @summary Test population of reference info for lambda expressions 28 * javac crash for annotated parameter type of lambda in a field 29 * @compile -g Driver.java ReferenceInfoUtil.java Lambda.java 30 * @run main Driver Lambda 31 * @author Werner Dietl 32 */ 33 34import static com.sun.tools.classfile.TypeAnnotation.TargetType.*; 35 36public class Lambda { 37 38 @TADescription(annotation = "TA", type = METHOD_REFERENCE, 39 offset = ReferenceInfoUtil.IGNORE_VALUE) 40 @TADescription(annotation = "TB", type = METHOD_REFERENCE, 41 offset = ReferenceInfoUtil.IGNORE_VALUE) 42 public String returnMethodRef1() { 43 return 44 "class Lambda {" + 45 " public String getName() { return \"Lambda!\"; }" + 46 "}" + 47 48 "class %TEST_CLASS_NAME% {" + 49 " java.util.function.Function<Lambda, String> lambda() {" + 50 " return @TA @TB Lambda::getName;" + 51 " }" + 52 "}"; 53 } 54 55 @TADescription(annotation = "TA", type = METHOD_REFERENCE, 56 offset = ReferenceInfoUtil.IGNORE_VALUE) 57 @TADescription(annotation = "TB", type = METHOD_REFERENCE, 58 offset = ReferenceInfoUtil.IGNORE_VALUE, 59 genericLocation = { 3, 0 }) 60 @TADescription(annotation = "TC", type = METHOD_REFERENCE, 61 offset = ReferenceInfoUtil.IGNORE_VALUE, 62 genericLocation = { 3, 0 }) 63 @TADescription(annotation = "TD", type = METHOD_REFERENCE, 64 offset = ReferenceInfoUtil.IGNORE_VALUE, 65 genericLocation = { 3, 1 }) 66 @TADescription(annotation = "TE", type = METHOD_REFERENCE, 67 offset = ReferenceInfoUtil.IGNORE_VALUE, 68 genericLocation = { 3, 1}) 69 public String returnMethodRef2() { 70 return 71 "class Lambda<S, T> {" + 72 " public String getName() { return \"Lambda!\"; }" + 73 "}" + 74 75 "class %TEST_CLASS_NAME% {" + 76 " java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" + 77 " return @TA Lambda<@TB @TC Integer, @TD @TE Float>::getName;" + 78 " }" + 79 "}"; 80 } 81 82 @TADescription(annotation = "CTA", type = METHOD_REFERENCE, 83 offset = ReferenceInfoUtil.IGNORE_VALUE) 84 @TADescription(annotation = "CTB", type = METHOD_REFERENCE, 85 offset = ReferenceInfoUtil.IGNORE_VALUE, 86 genericLocation = { 3, 0 }) 87 @TADescription(annotation = "CTC", type = METHOD_REFERENCE, 88 offset = ReferenceInfoUtil.IGNORE_VALUE, 89 genericLocation = { 3, 1 }) 90 public String returnMethodRef3() { 91 return 92 "class Lambda<S, T> {" + 93 " public String getName() { return \"Lambda!\"; }" + 94 "}" + 95 96 "@Target(ElementType.TYPE_USE)" + 97 "@interface CTA {" + 98 " String value();" + 99 "}" + 100 101 "@Target(ElementType.TYPE_USE)" + 102 "@interface CTB {" + 103 " int age();" + 104 "}" + 105 106 "@Target(ElementType.TYPE_USE)" + 107 "@interface CTC {" + 108 " String name();" + 109 "}" + 110 111 "class %TEST_CLASS_NAME% {" + 112 " java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" + 113 " return @CTA(\"x\") Lambda<@CTB(age = 5) Integer, @CTC(name = \"y\") Float>::getName;" + 114 " }" + 115 "}"; 116 } 117 118 119 @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE, 120 offset = ReferenceInfoUtil.IGNORE_VALUE) 121 @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE, 122 offset = ReferenceInfoUtil.IGNORE_VALUE) 123 public String returnConstructorRef1() { 124 return 125 "class Lambda {" + 126 " Lambda() { }" + 127 "}" + 128 129 "class %TEST_CLASS_NAME% {" + 130 " Runnable lambda() {" + 131 " return @TA @TB Lambda::new;" + 132 " }" + 133 "}"; 134 } 135 136 @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE, 137 offset = ReferenceInfoUtil.IGNORE_VALUE) 138 @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE, 139 offset = ReferenceInfoUtil.IGNORE_VALUE, 140 genericLocation = { 3, 0 }) 141 @TADescription(annotation = "TC", type = CONSTRUCTOR_REFERENCE, 142 offset = ReferenceInfoUtil.IGNORE_VALUE, 143 genericLocation = { 3, 0 }) 144 @TADescription(annotation = "TD", type = CONSTRUCTOR_REFERENCE, 145 offset = ReferenceInfoUtil.IGNORE_VALUE, 146 genericLocation = { 3, 1 }) 147 @TADescription(annotation = "TE", type = CONSTRUCTOR_REFERENCE, 148 offset = ReferenceInfoUtil.IGNORE_VALUE, 149 genericLocation = { 3, 1 }) 150 public String returnConstructorRef2() { 151 return 152 "class Lambda<S, T> {" + 153 " Lambda() { }" + 154 "}" + 155 156 "class %TEST_CLASS_NAME% {" + 157 " Runnable lambda() {" + 158 " return @TA Lambda<@TB @TC Integer, @TD @TE Float>::new;" + 159 " }" + 160 "}"; 161 } 162 163 @TADescription(annotation = "CTA", type = CONSTRUCTOR_REFERENCE, 164 offset = ReferenceInfoUtil.IGNORE_VALUE) 165 @TADescription(annotation = "CTB", type = CONSTRUCTOR_REFERENCE, 166 offset = ReferenceInfoUtil.IGNORE_VALUE, 167 genericLocation = { 3, 0 }) 168 @TADescription(annotation = "CTC", type = CONSTRUCTOR_REFERENCE, 169 offset = ReferenceInfoUtil.IGNORE_VALUE, 170 genericLocation = { 3, 1 }) 171 public String returnConstructorRef3() { 172 return 173 "class Lambda<S, T> {" + 174 " Lambda() { }" + 175 "}" + 176 177 "@Target(ElementType.TYPE_USE)" + 178 "@interface CTA {" + 179 " String value();" + 180 "}" + 181 182 "@Target(ElementType.TYPE_USE)" + 183 "@interface CTB {" + 184 " int age();" + 185 "}" + 186 187 "@Target(ElementType.TYPE_USE)" + 188 "@interface CTC {" + 189 " String name();" + 190 "}" + 191 192 "class %TEST_CLASS_NAME% {" + 193 " Runnable lambda() {" + 194 " return @CTA(\"x\") Lambda<@CTB(age = 5) Integer, @CTC(name = \"y\") Float>::new;" + 195 " }" + 196 "}"; 197 } 198 199 200 @TADescription(annotation = "TA", type = METHOD_REFERENCE_TYPE_ARGUMENT, 201 offset = ReferenceInfoUtil.IGNORE_VALUE, 202 typeIndex = 0) 203 @TADescription(annotation = "TB", type = METHOD_REFERENCE_TYPE_ARGUMENT, 204 offset = ReferenceInfoUtil.IGNORE_VALUE, 205 typeIndex = 1) 206 public String returnMethodRefTA1() { 207 return 208 "interface Lambda {" + 209 " <S, T> void generic(S p1, T p2);" + 210 "}" + 211 212 "class LambdaImpl implements Lambda {" + 213 " public <S, T> void generic(S p1, T p2) {}" + 214 "}" + 215 216 "class %TEST_CLASS_NAME% {" + 217 " Lambda lambda(LambdaImpl r) {" + 218 " return r::<@TA Object, @TB Object>generic;" + 219 " }" + 220 "}"; 221 } 222 223 @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT, 224 offset = ReferenceInfoUtil.IGNORE_VALUE, 225 typeIndex = 0) 226 @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT, 227 offset = ReferenceInfoUtil.IGNORE_VALUE, 228 typeIndex = 1) 229 public String returnConstructorRefTA2() { 230 return 231 "interface Lambda {" + 232 " <S, T> void generic(S p1, T p2);" + 233 "}" + 234 235 "class LambdaImpl implements Lambda {" + 236 " <S, T> LambdaImpl(S p1, T p2) {}" + 237 " public <S, T> void generic(S p1, T p2) {}" + 238 "}" + 239 240 "class %TEST_CLASS_NAME% {" + 241 " Lambda lambda() {" + 242 " return LambdaImpl::<@TA Object, @TB Object>new;" + 243 " }" + 244 "}"; 245 } 246 247 @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER, 248 paramIndex = 0) 249 @TADescription(annotation = "TB", type = METHOD_FORMAL_PARAMETER, 250 paramIndex = 1) 251 @TADescription(annotation = "TC", type = METHOD_FORMAL_PARAMETER, 252 paramIndex = 1, genericLocation = { 3, 0 }) 253 @TADescription(annotation = "TD", type = LOCAL_VARIABLE, 254 lvarOffset = ReferenceInfoUtil.IGNORE_VALUE, 255 lvarLength = ReferenceInfoUtil.IGNORE_VALUE, 256 lvarIndex = ReferenceInfoUtil.IGNORE_VALUE) 257 @TADescription(annotation = "TE", type = CAST, 258 offset = ReferenceInfoUtil.IGNORE_VALUE, 259 typeIndex = 0) 260 public String returnLambdaExpr1() { 261 return 262 "interface LambdaInt {" + 263 " void lambda(Object p1, List<Object> p2);" + 264 "}" + 265 "class %TEST_CLASS_NAME% {" + 266 " LambdaInt getLambda() {" + 267 " return (@TA Object x, @TB List<@TC Object> y) -> { @TD Object l = null; System.out.println((@TE Object) l); };" + 268 " }" + 269 "}"; 270 } 271 272 @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER, 273 paramIndex = 0) 274 public String lambdaField1() { 275 return 276 "class %TEST_CLASS_NAME% {" + 277 " java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" + 278 "}"; 279 } 280 281 @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER, 282 paramIndex = 0) 283 public String lambdaField2() { 284 return 285 "class %TEST_CLASS_NAME% {" + 286 " static java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" + 287 "}"; 288 } 289 290 @TADescription(annotation = "RTAs", type = METHOD_REFERENCE, 291 offset = ReferenceInfoUtil.IGNORE_VALUE) 292 public String returnMethodRefRepeatableAnnotation1() { 293 return 294 "class Lambda {" + 295 " public String getName() { return \"Lambda!\"; }" + 296 "}" + 297 298 "class %TEST_CLASS_NAME% {" + 299 " java.util.function.Function<Lambda, String> lambda() {" + 300 " return @RTA @RTA Lambda::getName;" + 301 " }" + 302 "}"; 303 } 304 305 @TADescription(annotation = "RTAs", type = METHOD_REFERENCE, 306 offset = ReferenceInfoUtil.IGNORE_VALUE) 307 @TADescription(annotation = "RTBs", type = METHOD_REFERENCE, 308 offset = ReferenceInfoUtil.IGNORE_VALUE, 309 genericLocation = { 3, 0 }) 310 @TADescription(annotation = "RTCs", type = METHOD_REFERENCE, 311 offset = ReferenceInfoUtil.IGNORE_VALUE, 312 genericLocation = { 3, 0 }) 313 @TADescription(annotation = "RTDs", type = METHOD_REFERENCE, 314 offset = ReferenceInfoUtil.IGNORE_VALUE, 315 genericLocation = { 3, 1 }) 316 @TADescription(annotation = "RTEs", type = METHOD_REFERENCE, 317 offset = ReferenceInfoUtil.IGNORE_VALUE, 318 genericLocation = { 3, 1}) 319 public String returnMethodRefRepeatableAnnotation2() { 320 return 321 "class Lambda<S, T> {" + 322 " public String getName() { return \"Lambda!\"; }" + 323 "}" + 324 325 "class %TEST_CLASS_NAME% {" + 326 " java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" + 327 " return @RTA @RTA Lambda<@RTB @RTB @RTC @RTC Integer, @RTD @RTD @RTE @RTE Float>::getName;" + 328 " }" + 329 "}"; 330 } 331 332 @TADescription(annotation = "RTAs", type = CONSTRUCTOR_REFERENCE, 333 offset = ReferenceInfoUtil.IGNORE_VALUE) 334 public String returnConstructorRefRepeatable1() { 335 return 336 "class Lambda {" + 337 " Lambda() { }" + 338 "}" + 339 340 "class %TEST_CLASS_NAME% {" + 341 " Runnable lambda() {" + 342 " return @RTA @RTA Lambda::new;" + 343 " }" + 344 "}"; 345 } 346 347 @TADescription(annotation = "RTAs", type = CONSTRUCTOR_REFERENCE, 348 offset = ReferenceInfoUtil.IGNORE_VALUE) 349 @TADescription(annotation = "RTBs", type = CONSTRUCTOR_REFERENCE, 350 offset = ReferenceInfoUtil.IGNORE_VALUE, 351 genericLocation = { 3, 0 }) 352 @TADescription(annotation = "RTCs", type = CONSTRUCTOR_REFERENCE, 353 offset = ReferenceInfoUtil.IGNORE_VALUE, 354 genericLocation = { 3, 1 }) 355 public String returnConstructorRefRepeatable2() { 356 return 357 "class Lambda<S, T> {" + 358 " Lambda() { }" + 359 "}" + 360 361 "class %TEST_CLASS_NAME% {" + 362 " Runnable lambda() {" + 363 " return @RTA @RTA Lambda<@RTB @RTB Integer, @RTC @RTC Float>::new;" + 364 " }" + 365 "}"; 366 } 367 368 @TADescription(annotation = "RTAs", type = METHOD_REFERENCE_TYPE_ARGUMENT, 369 offset = ReferenceInfoUtil.IGNORE_VALUE, 370 typeIndex = 0) 371 @TADescription(annotation = "RTBs", type = METHOD_REFERENCE_TYPE_ARGUMENT, 372 offset = ReferenceInfoUtil.IGNORE_VALUE, 373 typeIndex = 1) 374 public String returnMethodRefTARepeatableAnnotation1() { 375 return 376 "interface Lambda {" + 377 " <S, T> void generic(S p1, T p2);" + 378 "}" + 379 380 "class LambdaImpl implements Lambda {" + 381 " public <S, T> void generic(S p1, T p2) {}" + 382 "}" + 383 384 "class %TEST_CLASS_NAME% {" + 385 " Lambda lambda(LambdaImpl r) {" + 386 " return r::<@RTA @RTA Object, @RTB @RTB Object>generic;" + 387 " }" + 388 "}"; 389 } 390 391 @TADescription(annotation = "RTAs", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT, 392 offset = ReferenceInfoUtil.IGNORE_VALUE, 393 typeIndex = 0) 394 @TADescription(annotation = "RTBs", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT, 395 offset = ReferenceInfoUtil.IGNORE_VALUE, 396 typeIndex = 1) 397 public String returnConstructorRefTARepeatableAnnotation2() { 398 return 399 "interface Lambda {" + 400 " <S, T> void generic(S p1, T p2);" + 401 "}" + 402 403 "class LambdaImpl implements Lambda {" + 404 " <S, T> LambdaImpl(S p1, T p2) {}" + 405 " public <S, T> void generic(S p1, T p2) {}" + 406 "}" + 407 408 "class %TEST_CLASS_NAME% {" + 409 " Lambda lambda() {" + 410 " return LambdaImpl::<@RTA @RTA Object, @RTB @RTB Object>new;" + 411 " }" + 412 "}"; 413 } 414 415 @TADescription(annotation = "RTAs", type = METHOD_FORMAL_PARAMETER, 416 paramIndex = 0) 417 @TADescription(annotation = "RTBs", type = METHOD_FORMAL_PARAMETER, 418 paramIndex = 1) 419 @TADescription(annotation = "RTCs", type = METHOD_FORMAL_PARAMETER, 420 paramIndex = 1, genericLocation = { 3, 0 }) 421 @TADescription(annotation = "RTDs", type = LOCAL_VARIABLE, 422 lvarOffset = ReferenceInfoUtil.IGNORE_VALUE, 423 lvarLength = ReferenceInfoUtil.IGNORE_VALUE, 424 lvarIndex = ReferenceInfoUtil.IGNORE_VALUE) 425 @TADescription(annotation = "RTEs", type = CAST, 426 offset = ReferenceInfoUtil.IGNORE_VALUE, 427 typeIndex = 0) 428 public String returnLambdaExprRepeatableAnnotation1() { 429 return 430 "interface LambdaInt {" + 431 " void lambda(Object p1, List<Object> p2);" + 432 "}" + 433 "class %TEST_CLASS_NAME% {" + 434 " LambdaInt getLambda() {" + 435 " return (@RTA @RTA Object x, @RTB @RTB List<@RTC @RTC Object> y) ->" + 436 " { @RTD @RTD Object l = null; System.out.println((@RTE @RTE Object) l); };" + 437 " }" + 438 "}"; 439 } 440 441 @TADescription(annotation = "RTAs", type = METHOD_FORMAL_PARAMETER, 442 paramIndex = 0) 443 public String lambdaFieldRepeatableAnnotation1() { 444 return 445 "class %TEST_CLASS_NAME% {" + 446 " java.util.function.IntUnaryOperator field = (@RTA @RTA int y) -> 1;" + 447 "}"; 448 } 449 450 @TADescription(annotation = "RTAs", type = METHOD_FORMAL_PARAMETER, 451 paramIndex = 0) 452 public String lambdaFieldRepeatableAnnotation2() { 453 return 454 "class %TEST_CLASS_NAME% {" + 455 " static java.util.function.IntUnaryOperator field = (@RTA @RTA int y) -> 1;" + 456 "}"; 457 } 458} 459