Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk003.java
40948 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.jvmti.ClassFileLoadHook;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jvmti.*;2930/**31* Debuggee class of JVMTI test.32*/33public class classfloadhk003 extends DebugeeClass {3435/** Load native library if required. */36static {37System.loadLibrary("classfloadhk003");38}3940/** Run test from command line. */41public static void main(String argv[]) {42argv = nsk.share.jvmti.JVMTITest.commonInit(argv);4344// JCK-compatible exit45System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);46}4748/** Run test from JCK-compatible environment. */49public static int run(String argv[], PrintStream out) {50return new classfloadhk003().runIt(argv, out);51}5253/* =================================================================== */5455/* constant names */56public static final String PACKAGE_NAME = "nsk.jvmti.ClassFileLoadHook";57public static final String TESTED_CLASS_NAME = PACKAGE_NAME + ".classfloadhk003r";5859/* scaffold objects */60ArgumentHandler argHandler = null;61Log log = null;62long timeout = 0;63int status = Consts.TEST_PASSED;6465/* bytecode of loaded class */66public static byte origClassBytes[] = null;6768/* class loader for loaded class */69public static classfloadhk003ClassLoader classLoader = null;7071/** Run debuggee code. */72public int runIt(String argv[], PrintStream out) {73argHandler = new ArgumentHandler(argv);74log = new Log(out, argHandler);75timeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds7677String args[] = argHandler.getArguments();78if (args.length <= 0) {79throw new Failure("Path for tested class file to load not specified");80}8182String location = args[0];83String path = location + File.separator + "loadclass";84log.display("Using path to original tested class: \n\t" + path);8586classLoader = new classfloadhk003ClassLoader(path);8788log.display("Reading original bytecode of tested class: \n\t" + TESTED_CLASS_NAME);89try {90origClassBytes = classLoader.readBytes(path, TESTED_CLASS_NAME);91} catch (IOException e) {92throw new Failure("IOException in reading bytecode of tested class file:\n\t" + e);93}9495log.display("Sync: debugee ready to load tested class");96status = checkStatus(status);9798try {99log.display("Loading tested class: " + TESTED_CLASS_NAME);100Class<?> testedClass = Class.forName(TESTED_CLASS_NAME, true, classLoader);101} catch (ClassNotFoundException e) {102throw new Failure("Tested class not found: \n\t" + e);103}104105log.display("Sync: tested class loaded");106status = checkStatus(status);107108return status;109}110}111112/* =================================================================== */113114/** Classloader for tested class. */115class classfloadhk003ClassLoader extends ClassLoader {116private String path;117118/** Make classloader providing path to class files. */119public classfloadhk003ClassLoader(String path) {120this.path = path;121}122123/** Load specified class. */124protected Class<?> findClass(String name) throws ClassNotFoundException {125try {126byte[] bytes = readBytes(path, name);127return defineClass(name, bytes, 0, bytes.length);128} catch (IOException e) {129throw new ClassNotFoundException("IOException in loading class: " + name, e);130}131}132133/** Read classfile for specified path and class name. */134public static byte[] readBytes(String path, String classname) throws IOException {135String filename = path + File.separator136+ classname.replace('.', File.separatorChar) + ".class";137FileInputStream in = new FileInputStream(filename);138byte[] bytes = new byte[in.available()];139in.read(bytes);140in.close();141return bytes;142}143}144145146