1 /*
 2  * Copyright (c) 2022, 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 Check code generation for value creation ops
27  * @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
28  * @enablePreview
29  * @run main ValueCreationTest
30  */
31 
32 import java.io.PrintWriter;
33 import java.io.StringWriter;
34 import java.nio.file.Paths;
35 
36 public class ValueCreationTest {
37 
38     static final value class Point {
39 
40         final int x;
41         final int y;
42 
43         Point (int x, int y) {
44             this.x = x;
45             this.y = y;
46         }
47 
48         public static void main(String [] args) {
49             Point p = new Point(10, 20);
50         }
51     }
52 
53     public static void main(String[] args) {
54         new ValueCreationTest().run();
55     }
56 
57     void run() {
58         String [] params = new String [] { "-v",
59                                             Paths.get(System.getProperty("test.classes"),
60                                                 "ValueCreationTest$Point.class").toString() };
61         runCheck(params, new String [] {
62 
63          "final value class ValueCreationTest$Point",
64          "flags: (0x0010) ACC_FINAL",
65          "ValueCreationTest$Point(int, int);",
66          "descriptor: (II)V",
67          "flags: (0x0000)",
68          "0: aload_0",
69          "1: iload_1",
70          "2: putfield      #1                  // Field x:I",
71          "5: aload_0",
72          "6: iload_2",
73          "7: putfield      #7                  // Field y:I",
74         "10: aload_0",
75         "11: invokespecial #10                 // Method java/lang/Object.\"<init>\":()V",
76          });
77      }
78 
79     void runCheck(String [] params, String [] expectedOut) {
80         StringWriter s;
81         String out;
82 
83         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
84             com.sun.tools.javap.Main.run(params, pw);
85             out = s.toString();
86         }
87         int errors = 0;
88         for (String eo: expectedOut) {
89             if (!out.contains(eo)) {
90                 System.err.println("Match not found for string: " + eo);
91                 errors++;
92             }
93         }
94         if (errors > 0) {
95             throw new AssertionError("Unexpected javap output: " + out);
96         }
97     }
98 }