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

clean code; catch Throwable in Agent

上级 99ec6b1b
...@@ -11,7 +11,7 @@ import java.util.logging.Logger; ...@@ -11,7 +11,7 @@ import java.util.logging.Logger;
* @since 0.9.0 * @since 0.9.0
*/ */
public class MtContextAgent { public class MtContextAgent {
private static Logger logger = Logger.getLogger(MtContextAgent.class.getName()); private static final Logger logger = Logger.getLogger(MtContextAgent.class.getName());
public static void premain(String agentArgs, Instrumentation inst) { public static void premain(String agentArgs, Instrumentation inst) {
logger.warning("[MtContextAgent.premain] begin, agentArgs: " + agentArgs); logger.warning("[MtContextAgent.premain] begin, agentArgs: " + agentArgs);
......
...@@ -22,14 +22,13 @@ import java.util.logging.Logger; ...@@ -22,14 +22,13 @@ import java.util.logging.Logger;
* @since 0.9.0 * @since 0.9.0
*/ */
public class MtContextTransformer implements ClassFileTransformer { public class MtContextTransformer implements ClassFileTransformer {
private static final Logger logger = Logger.getLogger(MtContextTransformer.class.getName());
private static Logger logger = Logger.getLogger(MtContextTransformer.class.getName());
private static final String RUNNABLE_CLASS_NAME = "java.lang.Runnable"; private static final String RUNNABLE_CLASS_NAME = "java.lang.Runnable";
private static final String CALLABLE_CLASS_NAME = "java.util.concurrent.Callable"; private static final String CALLABLE_CLASS_NAME = "java.util.concurrent.Callable";
private static final String MTCONTEXT_RUNNABLE_CLASS_NAME = MtContextRunnable.class.getName(); private static final String MT_CONTEXT_RUNNABLE_CLASS_NAME = MtContextRunnable.class.getName();
private static final String MTCONTEXT_CALLABLE_CLASS_NAME = MtContextCallable.class.getName(); private static final String MT_CONTEXT_CALLABLE_CLASS_NAME = MtContextCallable.class.getName();
private static final String THREAD_POOL_CLASS_FILE = "java.util.concurrent.ThreadPoolExecutor".replace('.', '/'); private static final String THREAD_POOL_CLASS_FILE = "java.util.concurrent.ThreadPoolExecutor".replace('.', '/');
private static final String SCHEDULER_CLASS_FILE = "java.util.concurrent.ScheduledThreadPoolExecutor".replace('.', '/'); private static final String SCHEDULER_CLASS_FILE = "java.util.concurrent.ScheduledThreadPoolExecutor".replace('.', '/');
...@@ -53,16 +52,16 @@ public class MtContextTransformer implements ClassFileTransformer { ...@@ -53,16 +52,16 @@ public class MtContextTransformer implements ClassFileTransformer {
updateMethod(method); updateMethod(method);
} }
return clazz.toBytecode(); return clazz.toBytecode();
} catch (Exception e) { } catch (Throwable t) {
String msg = "Fail to transform class " + className + ", cause: " + e.getMessage(); String msg = "Fail to transform class " + className + ", cause: " + t.getMessage();
logger.severe(msg); logger.severe(msg);
throw new IllegalStateException(msg, e); throw new IllegalStateException(msg, t);
} }
} }
return null; return null;
} }
static Set<String> updateMethodNames = new HashSet<String>(); static final Set<String> updateMethodNames = new HashSet<String>();
static { static {
updateMethodNames.add("execute"); updateMethodNames.add("execute");
...@@ -76,7 +75,7 @@ public class MtContextTransformer implements ClassFileTransformer { ...@@ -76,7 +75,7 @@ public class MtContextTransformer implements ClassFileTransformer {
if (!updateMethodNames.contains(method.getName())) { if (!updateMethodNames.contains(method.getName())) {
return; return;
} }
int modifiers = method.getModifiers(); final int modifiers = method.getModifiers();
if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) { if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) {
return; return;
} }
...@@ -86,11 +85,11 @@ public class MtContextTransformer implements ClassFileTransformer { ...@@ -86,11 +85,11 @@ public class MtContextTransformer implements ClassFileTransformer {
for (int i = 0; i < parameterTypes.length; i++) { for (int i = 0; i < parameterTypes.length; i++) {
CtClass paraType = parameterTypes[i]; CtClass paraType = parameterTypes[i];
if (RUNNABLE_CLASS_NAME.equals(paraType.getName())) { if (RUNNABLE_CLASS_NAME.equals(paraType.getName())) {
String code = String.format("$%d = %s.get($%d);", i + 1, MTCONTEXT_RUNNABLE_CLASS_NAME, i + 1); String code = String.format("$%d = %s.get($%d);", i + 1, MT_CONTEXT_RUNNABLE_CLASS_NAME, i + 1);
logger.info("insert code before method " + method + ": " + code); logger.info("insert code before method " + method + ": " + code);
insertCode.append(code); insertCode.append(code);
} else if (CALLABLE_CLASS_NAME.equals(paraType.getName())) { } else if (CALLABLE_CLASS_NAME.equals(paraType.getName())) {
String code = String.format("$%d = %s.get($%d);", i + 1, MTCONTEXT_CALLABLE_CLASS_NAME, i + 1); String code = String.format("$%d = %s.get($%d);", i + 1, MT_CONTEXT_CALLABLE_CLASS_NAME, i + 1);
logger.info("insert code before method " + method + ": " + code); logger.info("insert code before method " + method + ": " + code);
insertCode.append(code); insertCode.append(code);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册