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  * @modules jdk.incubator.code
28  * @build SynchronizedTest
29  * @build CodeReflectionTester
30  * @run main CodeReflectionTester SynchronizedTest
31  */
32 
33 import jdk.incubator.code.CodeReflection;
34 
35 public class SynchronizedTest {
36     @CodeReflection
37     @IR("""
38             func @"test1" (%0 : java.type:"SynchronizedTest", %1 : java.type:"int")java.type:"int" -> {
39                 %2 : Var<java.type:"int"> = var %1 @"i";
40                 java.synchronized
41                     ()java.type:"SynchronizedTest" -> {
42                         yield %0;
43                     }
44                     ()java.type:"void" -> {
45                         %3 : java.type:"int" = var.load %2;
46                         %4 : java.type:"int" = constant @1;
47                         %5 : java.type:"int" = add %3 %4;
48                         var.store %2 %5;
49                         yield;
50                     };
51                 %6 : java.type:"int" = var.load %2;
52                 return %6;
53             };
54             """)
55     int test1(int i) {
56         synchronized (this) {
57             i++;
58         }
59         return i;
60     }
61 
62     static Object m() {
63         return null;
64     }
65 
66     @CodeReflection
67     @IR("""
68             func @"test2" (%0 : java.type:"SynchronizedTest", %1 : java.type:"int")java.type:"int" -> {
69                 %2 : Var<java.type:"int"> = var %1 @"i";
70                 java.synchronized
71                     ()java.type:"java.lang.Object" -> {
72                         %3 : java.type:"java.lang.Object" = invoke @java.ref:"SynchronizedTest::m():java.lang.Object";
73                         yield %3;
74                     }
75                     ()java.type:"void" -> {
76                         %4 : java.type:"int" = var.load %2;
77                         %5 : java.type:"int" = constant @1;
78                         %6 : java.type:"int" = add %4 %5;
79                         var.store %2 %6;
80                         yield;
81                     };
82                 %7 : java.type:"int" = var.load %2;
83                 return %7;
84             };
85             """)
86     int test2(int i) {
87         synchronized (m()) {
88             i++;
89         }
90         return i;
91     }
92 
93 }