From 79ba5271f9cf1f6da742df1a7f2c33382cb2d5b8 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Wed, 8 Apr 2020 03:17:58 +0800 Subject: [PATCH] fix the issue when the woven code is still running and throw NullPointerException after executing the stop command. #1094 --- spy/src/main/java/java/arthas/Spy.java | 73 +++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/spy/src/main/java/java/arthas/Spy.java b/spy/src/main/java/java/arthas/Spy.java index f4f7692d2..b05620894 100644 --- a/spy/src/main/java/java/arthas/Spy.java +++ b/spy/src/main/java/java/arthas/Spy.java @@ -8,7 +8,12 @@ import java.lang.reflect.Method; * Created by vlinux on 15/8/23. */ public class Spy { - + public static final String ON_BEFORE = "methodOnBegin"; + public static final String ON_RETURN = "methodOnReturnEnd"; + public static final String ON_THROWS = "methodOnThrowingEnd"; + public static final String BEFORE_INVOKE = "methodOnInvokeBeforeTracing"; + public static final String AFTER_INVOKE = "methodOnInvokeAfterTracing"; + public static final String THROW_INVOKE = "methodOnInvokeThrowTracing"; // -- 各种Advice的钩子引用 -- public static volatile Method ON_BEFORE_METHOD; @@ -53,13 +58,7 @@ public class Spy { * to avoid classloader leak. */ public static void destroy() { - CLASSLOADER = null; - ON_BEFORE_METHOD = null; - ON_RETURN_METHOD = null; - ON_THROWS_METHOD = null; - BEFORE_INVOKING_METHOD = null; - AFTER_INVOKING_METHOD = null; - THROW_INVOKING_METHOD = null; + initEmptySpy(); // clear the reference to ArthasClassLoader in AgentLauncher if (AGENT_RESET_METHOD != null) { try { @@ -71,4 +70,62 @@ public class Spy { AGENT_RESET_METHOD = null; } + private static void initEmptySpy() { + try { + Class adviceWeaverClass = Spy.class; + Method onBefore = adviceWeaverClass.getMethod(Spy.ON_BEFORE, int.class, ClassLoader.class, String.class, + String.class, String.class, Object.class, Object[].class); + Method onReturn = adviceWeaverClass.getMethod(Spy.ON_RETURN, Object.class); + Method onThrows = adviceWeaverClass.getMethod(Spy.ON_THROWS, Throwable.class); + Method beforeInvoke = adviceWeaverClass.getMethod(Spy.BEFORE_INVOKE, int.class, String.class, String.class, + String.class, int.class); + Method afterInvoke = adviceWeaverClass.getMethod(Spy.AFTER_INVOKE, int.class, String.class, String.class, + String.class, int.class); + Method throwInvoke = adviceWeaverClass.getMethod(Spy.THROW_INVOKE, int.class, String.class, String.class, + String.class, int.class); + Spy.init(null, onBefore, onReturn, onThrows, beforeInvoke, afterInvoke, throwInvoke); + } catch (Exception e) { + } + } + + /** + * empty method + * + * @see com.taobao.arthas.core.advisor.AdviceWeaver#methodOnBegin(int, + * ClassLoader, String, String, String, Object, Object[]) + * @param adviceId + * @param loader + * @param className + * @param methodName + * @param methodDesc + * @param target + * @param args + */ + public static void methodOnBegin(int adviceId, ClassLoader loader, String className, String methodName, + String methodDesc, Object target, Object[] args) { + } + + /** + * empty method + * + * @see com.taobao.arthas.core.advisor.AdviceWeaver#methodOnReturnEnd(Object) + * @param returnObject + */ + public static void methodOnReturnEnd(Object returnObject) { + } + + public static void methodOnThrowingEnd(Throwable throwable) { + } + + public static void methodOnInvokeBeforeTracing(int adviceId, String owner, String name, String desc, + int lineNumber) { + } + + public static void methodOnInvokeAfterTracing(int adviceId, String owner, String name, String desc, + int lineNumber) { + } + + public static void methodOnInvokeThrowTracing(int adviceId, String owner, String name, String desc, + int lineNumber) { + } }