1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 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 /*
25  * @test
26  * @summary Smoke test for code reflection with synchronized blocks.
27  * @build SynchronizedTest
28  * @build CodeReflectionTester
29  * @run main CodeReflectionTester SynchronizedTest
30  */
31 
32 import java.lang.runtime.CodeReflection;
33 
34 public class SynchronizedTest {
35     @CodeReflection
36     @IR("""
37             func @"test1" (%0 : SynchronizedTest, %1 : int)int -> {
38                 %2 : Var<int> = var %1 @"i";
39                 java.synchronized
40                     ()SynchronizedTest -> {
41                         yield %0;
42                     }
43                     ()void -> {
44                         %3 : int = var.load %2;
45                         %4 : int = constant @"1";
46                         %5 : int = add %3 %4;
47                         var.store %2 %5;
48                         yield;
49                     };
50                 %6 : int = var.load %2;
51                 return %6;
52             };
53             """)
54     int test1(int i) {
55         synchronized (this) {
56             i++;
57         }
58         return i;
59     }
60 
61     static Object m() {
62         return null;
63     }
64 
65     @CodeReflection
66     @IR("""
67             func @"test2" (%0 : SynchronizedTest, %1 : int)int -> {
68                 %2 : Var<int> = var %1 @"i";
69                 java.synchronized
70                     ()java.lang.Object -> {
71                         %3 : java.lang.Object = invoke @"SynchronizedTest::m()java.lang.Object";
72                         yield %3;
73                     }
74                     ()void -> {
75                         %4 : int = var.load %2;
76                         %5 : int = constant @"1";
77                         %6 : int = add %4 %5;
78                         var.store %2 %6;
79                         yield;
80                     };
81                 %7 : int = var.load %2;
82                 return %7;
83             };
84             """)
85     int test2(int i) {
86         synchronized (m()) {
87             i++;
88         }
89         return i;
90     }
91 
92 }