Reputation: 2565
I have a legacy java code that I currently cannot modify/don't have access to it's source code. We found out that there's a problem with all methods that have a runtime annotation called @SomeAnnotation, and we would like to fix something inside these methods.
@SomeAnnotation
void someMethod(...) {
...
}
I know that I can use AOP (aspectJ) to catch all functions with these annotation and add operations before/after, as well as replacing the execution code with something else... But what I'd like to do is to add something inside this function and leaving the rest of the code as is (scan the bytecode and modify the function in the middle...)
Is it possible to do that? If so - how?
Thanks
Upvotes: 2
Views: 486
Reputation: 1602
You can try javassist - It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file when the JVM loads it.
Or HotSwap - It allows changing method body, add/rename a method/field
Also you might find this link useful.
(Depending on what you need to do (add some code at the beginning, do the rest of the function, add some code at the end, and not modify the function right in the middle), you might be able to use an around advice, with a ProceedingJoinPoint invocation
as a parameter, you can execute your code and then call invocation.proceed()
to delegate the call to the original target.)
Upvotes: 1