Search code examples
tomcatjavassist

How I can make Javassist work with Tomcat


I want to perform bytecode instrumentation on servelets using Javassist, but the challenge is that, whatever instrumentation I did, it is not reflected in Tomcat classes. I could clearly see that this is an issue with class loaders but I couldn't make it even work with custom class loaders. I tried searching for any examples on this but couldn't find any. Do you have any examples on how we can make Tomcat work with Javassist?


Solution

  • I assume that you are using ClassPool.getDefault() in order to run your example on Tomcat. This will not work as application servers do not run on the class path.

    In order to inform Javassist of the classes of a specific application, you need to append the classes of the current class loader. You can do this by appending an instance of LoaderClassPath to the current class pool which references your application's class loader.