1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import annotations.ConstantMethodHandle; 18 19 import java.lang.invoke.MethodHandle; 20 21 public class ConstMethodHandleTest extends AbstractInvokeExactTest { 22 23 @Override $noinline$privateMethods()24 void $noinline$privateMethods() throws Throwable { 25 // TODO(b/378051428): can't create const-method-handle targeting private methods of 26 // inner classes. 27 } 28 29 @ConstantMethodHandle( 30 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 31 owner = "java/util/Optional", 32 fieldOrMethodName = "get", 33 descriptor = "()Ljava/lang/Object;") constOptionalGet()34 private static MethodHandle constOptionalGet() { 35 unreachable("should be replaced by const-method-handle"); 36 return null; 37 } 38 39 @Override optionalGet()40 public MethodHandle optionalGet() { 41 return constOptionalGet(); 42 } 43 44 @ConstantMethodHandle( 45 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 46 owner = "A", 47 fieldOrMethodName = "voidMethod", 48 descriptor = "()V") constVoidMethod()49 private static MethodHandle constVoidMethod() { 50 unreachable("should be replaced by const-method-handle"); 51 return null; 52 } 53 54 @Override voidMethod()55 public MethodHandle voidMethod() { 56 return constVoidMethod(); 57 } 58 59 @ConstantMethodHandle( 60 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 61 owner = "A", 62 fieldOrMethodName = "returnInt", 63 descriptor = "()I") constReturnInt()64 private static MethodHandle constReturnInt() { 65 unreachable("should be replaced by const-method-handle"); 66 return null; 67 } 68 69 @Override returnInt()70 public MethodHandle returnInt() { 71 return constReturnInt(); 72 } 73 74 @ConstantMethodHandle( 75 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 76 owner = "A", 77 fieldOrMethodName = "returnDouble", 78 descriptor = "()D") constReturnDouble()79 private static MethodHandle constReturnDouble() { 80 unreachable("should be replaced by const-method-handle"); 81 return null; 82 } 83 84 @Override returnDouble()85 public MethodHandle returnDouble() { 86 return constReturnDouble(); 87 } 88 89 @ConstantMethodHandle( 90 kind = ConstantMethodHandle.INVOKE_INTERFACE, 91 owner = "I", 92 fieldOrMethodName = "defaultMethod", 93 descriptor = "()V", 94 ownerIsInterface = true) constInterfaceDefaultMethod()95 private static MethodHandle constInterfaceDefaultMethod() { 96 unreachable("should be replaced by const-method-handle"); 97 return null; 98 } 99 100 @Override interfaceDefaultMethod()101 public MethodHandle interfaceDefaultMethod() { 102 return constInterfaceDefaultMethod(); 103 } 104 105 @ConstantMethodHandle( 106 kind = ConstantMethodHandle.INVOKE_INTERFACE, 107 owner = "I", 108 fieldOrMethodName = "overrideMe", 109 descriptor = "()V", 110 ownerIsInterface = true) constOverwrittenInterfaceDefaultMethod()111 private static MethodHandle constOverwrittenInterfaceDefaultMethod() { 112 unreachable("should be replaced by const-method-handle"); 113 return null; 114 } 115 116 @Override overwrittenInterfaceDefaultMethod()117 public MethodHandle overwrittenInterfaceDefaultMethod() { 118 return constOverwrittenInterfaceDefaultMethod(); 119 } 120 121 @ConstantMethodHandle( 122 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 123 owner = "A", 124 fieldOrMethodName = "throwException", 125 descriptor = "()V") constExceptionThrowingMethod()126 private static MethodHandle constExceptionThrowingMethod() { 127 unreachable("should be replaced by const-method-handle"); 128 return null; 129 } 130 131 @Override exceptionThrowingMethod()132 public MethodHandle exceptionThrowingMethod() { 133 return constExceptionThrowingMethod(); 134 } 135 136 @ConstantMethodHandle( 137 kind = ConstantMethodHandle.INVOKE_STATIC, 138 owner = "A", 139 fieldOrMethodName = "staticMethod", 140 descriptor = "(LA;)Ljava/lang/String;") constStaticMethod()141 private static MethodHandle constStaticMethod() { 142 unreachable("should be replaced by const-method-handle"); 143 return null; 144 } 145 146 @Override staticMethod()147 public MethodHandle staticMethod() { 148 return constStaticMethod(); 149 } 150 151 @ConstantMethodHandle( 152 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 153 owner = "Sums", 154 fieldOrMethodName = "sum", 155 descriptor = "(I)I") constSumI()156 private static MethodHandle constSumI() { 157 unreachable("should be replaced by const-method-handle"); 158 return null; 159 } 160 161 @Override sumI()162 public MethodHandle sumI() { 163 return constSumI(); 164 } 165 166 @ConstantMethodHandle( 167 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 168 owner = "Sums", 169 fieldOrMethodName = "sum", 170 descriptor = "(II)I") constSum2I()171 private static MethodHandle constSum2I() { 172 unreachable("should be replaced by const-method-handle"); 173 return null; 174 } 175 176 @Override sum2I()177 public MethodHandle sum2I() { 178 return constSum2I(); 179 } 180 181 @ConstantMethodHandle( 182 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 183 owner = "Sums", 184 fieldOrMethodName = "sum", 185 descriptor = "(III)I") constSum3I()186 private static MethodHandle constSum3I() { 187 unreachable("should be replaced by const-method-handle"); 188 return null; 189 } 190 191 @Override sum3I()192 public MethodHandle sum3I() { 193 return constSum3I(); 194 } 195 196 @ConstantMethodHandle( 197 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 198 owner = "Sums", 199 fieldOrMethodName = "sum", 200 descriptor = "(IIII)I") constSum4I()201 private static MethodHandle constSum4I() { 202 unreachable("should be replaced by const-method-handle"); 203 return null; 204 } 205 206 @Override sum4I()207 public MethodHandle sum4I() { 208 return constSum4I(); 209 } 210 211 @ConstantMethodHandle( 212 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 213 owner = "Sums", 214 fieldOrMethodName = "sum", 215 descriptor = "(IIIII)I") constSum5I()216 private static MethodHandle constSum5I() { 217 unreachable("should be replaced by const-method-handle"); 218 return null; 219 } 220 221 @Override sum5I()222 public MethodHandle sum5I() { 223 return constSum5I(); 224 } 225 226 @ConstantMethodHandle( 227 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 228 owner = "Sums", 229 fieldOrMethodName = "sum", 230 descriptor = "(IIIIII)I") constSum6I()231 private static MethodHandle constSum6I() { 232 unreachable("should be replaced by const-method-handle"); 233 return null; 234 } 235 236 @Override sum6I()237 public MethodHandle sum6I() { 238 return constSum6I(); 239 } 240 241 @ConstantMethodHandle( 242 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 243 owner = "Sums", 244 fieldOrMethodName = "sum", 245 descriptor = "(IIIIIII)I") constSum7I()246 private static MethodHandle constSum7I() { 247 unreachable("should be replaced by const-method-handle"); 248 return null; 249 } 250 251 @Override sum7I()252 public MethodHandle sum7I() { 253 return constSum7I(); 254 } 255 256 @ConstantMethodHandle( 257 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 258 owner = "Sums", 259 fieldOrMethodName = "sum", 260 descriptor = "(IIIIIIII)I") constSum8I()261 private static MethodHandle constSum8I() { 262 unreachable("should be replaced by const-method-handle"); 263 return null; 264 } 265 266 @Override sum8I()267 public MethodHandle sum8I() { 268 return constSum8I(); 269 } 270 271 @ConstantMethodHandle( 272 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 273 owner = "Sums", 274 fieldOrMethodName = "sum", 275 descriptor = "(IIIIIIIII)I") constSum9I()276 private static MethodHandle constSum9I() { 277 unreachable("should be replaced by const-method-handle"); 278 return null; 279 } 280 281 @Override sum9I()282 public MethodHandle sum9I() { 283 return constSum9I(); 284 } 285 286 @ConstantMethodHandle( 287 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 288 owner = "Sums", 289 fieldOrMethodName = "sum", 290 descriptor = "(IIIIIIIIII)I") constSum10I()291 private static MethodHandle constSum10I() { 292 unreachable("should be replaced by const-method-handle"); 293 return null; 294 } 295 296 @Override sum10I()297 public MethodHandle sum10I() { 298 return constSum10I(); 299 } 300 301 @ConstantMethodHandle( 302 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 303 owner = "Sums", 304 fieldOrMethodName = "sum", 305 descriptor = "(IJ)J") constSumIJ()306 private static MethodHandle constSumIJ() { 307 unreachable("should be replaced by const-method-handle"); 308 return null; 309 } 310 311 @Override sumIJ()312 public MethodHandle sumIJ() { 313 return constSumIJ(); 314 } 315 316 @ConstantMethodHandle( 317 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 318 owner = "Sums", 319 fieldOrMethodName = "sum", 320 descriptor = "(IJIJ)J") constSum2IJ()321 private static MethodHandle constSum2IJ() { 322 unreachable("should be replaced by const-method-handle"); 323 return null; 324 } 325 326 @Override sum2IJ()327 public MethodHandle sum2IJ() { 328 return constSum2IJ(); 329 } 330 331 @ConstantMethodHandle( 332 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 333 owner = "Sums", 334 fieldOrMethodName = "sum", 335 descriptor = "(IJIJIJ)J") constSum3IJ()336 private static MethodHandle constSum3IJ() { 337 unreachable("should be replaced by const-method-handle"); 338 return null; 339 } 340 341 @Override sum3IJ()342 public MethodHandle sum3IJ() { 343 return constSum3IJ(); 344 } 345 346 @ConstantMethodHandle( 347 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 348 owner = "Sums", 349 fieldOrMethodName = "sum", 350 descriptor = "(IJIJIJIJ)J") constSum4IJ()351 private static MethodHandle constSum4IJ() { 352 unreachable("should be replaced by const-method-handle"); 353 return null; 354 } 355 356 @Override sum4IJ()357 public MethodHandle sum4IJ() { 358 return constSum4IJ(); 359 } 360 361 362 @ConstantMethodHandle( 363 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 364 owner = "Sums", 365 fieldOrMethodName = "sum", 366 descriptor = "(IJIJIJIJIJ)J") constSum5IJ()367 private static MethodHandle constSum5IJ() { 368 unreachable("should be replaced by const-method-handle"); 369 return null; 370 } 371 372 @Override sum5IJ()373 public MethodHandle sum5IJ() { 374 return constSum5IJ(); 375 } 376 377 @ConstantMethodHandle( 378 kind = ConstantMethodHandle.INVOKE_INTERFACE, 379 owner = "Foo", 380 fieldOrMethodName = "nonDefault", 381 descriptor = "()Ljava/lang/String;", 382 ownerIsInterface = true) constFooNonDefault()383 private static MethodHandle constFooNonDefault() { 384 unreachable("should be replaced by const-method-handle"); 385 return null; 386 } 387 388 @Override fooNonDefault()389 public MethodHandle fooNonDefault() { 390 return constFooNonDefault(); 391 } 392 393 @ConstantMethodHandle( 394 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 395 owner = "FooBarImpl", 396 fieldOrMethodName = "nonDefault", 397 descriptor = "()Ljava/lang/String;") constFooBarImplNonDefault()398 private static MethodHandle constFooBarImplNonDefault() { 399 unreachable("should be replaced by const-method-handle"); 400 return null; 401 } 402 403 @Override fooBarImplNonDefault()404 public MethodHandle fooBarImplNonDefault() { 405 return constFooBarImplNonDefault(); 406 } 407 408 @ConstantMethodHandle( 409 kind = ConstantMethodHandle.INVOKE_INTERFACE, 410 owner = "Bar", 411 fieldOrMethodName = "defaultToOverride", 412 descriptor = "()Ljava/lang/String;", 413 ownerIsInterface = true) constBarDefault()414 private static MethodHandle constBarDefault() { 415 unreachable("should be replaced by const-method-handle"); 416 return null; 417 } 418 419 @Override barDefault()420 public MethodHandle barDefault() { 421 return constBarDefault(); 422 } 423 424 @ConstantMethodHandle( 425 kind = ConstantMethodHandle.INVOKE_INTERFACE, 426 owner = "Foo", 427 fieldOrMethodName = "defaultToOverride", 428 descriptor = "()Ljava/lang/String;", 429 ownerIsInterface = true) constFooDefault()430 private static MethodHandle constFooDefault() { 431 unreachable("should be replaced by const-method-handle"); 432 return null; 433 } 434 435 @Override fooDefault()436 public MethodHandle fooDefault() { 437 return constFooDefault(); 438 } 439 440 @ConstantMethodHandle( 441 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 442 owner = "FooBarImpl", 443 fieldOrMethodName = "defaultToOverride", 444 descriptor = "()Ljava/lang/String;") constFooBarImplDefault()445 private static MethodHandle constFooBarImplDefault() { 446 unreachable("should be replaced by const-method-handle"); 447 return null; 448 } 449 450 @Override fooBarImplDefault()451 public MethodHandle fooBarImplDefault() { 452 return constFooBarImplDefault(); 453 } 454 455 @ConstantMethodHandle( 456 kind = ConstantMethodHandle.INVOKE_INTERFACE, 457 owner = "Foo", 458 fieldOrMethodName = "nonOverriddenDefault", 459 descriptor = "()Ljava/lang/String;", 460 ownerIsInterface = true) constFooNonOverriddenDefault()461 private static MethodHandle constFooNonOverriddenDefault() { 462 unreachable("should be replaced by const-method-handle"); 463 return null; 464 } 465 466 @Override fooNonOverriddenDefault()467 public MethodHandle fooNonOverriddenDefault() { 468 return constFooNonOverriddenDefault(); 469 } 470 471 @ConstantMethodHandle( 472 kind = ConstantMethodHandle.INVOKE_INTERFACE, 473 owner = "Bar", 474 fieldOrMethodName = "nonOverriddenDefault", 475 descriptor = "()Ljava/lang/String;", 476 ownerIsInterface = true) constBarNonOverriddenDefault()477 private static MethodHandle constBarNonOverriddenDefault() { 478 unreachable("should be replaced by const-method-handle"); 479 return null; 480 } 481 482 @Override barNonOverriddenDefault()483 public MethodHandle barNonOverriddenDefault() { 484 return constBarNonOverriddenDefault(); 485 } 486 487 @ConstantMethodHandle( 488 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 489 owner = "FooBar", 490 fieldOrMethodName = "definedInAbstract", 491 descriptor = "()Ljava/lang/String;") constFooBarDefinedInAbstract()492 private static MethodHandle constFooBarDefinedInAbstract() { 493 unreachable("should be replaced by const-method-handle"); 494 return null; 495 } 496 497 @Override fooBarDefinedInAbstract()498 public MethodHandle fooBarDefinedInAbstract() { 499 return constFooBarDefinedInAbstract(); 500 } 501 502 @ConstantMethodHandle( 503 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 504 owner = "FooBarImpl", 505 fieldOrMethodName = "definedInAbstract", 506 descriptor = "()Ljava/lang/String;") constFooBarImplDefinedInAbstract()507 private static MethodHandle constFooBarImplDefinedInAbstract() { 508 unreachable("should be replaced by const-method-handle"); 509 return null; 510 } 511 512 @Override fooBarImplDefinedInAbstract()513 public MethodHandle fooBarImplDefinedInAbstract() { 514 return constFooBarImplDefinedInAbstract(); 515 } 516 517 @ConstantMethodHandle( 518 kind = ConstantMethodHandle.INVOKE_VIRTUAL, 519 owner = "FooBar", 520 fieldOrMethodName = "nonDefault", 521 descriptor = "()Ljava/lang/String;") constFooBarNonDefault()522 private static MethodHandle constFooBarNonDefault() { 523 unreachable("should be replaced by const-method-handle"); 524 return null; 525 } 526 527 @Override fooBarNonDefault()528 public MethodHandle fooBarNonDefault() { 529 return constFooBarNonDefault(); 530 } 531 532 @ConstantMethodHandle( 533 kind = ConstantMethodHandle.INVOKE_INTERFACE, 534 owner = "ToStringable", 535 fieldOrMethodName = "toString", 536 descriptor = "()Ljava/lang/String;", 537 ownerIsInterface = true) constToStringDefinedInAnInterface()538 private static MethodHandle constToStringDefinedInAnInterface() { 539 unreachable("should be replaced by const-method-handle"); 540 return null; 541 } 542 543 @Override toStringDefinedInAnInterface()544 public MethodHandle toStringDefinedInAnInterface() { 545 return constToStringDefinedInAnInterface(); 546 } 547 548 @ConstantMethodHandle( 549 kind = ConstantMethodHandle.INVOKE_INTERFACE, 550 owner = "Interface1", 551 fieldOrMethodName = "methodOne", 552 descriptor = "()Ljava/lang/String;", 553 ownerIsInterface = true) constInterfaceOneMethod()554 private static MethodHandle constInterfaceOneMethod() { 555 unreachable("should be replaced by const-method-handle"); 556 return null; 557 } 558 559 @Override interfaceOneMethod()560 public MethodHandle interfaceOneMethod() { 561 return constInterfaceOneMethod(); 562 } 563 564 @ConstantMethodHandle( 565 kind = ConstantMethodHandle.INVOKE_INTERFACE, 566 owner = "Interface2", 567 fieldOrMethodName = "methodTwo", 568 descriptor = "()Ljava/lang/String;", 569 ownerIsInterface = true) constInterfaceTwoMethod()570 private static MethodHandle constInterfaceTwoMethod() { 571 unreachable("should be replaced by const-method-handle"); 572 return null; 573 } 574 575 @Override interfaceTwoMethod()576 public MethodHandle interfaceTwoMethod() { 577 return constInterfaceTwoMethod(); 578 } 579 580 @ConstantMethodHandle( 581 kind = ConstantMethodHandle.INVOKE_INTERFACE, 582 owner = "Interface3", 583 fieldOrMethodName = "methodThree", 584 descriptor = "()Ljava/lang/String;", 585 ownerIsInterface = true) constInterfaceThreeMethod()586 private static MethodHandle constInterfaceThreeMethod() { 587 unreachable("should be replaced by const-method-handle"); 588 return null; 589 } 590 591 @Override interfaceThreeMethod()592 public MethodHandle interfaceThreeMethod() { 593 return constInterfaceThreeMethod(); 594 } 595 596 @ConstantMethodHandle( 597 kind = ConstantMethodHandle.INVOKE_INTERFACE, 598 owner = "Interface4", 599 fieldOrMethodName = "methodFour", 600 descriptor = "()Ljava/lang/String;", 601 ownerIsInterface = true) constInterfaceFourMethod()602 private static MethodHandle constInterfaceFourMethod() { 603 unreachable("should be replaced by const-method-handle"); 604 return null; 605 } 606 607 @Override interfaceFourMethod()608 public MethodHandle interfaceFourMethod() { 609 return constInterfaceFourMethod(); 610 } 611 612 @ConstantMethodHandle( 613 kind = ConstantMethodHandle.INVOKE_INTERFACE, 614 owner = "FooAndFooConflict", 615 fieldOrMethodName = "defaultToOverride", 616 descriptor = "()Ljava/lang/String;", 617 ownerIsInterface = true) constFooAndFooConflictDefault()618 private static MethodHandle constFooAndFooConflictDefault() { 619 unreachable("should be replaced by const-method-handle"); 620 return null; 621 } 622 623 @Override fooAndFooConflictDefault()624 public MethodHandle fooAndFooConflictDefault() { 625 return constFooAndFooConflictDefault(); 626 } 627 628 @ConstantMethodHandle( 629 kind = ConstantMethodHandle.INVOKE_INTERFACE, 630 owner = "BaseInterface", 631 fieldOrMethodName = "method", 632 descriptor = "()Ljava/lang/String;", 633 ownerIsInterface = true) constBaseInterface()634 private static MethodHandle constBaseInterface() { 635 unreachable("should be replaced by const-method-handle"); 636 return null; 637 } 638 639 @Override baseInterface()640 public MethodHandle baseInterface() { 641 return constBaseInterface(); 642 } 643 } 644