I studied java classloader rencently. Now I want to write a class that has the same package name and class name as one of class in rt.jar. For example, write a java.lang.Str
Have a look at the java.lang.instrumentation API: you can implement a ClassFileTransformer which ignores the passed-in byte array and returns a bute array containing your own implementation of java.lang.String
instead.
Better make sure that it's completely binary compatible with the original though or you will not get very far.
You can do this using the -Xbootclasspath/p
option at JVM startup:
-Xbootclasspath/p:/path/to/yourimpl.jar
/p
stands for "prepend".
Note: -Xbootclasspath
isn't a standard java option, so JVMs by different vendors may not support it.