Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to instrument with javassist java core classes? Classes loaded by the bootstrap classloader

I want to add an "insertBefore" on a method of a core JDK 5 class. For some reason it´s not working. Here's an example of the code:

ClassPool pool = ClassPool.getDefault();
CtClass ctClass =  pool.get("com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter");
CtMethod ctMethod = ctClass.getDeclaredMethods()[0];
ctMethod.insertBefore("System.out.println(\"WORKED\");");
ctClass.toClass();

The com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter comes bundled with the JDK, it's inside rt.jar. After the snippet above, I run some code that forces the XSMessageFormatter class to run, but my inserted code never runs. I can only get this to work on my own classes. This code is running as a simple standalone app.

Any ideas?

like image 604
Rod Avatar asked Mar 14 '12 14:03

Rod


1 Answers

It is bad idea to modify java core classes. The right way to use Java Instrumentation API (since Java 1.5).

like image 122
MikhailDudarev Avatar answered Oct 14 '22 20:10

MikhailDudarev