Monday, 11 September 2023

Patching core classes

When JVM loads, runtime classes are available for use and can't be patched.
Manipulating system properties (namely java.class.path) is futile
.
In %JAVA_PATH%\lib there's a runtime JAR and its source-code.

 

In order to add a method to System.out, the class we need to alter is PrintStream.

patch/
    java.base/java/io/PrintStream.java
    build/java/io/PrintStream.class
    build/Hello.class
    Hello.java 

javac --patch-module java.base=./java.base ./java.base/java/io/PrintStream.java -d ./build
javac --patch-module java.base=build Hello.java
java --patch-module java.base=build -cp build Hello

import java.io.PrintStream;

public abstract class Hello
{
    public static void main(String[] args)
    { 
        PrintStream s = System.out; 
        s.println("Code " + s.specialMethod()); 
    }
}

Resources

Jigsaw (OpenJDK)

No comments:

Post a Comment