1 /*
 2  * Copyright (c) 2021, Alibaba Group Holding Limited. 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 package runtime.valhalla.inlinetypes;
25 
26 import jdk.test.lib.process.ProcessTools;
27 import jdk.test.lib.process.OutputAnalyzer;
28 import jdk.test.lib.JDKToolFinder;
29 import jdk.internal.vm.annotation.LooselyConsistentValue;
30 import jdk.internal.vm.annotation.ImplicitlyConstructible;
31 import jdk.internal.vm.annotation.NullRestricted;
32 
33 /*
34  * @test
35  * @summary Test the VM.class_print_layout command
36  * @library /test/lib
37  * @modules java.base/jdk.internal.vm.annotation
38  * @enablePreview
39  * @compile ClassPrintLayoutDcmd.java
40  * @run main/othervm runtime.valhalla.inlinetypes.ClassPrintLayoutDcmd
41  */
42 
43 public value class ClassPrintLayoutDcmd {
44 
45     @ImplicitlyConstructible
46     @LooselyConsistentValue
47     static value class Point {
48         int i = 0;
49         int j = 0;
50     }
51 
52     @ImplicitlyConstructible
53     @LooselyConsistentValue
54     static value class Line {
55         @NullRestricted
56         Point p1;
57         @NullRestricted
58         Point p2;
59         Line() {
60             this.p1 = this.p2 = new Point();
61         }
62     }
63     static {
64         try {
65             Class.forName(Line.class.getName());
66         } catch(Throwable e) {
67             throw new RuntimeException("failed to find class");
68         }
69     }
70     static ProcessBuilder pb = new ProcessBuilder();
71 
72     static void testCmd(String arg, int expectExitCode, String... expectStrings) throws Exception {
73         // Grab my own PID
74         String pid = Long.toString(ProcessTools.getProcessId());
75 
76         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.class_print_layout", arg });
77         OutputAnalyzer output = new OutputAnalyzer(pb.start());
78         output.shouldHaveExitValue(expectExitCode);
79         for (String expectString : expectStrings) {
80             output.shouldContain(expectString);
81         }
82     }
83 
84     public static void main(String args[]) throws Exception {
85         testCmd("foo/bar", 0, "");
86         testCmd("", 1, "IllegalArgumentException", "mandatory");
87         testCmd("java/lang/Object", 0, "java/lang/Object", "@bootstrap");
88         testCmd("java/lang/Class", 0, "java/lang/Class", "@bootstrap");
89         testCmd("runtime/valhalla/inlinetypes/ClassPrintLayoutDcmd$Line", 0, "@app", "p1", "p2");
90     }
91 }