Skip to content

Commit e7a4500

Browse files
committed
8359707: Add classfile modification code to RedefineClassHelper
Reviewed-by: lmesnik, dholmes, sspitsyn
1 parent 38f59f8 commit e7a4500

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/ClassVersionAfterRedefine.java

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,68 +32,27 @@
3232
* @run main/othervm -javaagent:redefineagent.jar ClassVersionAfterRedefine
3333
*/
3434

35-
import java.io.InputStream;
3635
import java.lang.reflect.Method;
3736

3837
import static jdk.test.lib.Asserts.assertTrue;
3938

4039
public class ClassVersionAfterRedefine extends ClassLoader {
4140

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-
8041
public static void main(String[] s) throws Exception {
8142

82-
byte[] buf = getBytecodes("TestClassOld");
83-
// Poor man's renaming of class "TestClassOld" to "TestClassXXX"
84-
replaceAllStrings(buf, "TestClassOld", "TestClassXXX");
8543
ClassVersionAfterRedefine cvar = new ClassVersionAfterRedefine();
44+
45+
byte[] buf = RedefineClassHelper.replaceClassName(cvar, "TestClassOld", "TestClassXXX");
8646
Class<?> old = cvar.defineClass(null, buf, 0, buf.length);
8747
Method foo = old.getMethod("foo");
8848
Object result = foo.invoke(null);
8949
assertTrue("java-lang-String".equals(result));
9050
System.out.println(old.getSimpleName() + ".foo() = " + result);
9151

92-
buf = getBytecodes("TestClassNew");
9352
// Rename class "TestClassNew" to "TestClassXXX" so we can use it for
9453
// 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").
54+
buf = RedefineClassHelper.replaceClassName(cvar, "TestClassNew", "TestClassXXX");
55+
// Now redefine the original version of "TestClassXXX" (i.e. "TestClassOld").
9756
RedefineClassHelper.redefineClass(old, buf);
9857
result = foo.invoke(null);
9958
assertTrue("java.lang.String".equals(result));

test/lib/RedefineClassHelper.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,8 +21,14 @@
2121
* questions.
2222
*/
2323

24-
import java.lang.instrument.Instrumentation;
24+
import java.io.InputStream;
25+
import java.lang.classfile.ClassElement;
26+
import java.lang.classfile.ClassFile;
27+
import java.lang.classfile.ClassModel;
28+
import java.lang.constant.ClassDesc;
29+
2530
import java.lang.instrument.ClassDefinition;
31+
import java.lang.instrument.Instrumentation;
2632
import jdk.test.lib.compiler.InMemoryJavaCompiler;
2733
import jdk.test.lib.helpers.ClassFileInstaller;
2834

@@ -33,6 +39,7 @@
3339
*
3440
* See sample test in test/testlibrary_tests/RedefineClassTest.java
3541
*/
42+
3643
public class RedefineClassHelper {
3744

3845
public static Instrumentation instrumentation;
@@ -61,6 +68,41 @@ public static void redefineClass(Class<?> clazz, byte[] bytecode) throws Excepti
6168
instrumentation.redefineClasses(new ClassDefinition(clazz, bytecode));
6269
}
6370

71+
private static byte[] getBytecodes(ClassLoader loader, String name) throws Exception {
72+
try (InputStream is = loader.getResourceAsStream(name + ".class")) {
73+
byte[] buf = is.readAllBytes();
74+
System.out.println("sizeof(" + name + ".class) == " + buf.length);
75+
return buf;
76+
}
77+
}
78+
79+
/*
80+
* Copy the class defined by `bytes`, replacing the name of the class with `newClassName`,
81+
* so that both old and new classes can be compiled by jtreg for the test.
82+
*
83+
* @param bytes read from the original class file.
84+
* @param newClassName new class name for the returned class representation
85+
* @return a copy of the class represented by `bytes` but with the name `newClassName`
86+
*/
87+
public static byte[] replaceClassName(byte[] bytes, String newClassName) throws Exception {
88+
ClassModel classModel = ClassFile.of().parse(bytes);
89+
return ClassFile.of().build(ClassDesc.of(newClassName), classModel::forEach);
90+
}
91+
92+
/*
93+
* Replace class name in bytecodes to the class we're trying to redefine, so that both
94+
* old and new classes can be compiled with jtreg for the test.
95+
*
96+
* @param loader ClassLoader to find the bytes for the old class.
97+
* @param oldClassName old class name.
98+
* @param newClassName new class name to replace with old class name.
99+
* @return a copy of the class represented by `bytes` but with the name `newClassName`
100+
*/
101+
public static byte[] replaceClassName(ClassLoader loader, String oldClassName, String newClassName) throws Exception {
102+
byte[] buf = getBytecodes(loader, oldClassName);
103+
return replaceClassName(buf, newClassName);
104+
}
105+
64106
/**
65107
* Main method to be invoked before test to create the redefineagent.jar
66108
*/

0 commit comments

Comments
 (0)