in ttl2-compatible/src/main/java/com/alibaba/ttl/threadpool/agent/transformlet/helper/TtlTransformletHelper.java [122:151]
public static String addTryFinallyToMethod(@NonNull CtMethod method, @NonNull String nameForOriginalMethod, @NonNull String beforeCode, @NonNull String finallyCode) throws CannotCompileException, NotFoundException {
final CtClass clazz = method.getDeclaringClass();
final CtMethod newMethod = CtNewMethod.copy(method, clazz, null);
// rename original method, and set to private method(avoid reflect out renamed method unexpectedly)
newMethod.setName(nameForOriginalMethod);
newMethod.setModifiers(newMethod.getModifiers()
& ~Modifier.PUBLIC /* remove public */
& ~Modifier.PROTECTED /* remove protected */
| Modifier.PRIVATE /* add private */);
clazz.addMethod(newMethod);
final String returnOp;
if (method.getReturnType() == CtClass.voidType) {
returnOp = "";
} else {
returnOp = "return ";
}
// set new method implementation
final String code = "{\n" +
beforeCode + "\n" +
"try {\n" +
" " + returnOp + nameForOriginalMethod + "($$);\n" +
"} finally {\n" +
" " + finallyCode + "\n" +
"} }";
method.setBody(code);
return code;
}