Skip to content

Commit

Permalink
@coleenp comment - use RedefineClassHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinccheung committed Nov 1, 2024
1 parent d4aeb92 commit 7d062bf
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @compile ../../test-classes/OldSuper.jasm
* ../../test-classes/ChildOldSuper.java
* ../../test-classes/Hello.java
* @run driver RedefineClassHelper
* @run driver OldClassAndRedefineClass
*/

Expand All @@ -52,24 +53,15 @@ public class OldClassAndRedefineClass {
};
public static String sharedClasses[] = TestCommon.concat(appClasses);

public static String agentClasses[] = {
"InstrumentationClassFileTransformer",
"InstrumentationRegisterClassFileTransformer",
};

public static void main(String[] args) throws Throwable {
runTest();
}

public static void runTest() throws Throwable {
String appJar =
ClassFileInstaller.writeJar("OldClassAndRedefineClassApp.jar", appClasses);
String agentJar =
ClassFileInstaller.writeJar("InstrumentationAgent.jar",
ClassFileInstaller.Manifest.fromSourceFile("../InstrumentationAgent.mf"),
agentClasses);

String agentCmdArg = "-javaagent:" + agentJar;
String agentCmdArg = "-javaagent:redefineagent.jar";

OutputAnalyzer out = TestCommon.testDump(appJar, sharedClasses, "-Xlog:cds,cds+class=debug");
out.shouldMatch("klasses.*OldSuper.[*][*].unlinked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
*
*/

import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;

public class OldClassAndRedefineClassApp {

public static void main(String args[]) throws Throwable {
Expand All @@ -35,11 +32,10 @@ public static void main(String args[]) throws Throwable {
Class.forName("OldSuper", false, appClassLoader);

// Redefine a class unrelated to the above old class.
Instrumentation instrumentation = InstrumentationRegisterClassFileTransformer.getInstrumentation();
System.out.println("INFO: instrumentation = " + instrumentation);
System.out.println("INFO: instrumentation = " + RedefineClassHelper.instrumentation);
Class<?> c = Class.forName("Hello", false, appClassLoader);
byte[] bytes = c.getClassLoader().getResourceAsStream(c.getName().replace('.', '/') + ".class").readAllBytes();
instrumentation.redefineClasses(new ClassDefinition(c, bytes));
RedefineClassHelper.redefineClass(c, bytes);

System.out.println("Main: loading ChildOldSuper");
// Load and link a subclass of the above old class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/

import java.io.File;
import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;

public class RedefineBootClassApp {
public static void main(String args[]) throws Throwable {
Expand All @@ -39,10 +37,9 @@ public static void main(String args[]) throws Throwable {
}

// Redefine the class
Instrumentation instrumentation = InstrumentationRegisterClassFileTransformer.getInstrumentation();
byte[] bytes = Util.getClassFileFromJar(bootJar, "BootSuper");
Util.replace(bytes, "Hello", "HELLO");
instrumentation.redefineClasses(new ClassDefinition(superCls, bytes));
RedefineClassHelper.redefineClass(superCls, bytes);

{
BootSuper obj = (BootSuper)superCls.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* BootSuper BootChild
* InstrumentationClassFileTransformer
* InstrumentationRegisterClassFileTransformer
* @run driver RedefineClassHelper
* @run driver RedefineBootClassTest
*/

Expand All @@ -56,14 +57,9 @@ public class RedefineBootClassTest {
};
public static String appClasses[] = {
"RedefineBootClassApp",
};
public static String sharedClasses[] = TestCommon.concat(bootClasses, appClasses);

public static String agentClasses[] = {
"InstrumentationClassFileTransformer",
"InstrumentationRegisterClassFileTransformer",
"Util",
};
public static String sharedClasses[] = TestCommon.concat(bootClasses, appClasses);

public static void main(String[] args) throws Throwable {
runTest();
Expand All @@ -74,13 +70,9 @@ public static void runTest() throws Throwable {
ClassFileInstaller.writeJar("RedefineClassBoot.jar", bootClasses);
String appJar =
ClassFileInstaller.writeJar("RedefineClassApp.jar", appClasses);
String agentJar =
ClassFileInstaller.writeJar("InstrumentationAgent.jar",
ClassFileInstaller.Manifest.fromSourceFile("../InstrumentationAgent.mf"),
agentClasses);

String bootCP = "-Xbootclasspath/a:" + bootJar;
String agentCmdArg = "-javaagent:" + agentJar;
String agentCmdArg = "-javaagent:redefineagent.jar";

TestCommon.testDump(appJar, sharedClasses, bootCP, "-Xlog:cds,cds+class=debug");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/

import java.io.File;
import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;

public class RedefineOldSuperApp {
public static void main(String args[]) throws Throwable {
Expand All @@ -40,10 +38,9 @@ public static void main(String args[]) throws Throwable {
}

// Redefine the class
Instrumentation instrumentation = InstrumentationRegisterClassFileTransformer.getInstrumentation();
byte[] bytes = Util.getClassFileFromJar(bootJar, "OldSuper");
Util.replace(bytes, "Hello", "HELLO");
instrumentation.redefineClasses(new ClassDefinition(superCls, bytes));
RedefineClassHelper.redefineClass(superCls, bytes);

{
OldSuper obj = (OldSuper)superCls.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* NewChild
* InstrumentationClassFileTransformer
* InstrumentationRegisterClassFileTransformer
* @run driver RedefineClassHelper
* @run driver RedefineOldSuperTest
*/

Expand All @@ -55,14 +56,9 @@ public class RedefineOldSuperTest {
"OldSuper",
"NewChild",
"RedefineOldSuperApp",
};
public static String sharedClasses[] = TestCommon.concat(appClasses);

public static String agentClasses[] = {
"InstrumentationClassFileTransformer",
"InstrumentationRegisterClassFileTransformer",
"Util",
};
public static String sharedClasses[] = TestCommon.concat(appClasses);

public static void main(String[] args) throws Throwable {
runTest();
Expand All @@ -71,12 +67,8 @@ public static void main(String[] args) throws Throwable {
public static void runTest() throws Throwable {
String appJar =
ClassFileInstaller.writeJar("RedefineClassApp.jar", appClasses);
String agentJar =
ClassFileInstaller.writeJar("InstrumentationAgent.jar",
ClassFileInstaller.Manifest.fromSourceFile("../InstrumentationAgent.mf"),
agentClasses);

String agentCmdArg = "-javaagent:" + agentJar;
String agentCmdArg = "-javaagent:redefineagent.jar";

TestCommon.testDump(appJar, sharedClasses, "-Xlog:cds,cds+class=debug");

Expand Down

0 comments on commit 7d062bf

Please sign in to comment.