1 /*
   2  * Copyright (c) 2014, 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 sun.hotspot.WhiteBox;
  26 
  27 public class JvmtiApp {
  28     static Class forname(String cn) {
  29         try {
  30             return Class.forName(cn);
  31         } catch (Throwable t) {
  32             return null;
  33         }
  34     }
  35 
  36     static void failed(String msg) {
  37         System.out.println("TEST FAILED: " + msg);
  38         System.exit(1);
  39     }
  40 
  41     // See ../JvmtiAddPath.java for how the classpaths are configured.
  42     public static void main(String args[]) {
  43         String cn = "Hello";
  44         if (args.length >= 3) {
  45             cn = args[args.length - 1];
  46         }
  47 
  48         if (args[0].equals("noadd")) {
  49             if (forname(cn) != null) {
  50                 failed(cn + " class was loaded unexpectedly");
  51             }
  52             // We use -verbose:class to verify that Extra.class IS loaded by AppCDS if
  53             // the boot classpath HAS NOT been appended.
  54             ExtraClass.doit();
  55             System.exit(0);
  56         }
  57 
  58         WhiteBox wb = WhiteBox.getWhiteBox();
  59 
  60         if (args[0].equals("bootonly")) {
  61             wb.addToBootstrapClassLoaderSearch(args[1]);
  62             Class cls = forname(cn);
  63             if (cls == null) {
  64                 failed("Cannot find " + cn + " class");
  65             }
  66             if (cls.getClassLoader() != null) {
  67                 failed("Hello class not loaded by boot classloader");
  68             }
  69         } else if (args[0].equals("apponly")) {
  70             wb.addToSystemClassLoaderSearch(args[1]);
  71             Class cls = forname(cn);
  72             if (cls == null) {
  73                 failed("Cannot find " + cn + " class");
  74             }
  75             if (cls.getClassLoader() != JvmtiApp.class.getClassLoader()) {
  76                 failed(cn + " class not loaded by app classloader");
  77             }
  78         } else if (args[0].equals("noadd-appcds")) {
  79             cn = (args.length == 1) ? "Hello" : args[1];
  80             Class cls = forname(cn);
  81             if (cls == null) {
  82                 failed("Cannot find " + cn + " class");
  83             }
  84             if (cls.getClassLoader() != JvmtiApp.class.getClassLoader()) {
  85                 failed(cn + " class not loaded by app classloader");
  86             }
  87         } else if (args[0].equals("appandboot")) {
  88             wb.addToBootstrapClassLoaderSearch(args[1]);
  89             wb.addToSystemClassLoaderSearch(args[2]);
  90             cn = (args.length == 3) ? "Hello" : args[3];
  91             Class cls = forname(cn);
  92             if (cls == null) {
  93                 failed("Cannot find " + cn + " class");
  94             }
  95             if (cls.getClassLoader() != null) {
  96                 failed(cn + " class not loaded by boot classloader");
  97             }
  98         } else {
  99             failed("unknown option " + args[0]);
 100         }
 101 
 102         // We use -verbose:class to verify that Extra.class IS NOT loaded by AppCDS if
 103         // the boot classpath HAS been appended.
 104         ExtraClass.doit();
 105 
 106         System.out.println("Test passed: " + args[0]);
 107     }
 108 }
 109 
 110 class ExtraClass {
 111     static void doit() {}
 112 }