ctornqvi@8050: /* ctornqvi@8050: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. ctornqvi@8050: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ctornqvi@8050: * ctornqvi@8050: * This code is free software; you can redistribute it and/or modify it ctornqvi@8050: * under the terms of the GNU General Public License version 2 only, as ctornqvi@8050: * published by the Free Software Foundation. ctornqvi@8050: * ctornqvi@8050: * This code is distributed in the hope that it will be useful, but WITHOUT ctornqvi@8050: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ctornqvi@8050: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ctornqvi@8050: * version 2 for more details (a copy is included in the LICENSE file that ctornqvi@8050: * accompanied this code). ctornqvi@8050: * ctornqvi@8050: * You should have received a copy of the GNU General Public License version ctornqvi@8050: * 2 along with this work; if not, write to the Free Software Foundation, ctornqvi@8050: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ctornqvi@8050: * ctornqvi@8050: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ctornqvi@8050: * or visit www.oracle.com if you need additional information or have any ctornqvi@8050: * questions. ctornqvi@8050: */ ctornqvi@8050: ctornqvi@8050: import java.io.PrintWriter; ctornqvi@8050: import java.lang.instrument.*; ctornqvi@8050: import com.oracle.java.testlibrary.*; ctornqvi@8050: ctornqvi@8050: /* ctornqvi@8050: * Helper class to write tests that redefine classes. ctornqvi@8050: * When main method is run, it will create a redefineagent.jar that can be used ctornqvi@8050: * with the -javaagent option to support redefining classes in jtreg tests. ctornqvi@8050: * ctornqvi@8050: * See sample test in test/testlibrary_tests/RedefineClassTest.java ctornqvi@8050: */ ctornqvi@8050: public class RedefineClassHelper { ctornqvi@8050: ctornqvi@8050: public static Instrumentation instrumentation; ctornqvi@8050: public static void premain(String agentArgs, Instrumentation inst) { ctornqvi@8050: instrumentation = inst; ctornqvi@8050: } ctornqvi@8050: ctornqvi@8050: /** ctornqvi@8050: * Redefine a class ctornqvi@8050: * ctornqvi@8050: * @param clazz Class to redefine ctornqvi@8050: * @param javacode String with the new java code for the class to be redefined ctornqvi@8050: */ ctornqvi@8050: public static void redefineClass(Class clazz, String javacode) throws Exception { ctornqvi@8050: byte[] bytecode = InMemoryJavaCompiler.compile(clazz.getName(), javacode); ctornqvi@8050: redefineClass(clazz, bytecode); ctornqvi@8050: } ctornqvi@8050: ctornqvi@8050: /** ctornqvi@8050: * Redefine a class ctornqvi@8050: * ctornqvi@8050: * @param clazz Class to redefine ctornqvi@8050: * @param bytecode byte[] with the new class ctornqvi@8050: */ ctornqvi@8050: public static void redefineClass(Class clazz, byte[] bytecode) throws Exception { ctornqvi@8050: instrumentation.redefineClasses(new ClassDefinition(clazz, bytecode)); ctornqvi@8050: } ctornqvi@8050: ctornqvi@8050: /** ctornqvi@8050: * Main method to be invoked before test to create the redefineagent.jar ctornqvi@8050: */ ctornqvi@8050: public static void main(String[] args) throws Exception { ctornqvi@8050: ClassFileInstaller.main("RedefineClassHelper"); ctornqvi@8050: ctornqvi@8050: PrintWriter pw = new PrintWriter("MANIFEST.MF"); ctornqvi@8050: pw.println("Premain-Class: RedefineClassHelper"); ctornqvi@8050: pw.println("Can-Redefine-Classes: true"); ctornqvi@8050: pw.close(); ctornqvi@8050: ctornqvi@8050: sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); ctornqvi@8050: if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineClassHelper.class" })) { ctornqvi@8050: throw new Exception("jar operation failed"); ctornqvi@8050: } ctornqvi@8050: } ctornqvi@8050: }