Skip to content
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

Bytebuddy Advice not called #1625

Open
SaiprasadKrishnamurthy opened this issue Apr 28, 2024 · 1 comment
Open

Bytebuddy Advice not called #1625

SaiprasadKrishnamurthy opened this issue Apr 28, 2024 · 1 comment
Assignees
Labels
Milestone

Comments

@SaiprasadKrishnamurthy
Copy link

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
Copy link
Owner

raphw commented Apr 28, 2024

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();

@raphw raphw self-assigned this Apr 28, 2024
@raphw raphw added the question label Apr 28, 2024
@raphw raphw added this to the 1.12.14 milestone Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants