Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk005.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.*;26import java.lang.reflect.*;2728import nsk.share.*;29import nsk.share.jvmti.*;3031/**32* Debuggee class of JVMTI test.33*/34public class classfloadhk005 extends DebugeeClass {3536/** Load native library if required. */37static {38System.loadLibrary("classfloadhk005");39}4041/** Run test from command line. */42public static void main(String argv[]) {43argv = nsk.share.jvmti.JVMTITest.commonInit(argv);4445// JCK-compatible exit46System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);47}4849/** Run test from JCK-compatible environment. */50public static int run(String argv[], PrintStream out) {51return new classfloadhk005().runIt(argv, out);52}5354/* =================================================================== */5556/* constant names */57public static final String PACKAGE_NAME = "nsk.jvmti.ClassFileLoadHook";58public static final String TESTED_CLASS_NAME = PACKAGE_NAME + ".classfloadhk005r";5960/* scaffold objects */61ArgumentHandler argHandler = null;62Log log = null;63long timeout = 0;64int status = Consts.TEST_PASSED;6566/* new bytecode of tested class */67public static byte newClassBytes[] = null;6869/* tested method name */70public static final String TESTED_METHOD_NAME = "testedStaticMethod";7172/* possible values returned by tested method */73public static final long ORIG_VALUE = 20;74public static final long NEW_VALUE = 2200;7576/** Run debuggee code. */77public int runIt(String argv[], PrintStream out) {78argHandler = new ArgumentHandler(argv);79log = new Log(out, argHandler);80timeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds8182String args[] = argHandler.getArguments();83if (args.length <= 0) {84throw new Failure("Path for tested class file to load not specified");85}8687String location = args[0];88String origPath = location + File.separator + "loadclass";89log.display("Using path to original class: \n\t" + origPath);90String newPath = location + File.separator + "newclass";91log.display("Using path to instrumented class: \n\t" + newPath);9293classfloadhk005ClassLoader classLoader = new classfloadhk005ClassLoader(origPath);9495log.display("Reading instrumented bytecode of tested class: \n\t" + TESTED_CLASS_NAME);96try {97newClassBytes = classLoader.readBytes(newPath, TESTED_CLASS_NAME);98} catch (IOException e) {99throw new Failure("IOException in reading instrumented bytecode of tested class file:\n\t" + e);100}101102log.display("Sync: debugee ready to load tested class");103status = checkStatus(status);104105Class<?> testedClass = null;106try {107log.display("Loading original tested class: " + TESTED_CLASS_NAME);108testedClass = Class.forName(TESTED_CLASS_NAME, true, classLoader);109} catch (ClassNotFoundException e) {110throw new Failure("Tested class not found: \n\t" + e);111}112113log.display("Sync: tested class loaded");114status = checkStatus(status);115116log.display("Checking if the insrumented class was actually loaded");117try {118Method meth = testedClass.getMethod(TESTED_METHOD_NAME, new Class[0]);119Object res = meth.invoke(null, new Object[0]);120if (!(res instanceof Long)) {121log.complain("Tested method of instrumented class retured not Long value: " + res);122log.complain("The tested class was instrumented incorrectly!");123status = Consts.TEST_FAILED;124} else {125long value = ((Long)res).longValue();126log.display("Tested method of instrumented class returned value: " + value);127128if (value == ORIG_VALUE) {129log.complain("Tested method of instrumented class returned original value: " + value);130log.complain("The tested class was not instrumented!");131status = Consts.TEST_FAILED;132} else if (value != NEW_VALUE) {133log.complain("Tested method of instrumented class returned unexpected value: " + value);134log.complain("The tested class was instrumented incorrectly!");135status = Consts.TEST_FAILED;136} else {137log.display("Tested method of instrumented class returned expected new value: " + value);138log.display("The tested class was instrumented correctly!");139}140}141} catch (NoSuchMethodException e) {142log.complain("No tested method found in instrumented class:\n\t" + e);143status = Consts.TEST_FAILED;144} catch (Exception e) {145log.complain("Exception in invoking method of instrumented class:\n\t" + e);146status = Consts.TEST_FAILED;147}148149return status;150}151}152153/* =================================================================== */154155/** Classloader for tested class. */156class classfloadhk005ClassLoader extends ClassLoader {157private String path;158159/** Make classloader providing path to class files. */160public classfloadhk005ClassLoader(String path) {161this.path = path;162}163164/** Load specified class. */165protected Class<?> findClass(String name) throws ClassNotFoundException {166try {167byte[] bytes = readBytes(path, name);168return defineClass(name, bytes, 0, bytes.length);169} catch (IOException e) {170throw new ClassNotFoundException("IOException in loading class: " + name, e);171}172}173174/** Read classfile for specified path and class name. */175public static byte[] readBytes(String path, String classname) throws IOException {176String filename = path + File.separator177+ classname.replace('.', File.separatorChar) + ".class";178FileInputStream in = new FileInputStream(filename);179byte[] bytes = new byte[in.available()];180in.read(bytes);181in.close();182return bytes;183}184}185186187