RedefineClassHelper.java revision 2224:2a8815d86b93
133965Sjdp/* 2218822Sdim * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3218822Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 433965Sjdp * 533965Sjdp * This code is free software; you can redistribute it and/or modify it 633965Sjdp * under the terms of the GNU General Public License version 2 only, as 733965Sjdp * published by the Free Software Foundation. 833965Sjdp * 933965Sjdp * This code is distributed in the hope that it will be useful, but WITHOUT 1033965Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1133965Sjdp * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1233965Sjdp * version 2 for more details (a copy is included in the LICENSE file that 1333965Sjdp * accompanied this code). 1433965Sjdp * 1533965Sjdp * You should have received a copy of the GNU General Public License version 1633965Sjdp * 2 along with this work; if not, write to the Free Software Foundation, 1733965Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1833965Sjdp * 1933965Sjdp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2033965Sjdp * or visit www.oracle.com if you need additional information or have any 21218822Sdim * questions. 22218822Sdim */ 2333965Sjdp 2433965Sjdpimport java.io.PrintWriter; 2533965Sjdpimport java.lang.instrument.*; 2633965Sjdpimport jdk.test.lib.InMemoryJavaCompiler; 2733965Sjdp 2833965Sjdp/* 2933965Sjdp * Helper class to write tests that redefine classes. 3033965Sjdp * When main method is run, it will create a redefineagent.jar that can be used 3189857Sobrien * with the -javaagent option to support redefining classes in jtreg tests. 3233965Sjdp * 3333965Sjdp * See sample test in test/testlibrary_tests/RedefineClassTest.java 3433965Sjdp */ 3533965Sjdppublic class RedefineClassHelper { 3633965Sjdp 3733965Sjdp public static Instrumentation instrumentation; 3833965Sjdp public static void premain(String agentArgs, Instrumentation inst) { 3933965Sjdp instrumentation = inst; 4033965Sjdp } 4133965Sjdp 4233965Sjdp /** 4333965Sjdp * Redefine a class 4489857Sobrien * 4533965Sjdp * @param clazz Class to redefine 4633965Sjdp * @param javacode String with the new java code for the class to be redefined 4733965Sjdp */ 4833965Sjdp public static void redefineClass(Class clazz, String javacode) throws Exception { 4933965Sjdp byte[] bytecode = InMemoryJavaCompiler.compile(clazz.getName(), javacode); 5033965Sjdp redefineClass(clazz, bytecode); 5133965Sjdp } 5233965Sjdp 5333965Sjdp /** 5433965Sjdp * Redefine a class 5533965Sjdp * 5633965Sjdp * @param clazz Class to redefine 5733965Sjdp * @param bytecode byte[] with the new class 5833965Sjdp */ 5933965Sjdp public static void redefineClass(Class clazz, byte[] bytecode) throws Exception { 6033965Sjdp instrumentation.redefineClasses(new ClassDefinition(clazz, bytecode)); 6133965Sjdp } 6233965Sjdp 6333965Sjdp /** 6433965Sjdp * Main method to be invoked before test to create the redefineagent.jar 6533965Sjdp */ 6660484Sobrien public static void main(String[] args) throws Exception { 6733965Sjdp ClassFileInstaller.main("RedefineClassHelper"); 6833965Sjdp 6933965Sjdp PrintWriter pw = new PrintWriter("MANIFEST.MF"); 7033965Sjdp pw.println("Premain-Class: RedefineClassHelper"); 7133965Sjdp pw.println("Can-Redefine-Classes: true"); 7233965Sjdp pw.close(); 7333965Sjdp 7433965Sjdp sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); 7533965Sjdp if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineClassHelper.class" })) { 7689857Sobrien throw new Exception("jar operation failed"); 7789857Sobrien } 7889857Sobrien } 7933965Sjdp} 8089857Sobrien