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