1 /*
 2  * @test /nodynamiccopyright/
 3  * @bug 8279672
 4  * @summary Implement semantic checks for value classes
 5  * @compile/fail/ref=SemanticsViolationsTest.out -XDrawDiagnostics --should-stop=at=FLOW -XDdev SemanticsViolationsTest.java
 6  */
 7 
 8 public class SemanticsViolationsTest {
 9 
10     // A value class is implicitly final, so cannot be extended.
11     value class Base {}
12     class Subclass extends Base {} // Error: Base is implicitly final, cannot be extended.
13 
14 
15     // The class may not be declared abstract.
16     abstract value class AbsValue {}  // Error: value class cannot be abstract
17     value interface ValueInterface {} // Error: interface cannot modified with value.
18 
19     // All instance fields are implicitly final, so must be assigned exactly
20     // once by constructors or initializers, and cannot be assigned outside
21     // of a constructor or initializer.
22     value class Point {
23 
24         int x = 10;
25         int y;
26         int z;
27 
28         Point (int x, int y, int z) {
29             this.x = x; // Error, final field 'x' is already assigned to.
30             this.y = y; // OK.
31             // Error, final z is unassigned.
32         }
33         void foo(Point p) {
34             this.y = p.y; // Error, y is final and can't be written outside of ctor.
35         }
36     }
37 
38     // The class does not implement—directly or indirectly—IdentityObject.
39     // This implies that the superclass is either Object or a stateless abstract class.
40     value class IdentityValue implements IdentityObject { // Error, can't implement this
41     }
42     value class IdentityValue2 extends SemanticsViolationsTest { // Error, can't extend identity class
43     }
44     abstract static class AbstractWithState {
45        int xx;
46     }
47     value class BrokenValue3 extends AbstractWithState { // Error, super class has state.
48     }
49     abstract static class AbstractWithoutState {
50         static int ss;
51     }
52     value class GoodValue1 extends AbstractWithoutState {}
53     value class GoodValue2 extends Object {} // allowed.
54 
55     // No constructor makes a super constructor call. Instance creation will
56     // occur without executing any superclass initialization code.
57     value class BrokenValue4 {
58         BrokenValue4() {
59             super(); // Error, can't chain to super's ctor.
60         }
61     }
62 
63     // No instance methods are declared synchronized.
64     value class BrokenValue5 {
65         synchronized void foo() {} // Error;
66         synchronized static void soo() {} // OK.
67         { synchronized(this) { /* Error.*/  } }
68     }
69 
70     // The class does not declare a finalize() method.
71     value class BrokenValue6 {
72         public void finalize() {} // Error
73     }
74 
75     // (Possibly) The constructor does not make use of this except to set
76     // the fields in the constructor body, or perhaps after all fields are
77     // definitely assigned.
78     value class BrokenValue7 {
79         int x;
80         BrokenValue7() {
81             foo(this); // Error.
82             x = 10;
83             foo(this); // Ok.
84         }
85         void foo(BrokenValue7 bv) {
86         }
87     }
88 
89     value record BrokenValue8(int x, int y) {
90         synchronized void foo() { } // Error;
91         synchronized static void soo() {} // OK.
92     }
93 }