Skip to content

Commit

Permalink
avoid using getContextClassLoader()
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinccheung committed Oct 30, 2024
1 parent c60b76e commit e85ab0b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public static void main(String args[]) throws Throwable {

System.out.println("Main: loading OldSuper");
// Load an old class (version 49), but not linking it.
Thread.currentThread().getContextClassLoader().loadClass("OldSuper");
ClassLoader.getSystemClassLoader().loadClass("OldSuper");

// Redefine a class unrelated to the above old class.
Instrumentation instrumentation = InstrumentationRegisterClassFileTransformer.getInstrumentation();
System.out.println("INFO: instrumentation = " + instrumentation);
Class c = Thread.currentThread().getContextClassLoader().loadClass("Hello");
Class c = ClassLoader.getSystemClassLoader().loadClass("Hello");
byte[] bytes = c.getClassLoader().getResourceAsStream(c.getName().replace('.', '/') + ".class").readAllBytes();
instrumentation.redefineClasses(new ClassDefinition(c, bytes));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class RedefineBootClassApp {
public static void main(String args[]) throws Throwable {
File bootJar = new File(args[0]);

Class superCls = Thread.currentThread().getContextClassLoader().loadClass("BootSuper");
Class superCls = Class.forName("BootSuper", false, null);
System.out.println("BootSuper>> loader = " + superCls.getClassLoader());

{
Expand All @@ -53,7 +53,7 @@ public static void main(String args[]) throws Throwable {
}
}

Class childCls = Thread.currentThread().getContextClassLoader().loadClass("BootChild");
Class childCls = Class.forName("BootChild", false, null);
System.out.println("BootChild>> loader = " + childCls.getClassLoader());


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class RedefineOldSuperApp {
public static void main(String args[]) throws Throwable {
File bootJar = new File(args[0]);

Class superCls = Thread.currentThread().getContextClassLoader().loadClass("OldSuper");
Class superCls = ClassLoader.getSystemClassLoader().loadClass("OldSuper");
System.out.println("OldSuper>> loader = " + superCls.getClassLoader());

{
Expand All @@ -53,7 +53,7 @@ public static void main(String args[]) throws Throwable {
}
}

Class childCls = Thread.currentThread().getContextClassLoader().loadClass("NewChild");
Class childCls = ClassLoader.getSystemClassLoader().loadClass("NewChild");
System.out.println("NewChild>> loader = " + childCls.getClassLoader());


Expand Down

0 comments on commit e85ab0b

Please sign in to comment.