提交 c3c03e4d 编写于 作者: oldratlee's avatar oldratlee 🔥

doTryFinallyForMethod support void return type CtMethod

上级 2a7667a2
...@@ -82,9 +82,9 @@ ...@@ -82,9 +82,9 @@
相比[`InheritableThreadLocal`](https://docs.oracle.com/javase/10/docs/api/java/lang/InheritableThreadLocal.html),添加了 相比[`InheritableThreadLocal`](https://docs.oracle.com/javase/10/docs/api/java/lang/InheritableThreadLocal.html),添加了
1. `protected`方法`copy` 1. `copy`方法
用于定制 **任务提交给线程池时**`ThreadLocal`值传递到 **任务执行时** 的拷贝行为,缺省传递的是引用。 用于定制 **任务提交给线程池时**`ThreadLocal`值传递到 **任务执行时** 的拷贝行为,缺省传递的是引用。
1. `protected`方法`beforeExecute`/`afterExecute` 1. `protected``beforeExecute`/`afterExecute`方法
执行任务(`Runnable`/`Callable`)的前/后的生命周期回调,缺省是空操作。 执行任务(`Runnable`/`Callable`)的前/后的生命周期回调,缺省是空操作。
具体使用方式见下面的说明。 具体使用方式见下面的说明。
......
...@@ -57,16 +57,16 @@ public class TtlForkJoinTransformlet implements JavassistTransformlet { ...@@ -57,16 +57,16 @@ public class TtlForkJoinTransformlet implements JavassistTransformlet {
logger.info("add new field " + capturedFieldName + " to class " + className); logger.info("add new field " + capturedFieldName + " to class " + className);
final CtMethod doExecMethod = clazz.getDeclaredMethod("doExec", new CtClass[0]); final CtMethod doExecMethod = clazz.getDeclaredMethod("doExec", new CtClass[0]);
final String doExec_renamed_method_rename = renamedMethodNameByTtl(doExecMethod); final String doExec_renamed_method_name = renamedMethodNameByTtl(doExecMethod);
final String beforeCode = "if (this instanceof " + TtlEnhanced.class.getName() + ") {\n" + // if the class is already TTL enhanced(eg: com.alibaba.ttl.TtlRecursiveTask) final String beforeCode = "if (this instanceof " + TtlEnhanced.class.getName() + ") {\n" + // if the class is already TTL enhanced(eg: com.alibaba.ttl.TtlRecursiveTask)
" return " + doExec_renamed_method_rename + "($$);\n" + // return directly/do nothing " return " + doExec_renamed_method_name + "($$);\n" + // return directly/do nothing
"}\n" + "}\n" +
"Object backup = com.alibaba.ttl.TransmittableThreadLocal.Transmitter.replay(" + capturedFieldName + ");"; "Object backup = com.alibaba.ttl.TransmittableThreadLocal.Transmitter.replay(" + capturedFieldName + ");";
final String finallyCode = "com.alibaba.ttl.TransmittableThreadLocal.Transmitter.restore(backup);"; final String finallyCode = "com.alibaba.ttl.TransmittableThreadLocal.Transmitter.restore(backup);";
doTryFinallyForMethod(doExecMethod, doExec_renamed_method_rename, beforeCode, finallyCode); doTryFinallyForMethod(doExecMethod, doExec_renamed_method_name, beforeCode, finallyCode);
} }
private void updateConstructorDisableInheritable(@NonNull final CtClass clazz) throws NotFoundException, CannotCompileException { private void updateConstructorDisableInheritable(@NonNull final CtClass clazz) throws NotFoundException, CannotCompileException {
......
...@@ -68,11 +68,17 @@ public class Utils { ...@@ -68,11 +68,17 @@ public class Utils {
& ~Modifier.PROTECTED /* remove protected */ & ~Modifier.PROTECTED /* remove protected */
| Modifier.PRIVATE /* add private */); | Modifier.PRIVATE /* add private */);
final String returnOp;
if (method.getReturnType() == CtClass.voidType) {
returnOp = "";
} else {
returnOp = "return ";
}
// set new method implementation // set new method implementation
final String code = "{\n" + final String code = "{\n" +
beforeCode + "\n" + beforeCode + "\n" +
"try {\n" + "try {\n" +
" return " + renamedMethodName + "($$);\n" + " " + returnOp + renamedMethodName + "($$);\n" +
"} finally {\n" + "} finally {\n" +
" " + finallyCode + "\n" + " " + finallyCode + "\n" +
"} }"; "} }";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册