byte-buddy icon indicating copy to clipboard operation
byte-buddy copied to clipboard

Return a value calculate in Advice.OnMethodEnter and skip running original method logic

Open vuhoanghiep1993 opened this issue 5 months ago • 2 comments
trafficstars

In some condition, I calculated result in Advice.OnMethodEnter and now I can return result in Advice.OnMethodExit I want still return result calculated but custom the condition when run original logic method code.

        @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class, suppress = Throwable.class)
        public static boolean onEnter(@Advice.This Invoker<?> invoker,
                                      @Advice.Argument(0) Invocation invocation,
                                      @Advice.Local("mockResult") MockResult mockResult) {

                if (ContextManager.needReplay()) {
                    mockResult = extractor.replay();
            }
           // true if have mock result
            return mockResult != null && mockResult.notIgnoreMockResult(); 
        }

        @Advice.OnMethodExit(suppress = Throwable.class)
        public static void onExit(@Advice.Return(readOnly = false) Result result,
                                  @Advice.Local("mockResult") MockResult mockResult) {

           //If condition true, return mockresult
            if (mockResult != null && mockResult.notIgnoreMockResult()) {
                result = (Result) mockResult.getResult();
                return;
            }
        }

I want

if(mockResult.getRunOriginal()){
// run original method code but still return mock result (now it working as)
} else{
// skipping the original method code still return mock result
}

I looked at https://stackoverflow.com/questions/50237592/is-it-possible-to-return-from-a-method-using-advice-onmethodenter but it not work as I want

Can I do that, thank you

vuhoanghiep1993 avatar May 28 '25 04:05 vuhoanghiep1993

I am not sure if I understand what you mean. But you can repeatOn in exit advice, so you would be able to invoke the original method again if you return a specific value from the exit advice. This works similarly to skipOn.

Maybe this would allow you to construct what you want?

raphw avatar May 28 '25 18:05 raphw

I need : Return the value calculate in onEnter method and not run original method . If cannot calculate value in onEnter, It will work normally (run original method and onExit() )

Flow

https://viewer.diagrams.net/index.html?tags=%7B%7D&lightbox=1&highlight=0000ff&edit=_blank&layers=1&nav=1&title=Untitled%20Diagram.drawio.png&dark=auto#R%3Cmxfile%3E%3Cdiagram%20name%3D%22Page-1%22%20id%3D%22FxTnPhz1foQjGkwVANJo%22%3E3Vhdc6IwFP01zuw%2BuAPhyz5aa7u7s91260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