Tue, 03 Mar 2020 12:57:23 +0000
Merge
ctornqvi@8050 | 1 | /* |
ctornqvi@8050 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
ctornqvi@8050 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ctornqvi@8050 | 4 | * |
ctornqvi@8050 | 5 | * This code is free software; you can redistribute it and/or modify it |
ctornqvi@8050 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ctornqvi@8050 | 7 | * published by the Free Software Foundation. |
ctornqvi@8050 | 8 | * |
ctornqvi@8050 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ctornqvi@8050 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ctornqvi@8050 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ctornqvi@8050 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
ctornqvi@8050 | 13 | * accompanied this code). |
ctornqvi@8050 | 14 | * |
ctornqvi@8050 | 15 | * You should have received a copy of the GNU General Public License version |
ctornqvi@8050 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
ctornqvi@8050 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ctornqvi@8050 | 18 | * |
ctornqvi@8050 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ctornqvi@8050 | 20 | * or visit www.oracle.com if you need additional information or have any |
ctornqvi@8050 | 21 | * questions. |
ctornqvi@8050 | 22 | */ |
ctornqvi@8050 | 23 | |
ctornqvi@8050 | 24 | import java.io.PrintWriter; |
ctornqvi@8050 | 25 | import java.lang.instrument.*; |
ctornqvi@8050 | 26 | import com.oracle.java.testlibrary.*; |
ctornqvi@8050 | 27 | |
ctornqvi@8050 | 28 | /* |
ctornqvi@8050 | 29 | * Helper class to write tests that redefine classes. |
ctornqvi@8050 | 30 | * When main method is run, it will create a redefineagent.jar that can be used |
ctornqvi@8050 | 31 | * with the -javaagent option to support redefining classes in jtreg tests. |
ctornqvi@8050 | 32 | * |
ctornqvi@8050 | 33 | * See sample test in test/testlibrary_tests/RedefineClassTest.java |
ctornqvi@8050 | 34 | */ |
ctornqvi@8050 | 35 | public class RedefineClassHelper { |
ctornqvi@8050 | 36 | |
ctornqvi@8050 | 37 | public static Instrumentation instrumentation; |
ctornqvi@8050 | 38 | public static void premain(String agentArgs, Instrumentation inst) { |
ctornqvi@8050 | 39 | instrumentation = inst; |
ctornqvi@8050 | 40 | } |
ctornqvi@8050 | 41 | |
ctornqvi@8050 | 42 | /** |
ctornqvi@8050 | 43 | * Redefine a class |
ctornqvi@8050 | 44 | * |
ctornqvi@8050 | 45 | * @param clazz Class to redefine |
ctornqvi@8050 | 46 | * @param javacode String with the new java code for the class to be redefined |
ctornqvi@8050 | 47 | */ |
ctornqvi@8050 | 48 | public static void redefineClass(Class clazz, String javacode) throws Exception { |
ctornqvi@8050 | 49 | byte[] bytecode = InMemoryJavaCompiler.compile(clazz.getName(), javacode); |
ctornqvi@8050 | 50 | redefineClass(clazz, bytecode); |
ctornqvi@8050 | 51 | } |
ctornqvi@8050 | 52 | |
ctornqvi@8050 | 53 | /** |
ctornqvi@8050 | 54 | * Redefine a class |
ctornqvi@8050 | 55 | * |
ctornqvi@8050 | 56 | * @param clazz Class to redefine |
ctornqvi@8050 | 57 | * @param bytecode byte[] with the new class |
ctornqvi@8050 | 58 | */ |
ctornqvi@8050 | 59 | public static void redefineClass(Class clazz, byte[] bytecode) throws Exception { |
ctornqvi@8050 | 60 | instrumentation.redefineClasses(new ClassDefinition(clazz, bytecode)); |
ctornqvi@8050 | 61 | } |
ctornqvi@8050 | 62 | |
ctornqvi@8050 | 63 | /** |
ctornqvi@8050 | 64 | * Main method to be invoked before test to create the redefineagent.jar |
ctornqvi@8050 | 65 | */ |
ctornqvi@8050 | 66 | public static void main(String[] args) throws Exception { |
ctornqvi@8050 | 67 | ClassFileInstaller.main("RedefineClassHelper"); |
ctornqvi@8050 | 68 | |
ctornqvi@8050 | 69 | PrintWriter pw = new PrintWriter("MANIFEST.MF"); |
ctornqvi@8050 | 70 | pw.println("Premain-Class: RedefineClassHelper"); |
ctornqvi@8050 | 71 | pw.println("Can-Redefine-Classes: true"); |
ctornqvi@8050 | 72 | pw.close(); |
ctornqvi@8050 | 73 | |
ctornqvi@8050 | 74 | sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); |
ctornqvi@8050 | 75 | if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineClassHelper.class" })) { |
ctornqvi@8050 | 76 | throw new Exception("jar operation failed"); |
ctornqvi@8050 | 77 | } |
ctornqvi@8050 | 78 | } |
ctornqvi@8050 | 79 | } |