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 8335770
 26  * @summary improve javap code coverage for value classes
 27  * @enablePreview
 28  * @library /tools/lib
 29  * @modules jdk.jdeps/com.sun.tools.javap
 30  * @build toolbox.ToolBox toolbox.JavapTask
 31  * @run main ValueClassesJavapTest
 32  */
 33 
 34 import java.nio.file.*;
 35 import java.util.*;
 36 
 37 import toolbox.JavapTask;
 38 import toolbox.TestRunner;
 39 import toolbox.ToolBox;
 40 import toolbox.Task;
 41 
 42 public class ValueClassesJavapTest extends TestRunner {
 43     ToolBox tb = new ToolBox();
 44 
 45     abstract value class AbstractValueClass {}
 46     value class ValueClass {}
 47     class IdentityClass {
 48         ValueClass val;
 49     }
 50     class IdentityClass2 {
 51         void m(ValueClass param) {}
 52     }
 53 
 54     private static final List<String> expectedOutput1 = List.of(
 55             "Compiled from \"ValueClassesJavapTest.java\"",
 56             "final value class ValueClassesJavapTest$ValueClass {",
 57             "  ValueClassesJavapTest$ValueClass(ValueClassesJavapTest);",
 58             "}");
 59     private static final List<String> expectedOutput2 = List.of(
 60             "Compiled from \"ValueClassesJavapTest.java\"",
 61             "abstract value class ValueClassesJavapTest$AbstractValueClass {",
 62             "  ValueClassesJavapTest$AbstractValueClass(ValueClassesJavapTest);",
 63             "}");
 64     private static final List<String> expectedOutput3 = List.of(
 65             "LoadableDescriptors:",
 66             "  LValueClassesJavapTest$ValueClass;"
 67     );
 68 
 69     ValueClassesJavapTest() throws Exception {
 70         super(System.err);
 71     }
 72 
 73     public static void main(String... args) throws Exception {
 74         ValueClassesJavapTest tester = new ValueClassesJavapTest();
 75         tester.runTests();
 76     }
 77 
 78     protected void runTests() throws Exception {
 79         runTests(m -> new Object[] { Paths.get(m.getName()) });
 80     }
 81 
 82     @Test
 83     public void testMain(Path base) throws Exception {
 84         Path testClassesPath = Paths.get(System.getProperty("test.classes"));
 85         List<String> output = new JavapTask(tb)
 86                 .options("-p", testClassesPath.resolve(this.getClass().getSimpleName() + "$ValueClass.class").toString())
 87                 .run()
 88                 .writeAll()
 89                 .getOutputLines(Task.OutputKind.DIRECT);
 90         System.out.println(output);
 91         if (!output.equals(expectedOutput1)) {
 92             throw new AssertionError(String.format("unexpected output:\n %s", output));
 93         }
 94 
 95         output = new JavapTask(tb)
 96                 .options("-p", testClassesPath.resolve(this.getClass().getSimpleName() + "$AbstractValueClass.class").toString())
 97                 .run()
 98                 .writeAll()
 99                 .getOutputLines(Task.OutputKind.DIRECT);
100         System.out.println(output);
101         if (!output.equals(expectedOutput2)) {
102             throw new AssertionError(String.format("unexpected output:\n %s", output));
103         }
104 
105         output = new JavapTask(tb)
106                 .options("-p", "-v", testClassesPath.resolve(this.getClass().getSimpleName() + "$IdentityClass.class").toString())
107                 .run()
108                 .writeAll()
109                 .getOutputLines(Task.OutputKind.DIRECT);
110         System.out.println(output);
111         if (!output.containsAll(expectedOutput3)) {
112             throw new AssertionError(String.format("unexpected output:\n %s", output));
113         }
114 
115         output = new JavapTask(tb)
116                 .options("-p", "-v", testClassesPath.resolve(this.getClass().getSimpleName() + "$IdentityClass2.class").toString())
117                 .run()
118                 .writeAll()
119                 .getOutputLines(Task.OutputKind.DIRECT);
120         System.out.println(output);
121         if (!output.containsAll(expectedOutput3)) {
122             throw new AssertionError(String.format("unexpected output:\n %s", output));
123         }
124     }
125 }