Skip to content

Commit b367c39

Browse files
authored
fix: find loaded class (#578)
1 parent 46bf34d commit b367c39

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/listener/EventProcessor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.arex.agent.bootstrap.model.Mocker;
66
import io.arex.agent.bootstrap.util.AdviceClassesCollector;
77
import io.arex.agent.bootstrap.util.NumberUtil;
8+
import io.arex.agent.bootstrap.util.ReflectUtil;
89
import io.arex.agent.bootstrap.util.StringUtil;
910
import io.arex.agent.bootstrap.util.ServiceLoader;
1011
import io.arex.inst.runtime.config.Config;
@@ -18,6 +19,7 @@
1819
import io.arex.inst.runtime.serializer.StringSerializable;
1920
import io.arex.inst.runtime.util.MockUtils;
2021

22+
import java.lang.reflect.Method;
2123
import java.util.List;
2224

2325
import java.util.Set;
@@ -33,6 +35,7 @@ public class EventProcessor {
3335
private static final String CLOCK_METHOD = "currentTimeMillis";
3436
public static final String EXCLUDE_MOCK_TYPE = "java.util.HashMap-java.lang.String,java.util.HashSet";
3537
private static final AtomicReference<InitializeEnum> INIT_DEPENDENCY = new AtomicReference<>(InitializeEnum.START);
38+
private static final Method FIND_LOADED_METHOD = ReflectUtil.getMethod(ClassLoader.class, "findLoadedClass", String.class);
3639
private static boolean existJacksonDependency = true;
3740
static {
3841
try {
@@ -154,19 +157,15 @@ private static void initClass(ClassLoader contextClassLoader) {
154157
continue;
155158
}
156159
Class.forName(initClass, true, contextClassLoader);
157-
} catch (ClassNotFoundException e) {
160+
} catch (Throwable e) {
161+
// if the class init fails, the next class init will continue
158162
LOGGER.warn(LogManager.buildTitle("init.class"), e);
159163
}
160164
}
161165
}
162166

163167
private static boolean isClassLoaded(String className, ClassLoader classLoader) {
164-
try {
165-
Class<?> clazz = classLoader.loadClass(className);
166-
return clazz != null;
167-
} catch (ClassNotFoundException e) {
168-
return false;
169-
}
168+
return FIND_LOADED_METHOD == null || ReflectUtil.invoke(FIND_LOADED_METHOD, classLoader, className) != null;
170169
}
171170

172171
private static void initLog(ClassLoader contextClassLoader) {

arex-instrumentation-api/src/test/java/io/arex/inst/runtime/listener/EventProcessorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.arex.inst.runtime.serializer.StringSerializable;
1818
import io.arex.agent.bootstrap.util.ServiceLoader;
1919

20+
import java.lang.reflect.Field;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Type;
2223

0 commit comments

Comments
 (0)