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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
|
/*
* @test /nodynamiccopyright/
* @bug 8015831 8355753
* @compile/ref=ThisEscape.out -Xlint:this-escape -XDrawDiagnostics ThisEscape.java
* @summary Verify 'this' escape detection
*/
import java.util.function.*;
public class ThisEscape {
// Verify 'this' escape detection can follow references embedded as array elements
public static class ThisEscapeArrayElement {
public ThisEscapeArrayElement() {
final Object[][] array = new Object[][] { { this } };
((ThisEscapeArrayElement)array[0][0]).mightLeak();
}
public void mightLeak() {
}
}
// Verify basic 'this' escape detection
public static class ThisEscapeBasic {
public ThisEscapeBasic() {
this.mightLeak();
}
public void mightLeak() {
}
}
// Verify 'this' escape detection can follow references through various Java code structures
public static class ThisEscapeComplex {
public ThisEscapeComplex() {
this.method1().mightLeak();
}
public void mightLeak() {
}
private ThisEscapeComplex method1() {
while (true) {
do {
for (ThisEscapeComplex x = this.method2(); new Object().hashCode() < 10; ) {
for (int y : new int[] { 123, 456 }) {
return x;
}
}
} while (true);
}
}
private ThisEscapeComplex method2() {
switch (new Object().hashCode()) {
case 1:
case 2:
case 3:
return null;
default:
return this.method3();
}
}
private ThisEscapeComplex method3() {
return switch (new Object().hashCode()) {
case 1, 2, 3 -> this.method4();
default -> null;
};
}
private ThisEscapeComplex method4() {
return ThisEscapeComplex.this.method5();
}
private ThisEscapeComplex method5() {
final ThisEscapeComplex foo = this.method6();
return foo;
}
private ThisEscapeComplex method6() {
synchronized (new Object()) {
return this.method7();
}
}
private ThisEscapeComplex method7() {
ThisEscapeComplex x = null;
ThisEscapeComplex y = this.method8();
if (new Object().hashCode() == 3)
return x;
else
return y;
}
private ThisEscapeComplex method8() {
return (ThisEscapeComplex)(Object)this.method9();
}
private ThisEscapeComplex method9() {
return new Object().hashCode() == 3 ? this : null;
}
}
// Verify pruning of 'this' escape warnings for various constructors
public static class ThisEscapeCtors {
// This constructor should NOT generate a warning because it would be a
// duplicate of the warning already generated for ThisEscapeCtors(short).
public ThisEscapeCtors(char x) {
this((short)x);
}
// This constructor should generate a warning because it invokes leaky this()
// and is accessible to subclasses.
public ThisEscapeCtors(short x) {
this();
}
// This constructor should generate a warning because it invokes leaky this()
// and is accessible to subclasses.
public ThisEscapeCtors(int x) {
this();
}
// This constructor should NOT generate a warning because it is not accessbile
// to subclasses. However, other constructors do invoke it, and that should cause
// them to generate an indirect warning.
private ThisEscapeCtors() {
this.mightLeak();
}
public void mightLeak() {
}
}
// Verify 'this' escape detection in field initializers
public static class ThisEscapeFields {
private final int field1 = this.mightLeak1();
private final int field2 = this.mightLeak2();
public int mightLeak1() {
return 123;
}
public int mightLeak2() {
return 456;
}
}
// Verify 'this' escape detection properly handles lambdas
public static class ThisEscapeLambda {
public ThisEscapeLambda() {
Runnable r = () -> {
this.mightLeak();
};
System.out.println(r);
}
public void mightLeak() {
}
}
// Verify 'this' escape detection properly handles loop convergence
public static class ThisEscapeLoop {
public ThisEscapeLoop() {
ThisEscapeLoop ref1 = this;
ThisEscapeLoop ref2 = null;
ThisEscapeLoop ref3 = null;
ThisEscapeLoop ref4 = null;
for (int i = 0; i < 100; i++) {
ref4 = ref3;
ref3 = ref2;
ref2 = ref1;
if (ref4 != null)
ref4.mightLeak();
}
}
public void mightLeak() {
}
}
// Verify 'this' escape detection handles leaks via outer 'this'
public static class ThisEscapeOuterThis {
public ThisEscapeOuterThis() {
new InnerClass();
}
public void mightLeak() {
}
public class InnerClass {
InnerClass() {
ThisEscapeOuterThis.this.mightLeak();
}
}
// No leak here because class 'Local' cannot be externally extended
public static void method1() {
class Local {
Local() {
this.wontLeak();
}
void wontLeak() {
}
}
}
}
// Verify 'this' escape detection handles leaks via passing 'this' as a parameter
public static class ThisEscapeParameter {
public ThisEscapeParameter() {
ThisEscapeParameter.method(this);
}
public static void method(Object obj) {
obj.hashCode();
}
}
// Verify 'this' escape detection properly handles leaks via recursive methods
public static class ThisEscapeRecursion {
public ThisEscapeRecursion() {
this.noLeak(0); // no leak here
this.mightLeak(); // possible leak here
}
public final void noLeak(int depth) {
if (depth < 10)
this.noLeak(depth - 1);
}
public void mightLeak() {
}
}
// Verify proper handling of 'this' escape warnings from method references
public static class ThisEscapeReference {
// Test 1 - ReferenceKind.SUPER
public static class Test1 {
public void mightLeak() {
}
}
public static class Test1b extends Test1 {
public Test1b() {
new Thread(super::mightLeak); // this is a leak
}
}
public static class Test1c extends Test1 {
public Test1c() {
new Thread(super::notify); // this is not a leak
}
}
// Test 2 - ReferenceKind.BOUND
public static class Test2 {
public Test2() {
new Thread(this::mightLeak); // this is a leak
}
public Test2(int x) {
final Test2 foo = new Test2();
new Thread(foo::mightLeak); // this is not a leak
}
public Test2(char x) {
new Thread(this::noLeak); // this is not a leak
}
public void mightLeak() {
}
private void noLeak() {
}
}
// Test 3 - ReferenceKind.IMPLICIT_INNER
public static class Test3 {
public Test3() {
new Thread(Inner1::new); // this is a leak
}
public Test3(int x) {
new Thread(Inner2::new); // this is not a leak
}
public void mightLeak() {
}
public class Inner1 {
public Inner1() {
Test3.this.mightLeak();
}
}
public static class Inner2 {
public Inner2() {
new Test3().mightLeak();
}
}
}
// Test 4 - ReferenceKind.UNBOUND, STATIC, TOPLEVEL, ARRAY_CTOR
public static class Test4 {
// ReferenceKind.UNBOUND
public Test4() {
Test4.bar(Test4::sameHashCode);
}
// ReferenceKind.STATIC
public Test4(int x) {
new Thread(Test4::noLeak); // this is not a leak
}
// ReferenceKind.ARRAY_CTOR
public Test4(char x) {
Test4.foo(String[]::new); // this is not a leak
}
// ReferenceKind.TOPLEVEL
public Test4(short x) {
Test4.foo(Test4::new); // this is not a leak
}
public static void noLeak() {
}
public static void foo(IntFunction<?> x) {
x.hashCode();
}
public static void bar(BiPredicate<Test4, Object> x) {
x.hashCode();
}
public boolean sameHashCode(Object obj) {
return obj.hashCode() == this.hashCode();
}
}
}
// Verify 'this' escape detection properly handles leaks via method return values
public static class ThisEscapeReturnValue {
public ThisEscapeReturnValue() {
final Object rval = ThisEscapeReturnValue.method(this);
((ThisEscapeReturnValue)rval).mightLeak();
}
public static Object method(Object obj) {
return obj;
}
public void mightLeak() {
}
}
// Verify 'this' escape detection from a thrown 'this'
public static class ThisEscapeThrown extends RuntimeException {
public ThisEscapeThrown(Object obj) {
if (obj == null)
throw this;
}
}
// Verify proper 'this' escape interpretation of unqualified non-static method invocations
public static class ThisEscapeUnqualified {
// This class has a leak
public static class Example1 {
public Example1() {
new Inner();
}
public final class Inner {
public Inner() {
mightLeak(); // refers to Example1.mightLeak()
}
}
public void mightLeak() {
}
}
// This class does NOT have a leak
public static class Example2 {
public Example2() {
new Inner();
}
public final class Inner {
public Inner() {
mightLeak(); // refers to Inner.mightLeak()
}
public void mightLeak() {
}
}
public void mightLeak() {
}
}
}
// Verify 'this' escape detection handles leaks via switch expression yields
public static class ThisEscapeYield {
public ThisEscapeYield(int x) {
ThisEscapeYield y = switch (x) {
case 3:
if (x > 17)
yield this;
else
yield null;
default:
yield null;
};
if (y != null)
y.mightLeak();
}
public void mightLeak() {
}
}
// Verify 'this' escape warnings can be properly suppressed on constructors
public static class ThisEscapeSuppressCtor {
private final int x = this.mightLeak();
@SuppressWarnings("this-escape")
public ThisEscapeSuppressCtor() {
this.mightLeak();
}
public int mightLeak() {
return 0;
}
}
// Verify 'this' escape warnings can be properly suppressed on fields
public static class ThisEscapeSuppressField {
@SuppressWarnings("this-escape")
private final int x = this.mightLeak();
public ThisEscapeSuppressField() {
this.mightLeak();
}
public int mightLeak() {
return 0;
}
}
// Verify 'this' escape warnings can be properly suppressed on classes
public static class ThisEscapeSuppressClass {
@SuppressWarnings("this-escape")
private final int x = this.mightLeak();
@SuppressWarnings("this-escape")
public ThisEscapeSuppressClass() {
this.mightLeak();
}
public int mightLeak() {
return 0;
}
}
// Verify 'this' escape detection doesn't generate certain false positives
public static class ThisEscapeNoEscapes {
public ThisEscapeNoEscapes() {
this.noLeak1(); // invoked method is private
this.noLeak2(); // invoked method is final
ThisEscapeNoEscapes.noLeak3(); // invoked method is static
this.noLeak4(this); // parameter is 'this' but it's not leaked
this.noLeak5(new ThisEscapeNoEscapes(0)); // parameter is not 'this', so no leak
this.noLeak6(null, this, null); // method leaks 1st and 3rd parameters only
this.noLeak7(); // method does complicated stuff but doesn't leak
Runnable r1 = () -> { // lambda does not leak 'this'
if (System.out == System.err)
throw new RuntimeException();
};
System.out.println(r1); // lambda does not leak 'this'
Runnable r2 = () -> { // lambda leaks 'this' but is never used
this.mightLeak1();
};
Runnable r3 = this::mightLeak1; // reference leaks 'this' but is never used
}
public ThisEscapeNoEscapes(int x) {
}
public void mightLeak1() {
}
private void noLeak1() {
}
public final void noLeak2() {
}
public static void noLeak3() {
}
public static void noLeak4(ThisEscapeNoEscapes param) {
param.noLeak1();
param.noLeak2();
}
public final void noLeak5(ThisEscapeNoEscapes param) {
param.mightLeak1();
}
public final void noLeak6(ThisEscapeNoEscapes param1,
ThisEscapeNoEscapes param2, ThisEscapeNoEscapes param3) {
if (param1 != null)
param1.mightLeak1();
if (param2 != null)
param2.noLeak2();
if (param3 != null)
param3.mightLeak1();
}
public final void noLeak7() {
((ThisEscapeNoEscapes)(Object)this).noLeak2();
final ThisEscapeNoEscapes obj1 = switch (new Object().hashCode()) {
case 1, 2, 3 -> null;
default -> new ThisEscapeNoEscapes(0);
};
obj1.mightLeak1();
}
// PrivateClass
private static class PrivateClass {
PrivateClass() {
this.cantLeak(); // method is inside a private class
}
public void cantLeak() {
}
}
// FinalClass
public static final class FinalClass extends ThisEscapeNoEscapes {
public FinalClass() {
this.mightLeak1(); // class and therefore method is final
}
}
public static void main(String[] args) {
new ThisEscapeNoEscapes();
}
}
// Verify 'this' escape correctly follows outer instances through member classes
public static class ThisEscapeOuterRef1 {
class Inner {
void foo() {
ThisEscapeOuterRef1.this.hashCode();
}
}
public ThisEscapeOuterRef1() {
new Inner().foo();
}
}
// Verify 'this' escape correctly follows outer instances through local classes
public static class ThisEscapeOuterRef2 {
public ThisEscapeOuterRef2() {
class Inner {
void foo() {
ThisEscapeOuterRef2.this.hashCode();
}
}
new Inner().foo();
}
}
// Verify 'this' escape correctly follows outer instances through anonymous classes
public static class ThisEscapeOuterRef3 {
public ThisEscapeOuterRef3() {
new Runnable() {
public void run() {
ThisEscapeOuterRef3.this.hashCode(); // leak here
}
}.run();
}
}
// Verify 'this' escape knows that enhanced for loops invoke iterator(), etc.
public static class ThisEscapeForeach1 implements Iterable<Object> {
public ThisEscapeForeach1() {
for (Object v : this)
v.hashCode(); // possible leak here
}
@Override
public final java.util.Iterator<Object> iterator() {
return new java.util.Iterator<Object>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public Object next() {
return ThisEscapeForeach1.this;
}
};
}
}
// Verify 'this' escape tracks deferred return values
public static class DeferredReturn {
public DeferredReturn(int x) {
((Supplier<DeferredReturn>)this::self).get().mightLeak(); // leak here
}
public DeferredReturn(float x) {
((Supplier<DeferredReturn>)() -> this).get().mightLeak(); // leak here
}
private final DeferredReturn self() {
return this;
}
protected void mightLeak() {
}
}
// Verify 'this' escape properly tracks variable types
public static class TypeTracking {
public TypeTracking() {
Runnable r = new Runnable() {
public void run() {
TypeTracking.this.mightLeak();
}
};
r.run(); // leak here - we know "r" has type TypeTracking$1
}
protected void mightLeak() {
}
}
// Verify 'this' escape doesn't warn for outer instances of anonymous classes that don't use them
public static class ThisEscapeOuterRef4 {
public ThisEscapeOuterRef4() {
new Runnable() {
public void run() {
// there is no leak in here
}
}.run();
}
}
// Verify 'this' escape doesn't warn for doubly-outer instance references
public static class ThisEscapeDoubleOuter {
public class Inner1 {
public Inner1() {
new Inner2().foo(); // NOT a 'this' leak
}
class Inner2 {
void foo() {
ThisEscapeDoubleOuter.this.hashCode();
}
}
}
}
// Verify 'this' escape detection doesn't warn for sealed classes with local permits
public static sealed class ThisEscapeSealed permits ThisEscapeSealed.Sub1, ThisEscapeSealed.Sub2 {
public ThisEscapeSealed() {
this.mightLeak();
}
public void mightLeak() {
}
public static final class Sub1 extends ThisEscapeSealed {
}
public static final class Sub2 extends ThisEscapeSealed {
}
}
// Verify no assertion error occurs (JDK-8317336)
public static class ThisEscapeAssertionError {
public ThisEscapeAssertionError() {
System.out.println((Supplier<Object>)() -> this);
}
}
// Verify no assertion error occurs (JDK-8317336)
public static class ThisEscapeAssertionError2 {
public ThisEscapeAssertionError2() {
ThisEscapeAssertionError2[] array = new ThisEscapeAssertionError2[] { this };
for (Object obj : array)
;
}
}
// Verify no infinite recursion loop occurs (JDK-8317818)
public static class ThisEscapeRecursionExplosion {
private Object obj;
public ThisEscapeRecursionExplosion() {
getObject();
}
private Object getObject() {
if (this.obj == null) {
this.obj = new Object();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
getObject().hashCode();
}
return this.obj;
}
}
// JDK-8355753 - @SuppressWarnings("this-escape") not respected for indirect leak via field
public static class SuppressedIndirectLeakViaField {
private final int x = this.mightLeak(); // this leak should be suppressed
private int y;
{
y = this.mightLeak(); // this leak should be suppressed
}
public SuppressedIndirectLeakViaField() {
this("");
}
@SuppressWarnings("this-escape")
private SuppressedIndirectLeakViaField(String s) {
}
public int mightLeak() {
return 0;
}
}
public static class UnsuppressedIndirectLeakViaField {
private final int x = this.mightLeak(); // this leak should not be suppressed
public UnsuppressedIndirectLeakViaField() {
this(""); // this constructor does not trigger the warning
}
@SuppressWarnings("this-escape")
private UnsuppressedIndirectLeakViaField(String s) {
// this constructor does not trigger the warning (obviously; it's directly suppressed)
}
public UnsuppressedIndirectLeakViaField(int z) {
// this constructor triggers the warning
}
public UnsuppressedIndirectLeakViaField(float z) {
// this constructor also triggers the warning, but should not create a duplicate
}
public int mightLeak() {
return 0;
}
}
}
|