1 /*
   2  * Copyright (c) 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 import java.io.File;
  26 import java.net.URL;
  27 import java.net.URLClassLoader;
  28 import sun.hotspot.WhiteBox;
  29 
  30 //
  31 // Test class mirror objects are cached when open archive heap objects are mapped:
  32 //  - Well-known shared library classes:
  33 //      java.lang.Object
  34 //      java.lang.String
  35 //  - Shared application class loaded by the system class loader
  36 //  - Shared application class loaded user defined class loader
  37 //
  38 public class CheckCachedMirrorApp {
  39     static WhiteBox wb;
  40     public static void main(String args[]) throws Exception {
  41         String path = args[0];
  42         URL url = new File(path).toURI().toURL();
  43         URL[] urls = new URL[] {url};
  44 
  45         URLClassLoader loader = new URLClassLoader(urls);
  46         Class hello = loader.loadClass("Hello");
  47         System.out.println("Loaded " + hello + " from " + url + " using loader " + loader);
  48 
  49         wb = WhiteBox.getWhiteBox();
  50 
  51         if (!wb.areOpenArchiveHeapObjectsMapped()) {
  52             System.out.println("Archived open_archive_heap objects are not mapped.");
  53             System.out.println("This may happen during normal operation. Test Skipped.");
  54             return;
  55         }
  56 
  57         // Well-known shared library classes
  58         Class object_class = Object.class;
  59         checkMirror(object_class, true);
  60         Class string_class = String.class;
  61         checkMirror(string_class, true);
  62 
  63         // Shared app class
  64         Class app_class = CheckCachedMirrorApp.class;
  65         checkMirror(app_class, true);
  66 
  67         // Hello is shared class and loaded by the 'loader' defined in current app.
  68         // It should not have cached resolved_references.
  69         Class class_with_user_defined_loader = hello;
  70         checkMirror(class_with_user_defined_loader, false);
  71     }
  72 
  73     static void checkMirror(Class c, boolean mirrorShouldBeArchived) {
  74         System.out.print("Check cached mirror for " + c);
  75         if (wb.isSharedClass(c)) {
  76             // Check if the Class object is cached
  77             if (mirrorShouldBeArchived && wb.isShared(c)) {
  78                 System.out.println(c + " mirror is cached. Expected.");
  79             } else if (!mirrorShouldBeArchived && !wb.isShared(c)) {
  80                 System.out.println(c + " mirror is not cached. Expected.");
  81             } else if (mirrorShouldBeArchived && !wb.isShared(c)) {
  82                 throw new RuntimeException(
  83                     "FAILED. " + c + " mirror is not cached.");
  84             } else {
  85                 throw new RuntimeException(
  86                     "FAILED. " + c + " mirror should not be cached.");
  87             }
  88         } else {
  89           System.out.println("Class " + c + "is not shared, skipping the check for mirror");
  90         }
  91     }
  92 }