1 /*
   2  * Copyright (c) 2019, 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
  26  * @bug 8212117
  27  * @summary Verify JVMTI GetClassStatus returns CLASS_PREPARE after call to Class.forName()
  28  * @run main/othervm/native -agentlib:ClassStatus ClassStatus
  29  */
  30 
  31 
  32 public class ClassStatus {
  33     static {
  34         try {
  35             System.out.println("ClassStatus static block");
  36             System.loadLibrary("ClassStatus");
  37         } catch (UnsatisfiedLinkError ule) {
  38             System.err.println("Could not load ClassStatus library");
  39             System.err.println("java.library.path: "
  40                 + System.getProperty("java.library.path"));
  41             throw ule;
  42         }
  43     }
  44 
  45     static native int check(Class klass);
  46 
  47     public static void main(String[] args) throws ClassNotFoundException {
  48         ClassLoader loader = ClassStatus.class.getClassLoader();
  49         Module module = loader.getUnnamedModule();
  50 
  51         // Load class, but don't initialize it
  52         Class foo2 = Class.forName(module, "Foo2");
  53         Class foo3 = Class.forName("Foo3", false, loader);
  54 
  55         System.out.println("Loaded: " + foo2);
  56         System.out.println("Loaded: " + foo3);
  57 
  58         int status2 = check(foo2);
  59         int status3 = check(foo3);
  60 
  61         new Foo2().bar();
  62         new Foo3().bar();
  63 
  64         if (status2 != 0) {
  65             System.out.println("The agent returned non-zero exit status for Foo2: " + status2);
  66         }
  67         if (status3 != 0) {
  68             System.out.println("The agent returned non-zero exit status for Foo3: " + status3);
  69         }
  70         if (status2 != 0 || status3 != 0) {
  71             throw new RuntimeException("Non-zero status returned from the agent");
  72         }
  73     }
  74 }
  75 
  76 class Foo2 {
  77     static {
  78         System.out.println("Foo2 is initialized");
  79     }
  80     void bar() {
  81         System.out.println("Foo2.bar() is called");
  82     }
  83 }
  84 
  85 class Foo3 {
  86     static {
  87         System.out.println("Foo3 is initialized");
  88     }
  89     void bar() {
  90         System.out.println("Foo3.bar() is called");
  91     }
  92 }