简体   繁体   English

Java 字节码检测到 ASPECTJ 标识的方法

[英]Java bytecode instrumentation to a method identified by ASPECTJ

I have a legacy java code that I currently cannot modify/don't have access to it's source code.我有一个遗留的 Java 代码,我目前无法修改/无法访问它的源代码。 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 的运行时注释的方法都存在问题,我们希望修复这些方法中的某些内容。

@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...)我知道我可以使用 AOP (aspectJ) 来捕获所有带有这些注释的函数并在之前/之后添加操作,以及用其他东西替换执行代码......但我想做的是在里面添加一些东西此函数并保留其余代码原样(扫描字节码并修改中间的函数......)

Is it possible to do that?有可能这样做吗? If so - how?如果是这样 - 如何?

Thanks谢谢

You can try javassist - It is a class library for editing bytecodes in Java;你可以试试javassist - 它是一个用于在 Java 中编辑字节码的类库; it enables Java programs to define a new class at runtime and to modify a class file when the JVM loads it.它使 Java 程序能够在运行时定义一个新类,并在 JVM 加载类文件时修改它。

Or HotSwap - It allows changing method body, add/rename a method/fieldHotSwap - 它允许更改方法主体,添加/重命名方法/字段

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.) (取决于你需要做什么(在开始添加一些代码,完成函数的其余部分,在最后添加一些代码,而不是在中间修改函数),你也许可以使用一个 around 建议,使用ProceedingJoinPoint invocation作为参数,您可以执行您的代码,然后调用invocation.proceed()invocation.proceed()委托给原始目标。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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