As classes can load at any time, Substrate provides a means to detect when classes of interest are loaded, allowing developers to then use other APIs such as MS.hookMethod (or even just normal Java reflection) to alter their implementation.

This API takes an object implementing a simple interface MS.ClassLoadHook that has a single method called "classLoaded" which will be executed when the class is loaded. This method will be passed as an argument the Java class that was loaded.

One important consideration with this API is that, as your extension is loaded in its own ClassLoader, while you will be able to work with the Class object indirectly, you might not be able to directly use the type. For a solution, see MS.moveUnderClassLoader.

void hookClassLoad(String name, MS.ClassLoadHook hook);
Parameter Description
name The name of the class, using Java's dot notation.
hook An instance of MS.ClassLoadHook whose classLoaded method will be executed when the class is loaded.
MS.hookClassLoad("java.net.HttpURLConnection",
    new MS.ClassLoadHook() {
        public void classLoaded(Class<?> _class) {
            /* do something with _class argument */
        }
    }
);