Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk007.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 classfloadhk007 extends DebugeeClass {3536/** Load native library if required. */37static {38System.loadLibrary("classfloadhk007");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 classfloadhk007().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 + ".classfloadhk007r";5960/* scaffold objects */61ArgumentHandler argHandler = null;62Log log = null;63long timeout = 0;64int status = Consts.TEST_PASSED;6566/* bytecode of redefined class */67public static byte redefClassBytes[] = null;6869/* tested class to de redefined */70public static Class<?> testedClass = null;7172/* custom classloader to load tested class */73public static classfloadhk007ClassLoader classLoader = null;7475/* tested method name */76public static final String TESTED_METHOD_NAME = "testedStaticMethod";7778/* possible values returned by tested method */79public static final long ORIG_VALUE = 20;80public static final long NEW_VALUE = 12;8182/** Run debuggee code. */83public int runIt(String argv[], PrintStream out) {84argHandler = new ArgumentHandler(argv);85log = new Log(out, argHandler);86timeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds8788String args[] = argHandler.getArguments();89if (args.length <= 0) {90throw new Failure("Path for tested class file to load not specified");91}9293String location = args[0];94String origPath = location + File.separator + "loadclass";95log.display("Using path to original class: \n\t" + origPath);96String redefPath = location + File.separator + "newclass";97log.display("Using path to redefined class: \n\t" + redefPath);9899classLoader = new classfloadhk007ClassLoader(origPath);100101log.display("Reading redefined bytecode of tested class: \n\t" + TESTED_CLASS_NAME);102try {103redefClassBytes = classLoader.readBytes(redefPath, TESTED_CLASS_NAME);104} catch (IOException e) {105throw new Failure("IOException in reading redefined bytecode of tested class file:\n\t" + e);106}107108try {109log.display("Loading original tested class: " + TESTED_CLASS_NAME);110testedClass = Class.forName(TESTED_CLASS_NAME, true, classLoader);111} catch (ClassNotFoundException e) {112throw new Failure("Tested class not found: \n\t" + e);113}114115log.display("Sync: tested class loaded");116status = checkStatus(status);117118log.display("Checking if the tested class was actually redefined");119try {120Method meth = testedClass.getMethod(TESTED_METHOD_NAME, new Class[0]);121Object res = meth.invoke(null, new Object[0]);122if (!(res instanceof Long)) {123log.complain("Tested method of redefined class retured not Long value: " + res);124log.complain("The tested class was redefined incorrectly!");125status = Consts.TEST_FAILED;126} else {127long value = ((Long)res).longValue();128log.display("Tested method of redefined class returned value: " + value);129130if (value == ORIG_VALUE) {131log.complain("Tested method of redefined class returned original value: " + value);132log.complain("The tested class was not redefined!");133status = Consts.TEST_FAILED;134} else if (value != NEW_VALUE) {135log.complain("Tested method of redefined class returned unexpected value: " + value);136log.complain("The tested class was redefined incorrectly!");137status = Consts.TEST_FAILED;138} else {139log.display("Tested method of redefined class returned expected new value: " + value);140log.display("The tested class was redefined correctly!");141}142}143} catch (NoSuchMethodException e) {144log.complain("No tested method found in redefined class:\n\t" + e);145status = Consts.TEST_FAILED;146} catch (Exception e) {147log.complain("Exception in invoking method of redefined class:\n\t" + e);148status = Consts.TEST_FAILED;149}150151return status;152}153}154155/* =================================================================== */156157/** Classloader for tested class. */158class classfloadhk007ClassLoader extends ClassLoader {159private String path;160161/** Make classloader providing path to class files. */162public classfloadhk007ClassLoader(String path) {163this.path = path;164}165166/** Load specified class. */167protected Class<?> findClass(String name) throws ClassNotFoundException {168try {169byte[] bytes = readBytes(path, name);170return defineClass(name, bytes, 0, bytes.length);171} catch (IOException e) {172throw new ClassNotFoundException("IOException in loading class: " + name, e);173}174}175176/** Read classfile for specified path and class name. */177public static byte[] readBytes(String path, String classname) throws IOException {178String filename = path + File.separator179+ classname.replace('.', File.separatorChar) + ".class";180FileInputStream in = new FileInputStream(filename);181byte[] bytes = new byte[in.available()];182in.read(bytes);183in.close();184return bytes;185}186}187188189