raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.23k stars 804 forks source link

Bytebuddy Advice not called #1625

Closed SaiprasadKrishnamurthy closed 6 days ago

SaiprasadKrishnamurthy commented 5 months ago

Newbie Alert!

This is my main application.

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DataTransformationServiceApplication {
    public static void main(String[] args) throws Exception {
        DynamicType.Unloaded<BodyNode> unloaded = new ByteBuddy()
                .redefine(BodyNode.class)
                .visit(Advice.to(MyAdvices.class).on(ElementMatchers.isMethod()))
                .make();

        Class<? extends BodyNode> loaded = unloaded
                .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST)
                .getLoaded();

        SpringApplication.run(DataTransformationServiceApplication.class, args);
    }

My advice looks like this:

public class MyAdvices {
    @Advice.OnMethodEnter(suppress = Throwable.class)
    static long enter(@Advice.This Object thisObject,
                      @Advice.Origin String origin,
                      @Advice.Origin("#t #m") String detaildOrigin,
                      @Advice.AllArguments Object[] ary){

        System.out.println("Inside enter method . . .  ");

        if(ary != null) {
            for(int i =0 ; i < ary.length ; i++){
                System.out.println("Argument: " + i + " is " + ary[i]);
            }
        }

        System.out.println("Origin :" + origin);
        System.out.println("Detailed Origin :" + detaildOrigin);

        return System.nanoTime();
    }

    @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
    static void exit(@Advice.Enter long time){
        System.out.println("Inside exit method . . .");
        System.out.println("Method Execution Time: " + (System.nanoTime() - time) + " nano seconds");
    }
}

My 3rd party library when it calls BodyNode.render(...) with a hard coded object within it's class (using new), my advice isn't getting called. What am I missing? I'm looking to log a few things and then pass the control back to the original method (similar to the around advice).

raphw commented 5 months ago

By .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST) you create a new class in a child loader. Once your regular program loads the class, it won't pick it up from that loader but load the original class. You would need to use an injection strategy, or use a MethodHandle.Lookup to inject the class in the loader. To do so, you need to avoid loading the class:

    DynamicType.Unloaded<BodyNode> unloaded = new ByteBuddy()
            .redefine("your.packagelocation.BodyNode", ClassFileLocator.ForClassLoader.ofSystemLoader())
            .visit(Advice.to(MyAdvices.class).on(ElementMatchers.isMethod()))
            .make();

    Class<? extends BodyNode> loaded = unloaded
            .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECT) // or lookup when using Java 9+
            .getLoaded();