0

Basically, I want to intercept all classes that have a specific annotation (that was created by me) and i want to perform my custom logic in method and constructor entry.

I have tried using byte-buddy agent builder as you can see from my code sample.

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 class

@Advice.OnMethodEnter
public static void enter(@Advice.Origin Constructor method) throws 
Exception {
    System.out.println("Intercepted Constr >> " + method);
}

Using above, i can get what i expected if use method advice and leave out constructor part. But when i use constructor part, it doesn't go to advice enter method and i get the below errors.


    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

I checked the posts on StackOverflow and googled it but couldn't find a solution. Any advice regarding this error?

osmanates
  • 23
  • 1
  • 5

0 Answers0