-
-
Notifications
You must be signed in to change notification settings - Fork 808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does Byte Buddy have an API to help convert regular Java code into ASM syntax? #1745
Comments
ASM offers that, using ASMifier. You will only have to adjust the imports for using the same with Byte Buddy. |
ASM操作比较繁琐但自由度很高,Byte Buddy都集成到一起了,操作简单但是很多地方很难自由发挥 |
现在是准备能使用现成的就使用现成的,自己今天拼了下这儿的ASM指令,尤其是这儿的局部变量值和栈大小,直接错乱掉了; 比如:我希望给方法内的每个调用加上执行时间的打印;
得到结果:(错误的)
转化代码:
简单场景可以自己手动控制这儿的 Opcodes.LSTORE值,如果我给做成功能 sayHello的结构体变复杂了,我感觉自己写的可能就不靠谱了;所以现在是想找找有没有现成的封装能使用的,比如 byte buddy 在 advice的 @Advice.OnMethodEnter 和 @Advice.OnMethodExit 实现中是怎么将代码正常嵌入进去的; |
Thank you for your reply, I tried the ASM.ASMifier tool, and it is really useful. .However, after testing, I found a problem. It's very easy for me to make mistakes when concatenating the local variable table and stack size, for example(Troublesome):
I realized that the @Advice.OnMethodEnter and @Advice.OnMethodExit I initially used for adding code before and after method execution are very useful, and I don't need to worry about setting the sizes of local variables. Now, my requirement is to add code logic before and after every method execution within a method, which is very similar to the previous @advice logic. So, I would like to ask if it's possible to migrate the internal logic of @advice to achieve similar functionality. How can it be implemented, or which part should I look into? My program's goal:
|
Advice is a very complex component and I do not think it can be retrofitted. You want to concattendate ASMified code? |
Have you looked into MemberSubstitution.relaxed().method(any()).replaceWithChain(
Lists.newArrayList(MemberSubstitution.Substitution.Chain.Step.ForDelegation.withCustomMapping().to(method))
).on(not(isSynthetic().or(isConstructor()))); The above code will eventually return an |
Does Byte Buddy have an API to help convert regular Java code into ASM syntax, or do you have any other suggestions? I now want to add execution time logging to every method call in a method. I haven't used ASM-related content before, and now I'm getting errors while writing ASM code myself. If I expand the functionality, I expect more errors. That's why I'm asking about the current issue. Example:
my error code: (T_T)
The text was updated successfully, but these errors were encountered: