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 * @test
25 * @bug 8194743 8345438 8356551 8349754
26 * @summary Test valid placements of super()/this() in constructors
27 */
28
29 import java.util.concurrent.atomic.AtomicReference;
30
31 public class SuperInitGood {
32
33 SuperInitGood(Object obj) {
34 }
35
36 SuperInitGood(int x) {
37 }
38
39 // Default constructor provided by compiler
40 static class Test0 {
41 }
42
43 // No explicit calls to this()/super()
44 static class Test1 {
45 Test1() {
46 }
47 Test1(int a) {
48 this.hashCode();
49 }
50 }
51
52 // Explicit calls to this()/super()
53 static class Test2<T> {
54 static int i;
55 Test2() {
56 this(0);
57 }
58 Test2(int i) {
59 Test2.i = i;
60 super();
61 }
62 Test2(T obj) {
63 this(java.util.Objects.hashCode(obj));
64 }
65 public T get() {
66 return null;
404 // ignore
405 }
406 }
407 }
408
409 // we allow 'this' reference prior to super() for field assignments only
410 public static class Test20 {
411 private int x;
412 public Test20(short x) {
413 x = x;
414 super();
415 }
416 public Test20(int x) {
417 this.x = x;
418 super();
419 }
420 public Test20(char x) {
421 Test20.this.x = x;
422 super();
423 }
424 public Test20(byte y) {
425 x = y;
426 this((int)y);
427 this.x++;
428 }
429 }
430
431 // allow creating and using local and anonymous classes before super()
432 // they will not have enclosing instances though
433 public static class Test21 {
434 public Test21(int x) {
435 Runnable r = new Runnable() {
436 public void run() {
437 this.hashCode();
438 }
439 };
440 r.run();
441 super();
442 r.run();
443 }
444 public Test21(float x) {
445 class Foo {
446 public void bar() {
447 this.hashCode();
448 }
495 class Local {
496 Local(Test23 Test23.this) {
497 }
498 }
499 super();
500 new Local();
501 }
502 }
503
504 // Test for JDK-8349754
505 public static class Test24 {
506 private int i;
507 class Sub extends Test24 {
508 Sub() {
509 i = 3; // here "i" refers to "Test23.this.i", not "this.i" - so it's OK
510 super();
511 }
512 }
513 }
514
515 public static void main(String[] args) {
516 new Test0();
517 new Test1();
518 new Test1(7);
519 new Test2<Byte>();
520 new Test2<>(args);
521 new Test3();
522 new SuperInitGood(3).new Test5(3);
523 new SuperInitGood(3).new Test6();
524 SuperInitGood.test7("foo");
525 SuperInitGood.test8();
526 new Test9(new SuperInitGood(5), "abc");
527 new Test10(7);
528 new Test11(9);
529 new Test12();
530 new Test13();
531 Test14 t14 = new Test14();
532 assert t14.x == 0 && t14.y == -1 && t14.z == 13;
533 t14 = new Test14(7);
534 assert t14.x == 7 && t14.y == -1 && t14.z == 13;
542 // expected
543 }
544 try {
545 new Test18(true);
546 assert false : "expected exception";
547 } catch (Test18.MyException e) {
548 // expected
549 }
550 try {
551 new Test18(false);
552 } catch (Test18.MyException e) {
553 assert false : "unexpected exception: " + e;
554 }
555 new Test19(123);
556 new Test20(123);
557 new Test21((int)123);
558 new Test21((float)123);
559 new Test22('x');
560 new Test23();
561 new Test24();
562 }
563 }
|
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 * @test
25 * @bug 8194743 8345438 8356551 8349754 8379833
26 * @library /tools/javac/lib
27 * @summary Test valid placements of super()/this() in constructors
28 * @modules jdk.compiler/com.sun.tools.javac.tree
29 * jdk.compiler/com.sun.tools.javac.util
30 * @enablePreview
31 * @run main SuperInitGood
32 */
33
34 import java.util.concurrent.atomic.AtomicReference;
35
36 public class SuperInitGood {
37
38 SuperInitGood(Object obj) {
39 }
40
41 SuperInitGood(int x) {
42 }
43
44 // Default constructor provided by compiler
45 static class Test0 {
46 }
47
48 // No explicit calls to this()/super()
49 static class Test1 {
50 Test1() {
51 }
52
53 Test1(int a) {
54 this.hashCode();
55 }
56 }
57
58 // Explicit calls to this()/super()
59 static class Test2<T> {
60 static int i;
61 Test2() {
62 this(0);
63 }
64 Test2(int i) {
65 Test2.i = i;
66 super();
67 }
68 Test2(T obj) {
69 this(java.util.Objects.hashCode(obj));
70 }
71 public T get() {
72 return null;
410 // ignore
411 }
412 }
413 }
414
415 // we allow 'this' reference prior to super() for field assignments only
416 public static class Test20 {
417 private int x;
418 public Test20(short x) {
419 x = x;
420 super();
421 }
422 public Test20(int x) {
423 this.x = x;
424 super();
425 }
426 public Test20(char x) {
427 Test20.this.x = x;
428 super();
429 }
430 }
431
432 // allow creating and using local and anonymous classes before super()
433 // they will not have enclosing instances though
434 public static class Test21 {
435 public Test21(int x) {
436 Runnable r = new Runnable() {
437 public void run() {
438 this.hashCode();
439 }
440 };
441 r.run();
442 super();
443 r.run();
444 }
445 public Test21(float x) {
446 class Foo {
447 public void bar() {
448 this.hashCode();
449 }
496 class Local {
497 Local(Test23 Test23.this) {
498 }
499 }
500 super();
501 new Local();
502 }
503 }
504
505 // Test for JDK-8349754
506 public static class Test24 {
507 private int i;
508 class Sub extends Test24 {
509 Sub() {
510 i = 3; // here "i" refers to "Test23.this.i", not "this.i" - so it's OK
511 super();
512 }
513 }
514 }
515
516 public static class Test25 {
517 public Test25(Object o) {}
518
519 class Sub extends Test25 {
520 public Sub() {
521 super(new Object() {
522 void foo() {
523 getClass();
524 }
525 });
526 }
527 }
528 }
529
530 public static class Test26 {
531 Test26(Test26 t) {}
532
533 Test26(String s) {
534 this(new Test26());
535 }
536
537 Test26() {}
538
539 public static void main(String[] args) {
540 new Test26("test");
541 }
542 }
543
544 public static void main(String[] args) {
545 new Test0();
546 new Test1();
547 new Test1(7);
548 new Test2<Byte>();
549 new Test2<>(args);
550 new Test3();
551 new SuperInitGood(3).new Test5(3);
552 new SuperInitGood(3).new Test6();
553 SuperInitGood.test7("foo");
554 SuperInitGood.test8();
555 new Test9(new SuperInitGood(5), "abc");
556 new Test10(7);
557 new Test11(9);
558 new Test12();
559 new Test13();
560 Test14 t14 = new Test14();
561 assert t14.x == 0 && t14.y == -1 && t14.z == 13;
562 t14 = new Test14(7);
563 assert t14.x == 7 && t14.y == -1 && t14.z == 13;
571 // expected
572 }
573 try {
574 new Test18(true);
575 assert false : "expected exception";
576 } catch (Test18.MyException e) {
577 // expected
578 }
579 try {
580 new Test18(false);
581 } catch (Test18.MyException e) {
582 assert false : "unexpected exception: " + e;
583 }
584 new Test19(123);
585 new Test20(123);
586 new Test21((int)123);
587 new Test21((float)123);
588 new Test22('x');
589 new Test23();
590 new Test24();
591 new Test25(null);
592 new Test26("");
593 }
594 }
|