Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/ClassVersionAfterRedefine.java
41153 views
1
/*
2
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8267555
27
* @requires vm.jvmti
28
* @summary Class redefinition with a different class file version
29
* @library /test/lib
30
* @compile TestClassOld.jasm TestClassNew.jasm
31
* @run main RedefineClassHelper
32
* @run main/othervm -javaagent:redefineagent.jar ClassVersionAfterRedefine
33
*/
34
35
import java.io.InputStream;
36
import java.lang.reflect.Method;
37
38
import static jdk.test.lib.Asserts.assertTrue;
39
40
public class ClassVersionAfterRedefine extends ClassLoader {
41
42
private static String myName = ClassVersionAfterRedefine.class.getName();
43
44
private static byte[] getBytecodes(String name) throws Exception {
45
InputStream is = ClassVersionAfterRedefine.class.getResourceAsStream(name + ".class");
46
byte[] buf = is.readAllBytes();
47
System.out.println("sizeof(" + name + ".class) == " + buf.length);
48
return buf;
49
}
50
51
private static int getStringIndex(String needle, byte[] buf) {
52
return getStringIndex(needle, buf, 0);
53
}
54
55
private static int getStringIndex(String needle, byte[] buf, int offset) {
56
outer:
57
for (int i = offset; i < buf.length - offset - needle.length(); i++) {
58
for (int j = 0; j < needle.length(); j++) {
59
if (buf[i + j] != (byte)needle.charAt(j)) continue outer;
60
}
61
return i;
62
}
63
return 0;
64
}
65
66
private static void replaceString(byte[] buf, String name, int index) {
67
for (int i = index; i < index + name.length(); i++) {
68
buf[i] = (byte)name.charAt(i - index);
69
}
70
}
71
72
private static void replaceAllStrings(byte[] buf, String oldString, String newString) throws Exception {
73
assertTrue(oldString.length() == newString.length(), "must have same length");
74
int index = -1;
75
while ((index = getStringIndex(oldString, buf, index + 1)) != 0) {
76
replaceString(buf, newString, index);
77
}
78
}
79
80
public static void main(String[] s) throws Exception {
81
82
byte[] buf = getBytecodes("TestClassOld");
83
// Poor man's renaming of class "TestClassOld" to "TestClassXXX"
84
replaceAllStrings(buf, "TestClassOld", "TestClassXXX");
85
ClassVersionAfterRedefine cvar = new ClassVersionAfterRedefine();
86
Class<?> old = cvar.defineClass(null, buf, 0, buf.length);
87
Method foo = old.getMethod("foo");
88
Object result = foo.invoke(null);
89
assertTrue("java-lang-String".equals(result));
90
System.out.println(old.getSimpleName() + ".foo() = " + result);
91
92
buf = getBytecodes("TestClassNew");
93
// Rename class "TestClassNew" to "TestClassXXX" so we can use it for
94
// redefining the original version of "TestClassXXX" (i.e. "TestClassOld").
95
replaceAllStrings(buf, "TestClassNew", "TestClassXXX");
96
// Now redine the original version of "TestClassXXX" (i.e. "TestClassOld").
97
RedefineClassHelper.redefineClass(old, buf);
98
result = foo.invoke(null);
99
assertTrue("java.lang.String".equals(result));
100
System.out.println(old.getSimpleName() + ".foo() = " + result);
101
}
102
}
103
104