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