pengda
3 years ago
9 changed files with 197 additions and 40 deletions
@ -0,0 +1,50 @@
|
||||
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); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,15 @@
|
||||
package com.fr.design.record.analyzer; |
||||
|
||||
import java.util.concurrent.Callable; |
||||
|
||||
/** |
||||
* created by Harrison on 2022/03/09 |
||||
**/ |
||||
public class TestCallableHelper { |
||||
|
||||
public static String test(Callable<Object> callable) throws Exception { |
||||
|
||||
callable.call(); |
||||
return "[test]Callable"; |
||||
} |
||||
} |
Loading…
Reference in new issue