基本上,我想拦截所有具有特定注释(由我创建)的类,并且希望在方法和构造函数条目中执行自定义逻辑。
正如我从代码示例中看到的那样,我尝试使用字节伙伴代理生成器。
public static void agentmain(final String agentArgs,
final Instrumentation inst) {
new AgentBuilder.Default()
.type(ElementMatchers.
<TypeDescription>isAnnotatedWith(Licensable.class))
.transform((builder, td, cl, m) -> builder
.visit(Advice.to(AdviceToClasses.class).on(isMethod()))
.visit(Advice.to(AdviceToConstructor.class).on(isConstructor())))
.installOn(inst);
}
AdviceToConstructor类
@Advice.OnMethodEnter
public static void enter(@Advice.Origin Constructor method) throws
Exception {
System.out.println("Intercepted Constr >> " + method);
}
使用上面的方法,如果使用方法建议并且省去了构造函数部分,我可以得到期望的结果。但是当我使用构造函数部分时,它并没有建议输入方法,并且出现以下错误。
complete --- sample.TestAnnotation complete --- java.lang.VerifyError Exception in thread "main" complete --- java.lang.Throwable$WrappedPrintStream complete --- java.lang.Throwable$PrintStreamOrWriter complete --- java.util.IdentityHashMap$KeySet java.lang.VerifyError: Inconsistent stackmap frames at branch target 96 Exception Details: Location: sample/TestAnnotation.()V @96: aload_0 Reason: Type uninitializedThis (current frame, locals[0]) is not assignable to 'sample/TestAnnotation' (stack map, locals[0]) Current Frame: bci: @93 flags: { flagThisUninit } locals: { uninitializedThis, 'sample/annotation/Licensable' } stack: { } Stackmap Frame: bci: @96 flags: { } locals: { 'sample/TestAnnotation' } stack: { } Bytecode: 0x0000000: b200 02bb 0028 59b7 0029 122b b600 2f12 0x0000010: 0703 bd00 31b6 0035 b600 38b6 003c b600 0x0000020: 04b2 0002 123e b600 0412 0703 bd00 31b6 0x0000030: 0035 b600 4412 46b6 004a c000 464c 2bc6 0x0000040: 001e b200 502b b900 5301 00b6 0059 3d1c 0x0000050: 9a00 0dbb 005b 5912 5db7 005f bfa7 0003 0x0000060: 2ab7 0001 b1 Stackmap Table: append_frame(@93,Object[#70]) full_frame(@96,{Object[#7]},{}) at sample.Sample.main(SampleApp.java:47) complete --- java.util.IdentityHashMap$KeyIterator complete --- java.util.IdentityHashMap$IdentityHashMapIterator
我检查了StackOverflow上的帖子并用Google搜索,但找不到解决方案。关于此错误有什么建议吗?