From b266e1c967268f3d41f28080ced0f12b124cc3a6 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Mon, 2 Dec 2024 15:13:55 +0800 Subject: [PATCH] fix classLoader#getURLs() may throw NullPointerException. fix #2968 --- .../core/command/klass100/ClassLoaderCommand.java | 2 +- .../com/taobao/arthas/core/util/ClassLoaderUtils.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/taobao/arthas/core/command/klass100/ClassLoaderCommand.java b/core/src/main/java/com/taobao/arthas/core/command/klass100/ClassLoaderCommand.java index a711d7a8ba7..f283eff4a9d 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/klass100/ClassLoaderCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/klass100/ClassLoaderCommand.java @@ -57,7 +57,7 @@ Constants.WIKI + Constants.WIKI_HOME + "classloader") public class ClassLoaderCommand extends AnnotatedCommand { - private Logger logger = LoggerFactory.getLogger(ClassLoaderCommand.class); + private static Logger logger = LoggerFactory.getLogger(ClassLoaderCommand.class); private boolean isTree = false; private String hashCode; private String classLoaderClass; diff --git a/core/src/main/java/com/taobao/arthas/core/util/ClassLoaderUtils.java b/core/src/main/java/com/taobao/arthas/core/util/ClassLoaderUtils.java index 7be694adebb..a8e49118f91 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/ClassLoaderUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/ClassLoaderUtils.java @@ -9,13 +9,16 @@ import java.util.List; import java.util.Set; +import com.alibaba.arthas.deps.org.slf4j.Logger; +import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; + /** * * @author hengyunabc 2019-02-05 * */ public class ClassLoaderUtils { - + private static Logger logger = LoggerFactory.getLogger(ClassLoaderUtils.class); public static Set getAllClassLoader(Instrumentation inst) { Set classLoaderSet = new HashSet(); @@ -127,7 +130,11 @@ else if (!StringUtils.isEmpty(classLoaderToString) && StringUtils.isEmpty(classL @SuppressWarnings({ "unchecked", "restriction" }) public static URL[] getUrls(ClassLoader classLoader) { if (classLoader instanceof URLClassLoader) { - return ((URLClassLoader) classLoader).getURLs(); + try { + return ((URLClassLoader) classLoader).getURLs(); + } catch (Throwable e) { + logger.error("classLoader: {} getUrls error", classLoader, e); + } } // jdk9