1 /* 2 * @test /nodynamiccopyright/ 3 * @summary Check behavior of synzhronized key word on value classes instances and methods. 4 * @enablePreview 5 * @compile/fail/ref=CheckSynchronized.out -XDrawDiagnostics CheckSynchronized.java 6 */ 7 8 value final class CheckSynchronized implements java.io.Serializable { 9 synchronized void foo() { // <<-- ERROR, no monitor associated with `this' 10 } 11 void goo() { 12 synchronized(this) {} // <<-- ERROR, no monitor associated with `this' 13 } 14 synchronized static void zoo(CheckSynchronized cs) { // OK, static method. 15 synchronized(cs) { // <<-- ERROR, no monitor associated with value class instance. 16 } 17 18 CheckSynchronized csr = cs; 19 synchronized(csr) { 20 // Error, no identity. 21 } 22 23 synchronized(x) { 24 // Error, no identity. 25 } 26 27 Object o = cs; 28 synchronized(o) { 29 // Error BUT not discernible at compile time 30 } 31 java.io.Serializable jis = cs; 32 synchronized(jis) { 33 // Error BUT not discernible at compile time 34 } 35 } 36 static int x = 10; 37 38 value record CheckSynchronizedRecord(int x, int y) { 39 synchronized void foo() { // <<-- ERROR, no monitor associated with `this' 40 } 41 synchronized static void zoo() { // OK, static method. 42 } 43 } 44 }