简体   繁体   中英

How to dynamically intercept methods in a jar without altering code?

I have a jar file containing multiple .class files. Each .class file has several methods in it. Now, while the jar is executing, ie during runtime, I want to intercept the methods with an external user defined method. That is, I want to run an external method before or after each method call in my jar classes.

I have looked into Spring-AOP, but it says I need to declare the classes in the Spring context and hence the classes will be called with their methods in the sequence in which it is written in the Spring context.

But I want the execution flow to be that of the original jar webapp.

Can you share with me anything which can be used to achieve what I explained above? Thanks in advance!

You can use JVM instrumentation API, it allows you to modify classes being loaded using some library like http://commons.apache.org/proper/commons-bcel/ . See example here https://web.archive.org/web/20181012004753/http://blog.javabenchmark.org/2013/05/java-instrumentation-tutorial.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM