Skip to content

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

@vuhoanghiep1993

Description

@vuhoanghiep1993

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions