Can i use javassist java core classes tools? Classes loaded by bootstrap class loader

I want to add "insertBefore" to the JDK 5 base class method. For some reason, it does not work. Here is a sample 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(); 

com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter comes bundled with the JDK, inside rt.jar. After the above snippet, I run some code that causes the XSMessageFormatter class to run, but my pasted code never runs. I can only make it work in my own classes. This code works like a simple standalone application.

Any ideas?

+6
source share
1 answer

It’s a bad idea to change Java base classes. The correct way to use the Java Instrumentation API (since Java 1.5).

+1
source

Source: https://habr.com/ru/post/910714/


All Articles