You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.9 KiB
51 lines
1.9 KiB
3 years ago
|
package com.fr.design.record.analyzer;
|
||
|
|
||
|
import com.fr.record.analyzer.advice.AdviceCallable;
|
||
|
import com.fr.record.analyzer.advice.AdviceContext;
|
||
|
import com.fr.third.net.bytebuddy.asm.Advice;
|
||
|
import com.fr.third.net.bytebuddy.implementation.bytecode.assign.Assigner;
|
||
|
import com.fr.tolerance.FaultTolerance;
|
||
|
|
||
|
import java.lang.reflect.Method;
|
||
|
import java.util.concurrent.Callable;
|
||
|
|
||
|
/**
|
||
|
* created by Harrison on 2022/03/09
|
||
|
**/
|
||
|
public class TestCallableAdvice {
|
||
|
|
||
|
@Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class)
|
||
|
public static boolean onMethodEnter(@Advice.Local("context")AdviceContext adviceContext) {
|
||
|
|
||
|
adviceContext = AdviceContext
|
||
|
.builder()
|
||
|
.onAdviceCall()
|
||
|
.build();
|
||
|
// 如果是切面调用,则忽视当前方法
|
||
|
return adviceContext.isOnAdviceCall();
|
||
|
}
|
||
|
|
||
|
@Advice.OnMethodExit(onThrowable = Exception.class)
|
||
|
public static void onMethodExit(@Advice.This(optional = true, typing = Assigner.Typing.DYNAMIC) Object self,
|
||
|
@Advice.Origin Method method,
|
||
|
@Advice.AllArguments(typing = Assigner.Typing.DYNAMIC) Object[] args,
|
||
|
@Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object result,
|
||
|
@Advice.Local("context") AdviceContext adviceContext) throws Exception {
|
||
|
|
||
|
// 如果是切面调用,则忽视不继续 exit
|
||
|
if (adviceContext != null && adviceContext.isOnAdviceCall()) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
FaultTolerance faultTolerance = method.getAnnotation(FaultTolerance.class);
|
||
|
Callable<Object> callable = new AdviceCallable<Object>() {
|
||
|
@Override
|
||
|
public Object call() throws Exception {
|
||
|
return method.invoke(self, args);
|
||
|
}
|
||
|
};
|
||
|
result = TestCallableHelper.test(callable);
|
||
|
}
|
||
|
|
||
|
}
|