1 /*
2  * Copyright (c) 2016, 2018, 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 /*
26  * @test
27  * @summary DumpLoadedClassList should exclude generated classes, classes in bootclasspath/a and
28  *          --patch-module.
29  * @requires vm.cds
30  * @library /test/lib
31  * @modules java.base/jdk.internal.misc
32  *          jdk.jartool/sun.tools.jar
33  * @compile test-classes/ArrayListTest.java
34  * @run driver DumpClassList
35  */
36 
37 import jdk.test.lib.compiler.InMemoryJavaCompiler;
38 import jdk.test.lib.process.OutputAnalyzer;
39 import jdk.test.lib.process.ProcessTools;
40 
41 public class DumpClassList {
main(String[] args)42     public static void main(String[] args) throws Exception {
43         // build The app
44         String[] appClass = new String[] {"ArrayListTest"};
45         String classList = "app.list";
46 
47         JarBuilder.build("app", appClass[0]);
48         String appJar = TestCommon.getTestJar("app.jar");
49 
50         // build patch-module
51         String source = "package java.lang; "                       +
52                         "public class NewClass { "                  +
53                         "    static { "                             +
54                         "        System.out.println(\"NewClass\"); "+
55                         "    } "                                    +
56                         "}";
57 
58         ClassFileInstaller.writeClassToDisk("java/lang/NewClass",
59              InMemoryJavaCompiler.compile("java.lang.NewClass", source, "--patch-module=java.base"),
60              System.getProperty("test.classes"));
61 
62         String patchJar = JarBuilder.build("javabase", "java/lang/NewClass");
63 
64         // build bootclasspath/a
65         String source2 = "package boot.append; "                 +
66                         "public class Foo { "                    +
67                         "    static { "                          +
68                         "        System.out.println(\"Foo\"); "  +
69                         "    } "                                 +
70                         "}";
71 
72         ClassFileInstaller.writeClassToDisk("boot/append/Foo",
73              InMemoryJavaCompiler.compile("boot.append.Foo", source2),
74              System.getProperty("test.classes"));
75 
76         String appendJar = JarBuilder.build("bootappend", "boot/append/Foo");
77 
78         // dump class list
79         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
80             true,
81             "-XX:DumpLoadedClassList=" + classList,
82             "--patch-module=java.base=" + patchJar,
83             "-Xbootclasspath/a:" + appendJar,
84             "-cp",
85             appJar,
86             appClass[0]);
87         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dumpClassList");
88         TestCommon.checkExecReturn(output, 0, true,
89                                    "hello world",
90                                    "skip writing class java/lang/NewClass") // skip classes outside of jrt image
91             .shouldNotContain("skip writing class boot/append/Foo");        // but classes on -Xbootclasspath/a should not be skipped
92 
93         output = TestCommon.createArchive(appJar, appClass,
94                                           "-Xbootclasspath/a:" + appendJar,
95                                           "-Xlog:class+load",
96                                           "-XX:SharedClassListFile=" + classList);
97         TestCommon.checkDump(output)
98             .shouldNotContain("Preload Warning: Cannot find java/lang/invoke/LambdaForm")
99             .shouldNotContain("Preload Warning: Cannot find boot/append/Foo")
100             .shouldContain("[info][class,load] boot.append.Foo");
101     }
102 }
103