1 /*
2  * Copyright (c) 2017, 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 import java.io.File;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import sun.hotspot.WhiteBox;
29 
30 public class CheckCachedResolvedReferencesApp {
main(String args[])31     public static void main(String args[]) throws Exception {
32         String path = args[0];
33         URL url = new File(path).toURI().toURL();
34         URL[] urls = new URL[] {url};
35 
36         URLClassLoader loader = new URLClassLoader(urls);
37         Class hello = loader.loadClass("Hello");
38         System.out.println("Loaded " + hello + " from " + url + " using loader " + loader);
39 
40         WhiteBox wb = WhiteBox.getWhiteBox();
41 
42         if (!wb.areOpenArchiveHeapObjectsMapped()) {
43             System.out.println("Archived open_archive_heap objects are not mapped.");
44             System.out.println("This may happen during normal operation. Test Skipped.");
45             return;
46         }
47 
48         // CheckCachedResolvedReferencesApp is shared class and loaded by the
49         // AppClassLoader. It should have cached resolved_references.
50         if (wb.isSharedClass(CheckCachedResolvedReferencesApp.class)) {
51             Object refs1 = wb.getResolvedReferences(CheckCachedResolvedReferencesApp.class);
52             if (refs1 != null && wb.isShared(refs1)) {
53                 System.out.println(
54                     "resolved references from CheckCachedResolvedReferencesApp is cached");
55             } else {
56                 throw new RuntimeException(
57                     "FAILED. CheckCachedResolvedReferencesApp has no cached resolved references");
58             }
59         }
60 
61         // Hello is shared class and loaded by the 'loader' defined in current app.
62         // It should not have cached resolved_references.
63         if (wb.isSharedClass(hello)) {
64             Object refs2 = wb.getResolvedReferences(hello);
65             if (refs2 != null) {
66                 if (!wb.isShared(refs2)) {
67                     System.out.println("resolved references from hello is not cached");
68                 } else {
69                     throw new RuntimeException(
70                         "FAILED. Hello has unexpected cached resolved references");
71                 }
72             } else {
73                 throw new RuntimeException("FAILED. Hello has no resolved references");
74             }
75         }
76     }
77 }
78