Ander Ruiz
Ander Ruiz

Reputation: 49

Replacing Class.forName method call in ASM (inline)

I'm trying to capture the classes being loaded by replacing each method call to Class.forName in the place where the method is called (a callback in the method is not an option). i.e

...
Class.forName(className);
...

is replaced by

...
Class.forName(MyProxy.call(className));
...

My problem is trying to do the same with Class.forName(String, boolean, ClassLoader) method. My idea would be to do the following:

Class.forName(MyProxy.call(className), booleanParameter, classLoaderParameter)

However, I don't find a way to do this. Using and invokeStatic is not an option because Class.forName will not work properly if classes are protected for example

Upvotes: 0

Views: 657

Answers (1)

Ander Ruiz
Ander Ruiz

Reputation: 49

Thanks to @Holger suggestion I was able to find a solution, finally my code replaces:

...
Class.forName(className, booleanParameter, loader);
...

With

...
__callClassForName(className, booleanParameter, loader);
...

private static Class __callClassForName(String className, boolean bParameter, ClassLoader loader) {
    MyProxy.call(className);
    return Class.forName(className, bParameter, loader);
}

Upvotes: 1

Related Questions