Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.TreeMap;

Expand Down Expand Up @@ -79,10 +80,9 @@ private static JsonUtil createJsonUtil() {
private static JsonUtil loadExtensions(String name, ClassLoader classLoader, Map<String, JsonUtil> extensions) {
ServiceLoader<JsonUtil> loader = ServiceLoader.load(JsonUtil.class, classLoader);
Iterator<JsonUtil> it = loader.iterator();
// In JDK 21+, ServiceLoader.hasNext() may throw NoClassDefFoundError
// when checking class dependencies, so we need to catch it here
while (true) {
try {
// ServiceLoader may resolve provider constructors in hasNext(), before next() can be called.
if (!it.hasNext()) {
break;
}
Expand All @@ -93,9 +93,7 @@ private static JsonUtil loadExtensions(String name, ClassLoader classLoader, Map
}
extensions.put(extension.getName(), extension);
}
} catch (Throwable ignored) {
// Ignore loading failures (e.g., NoClassDefFoundError in JDK 25)
// and continue with the next extension
} catch (ServiceConfigurationError | LinkageError | RuntimeException ignored) {
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.apache.dubbo.common.utils.json.TestObjectB;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -396,6 +399,42 @@ void testGetJson2() {
setJson(null);
}

@Test
void testLoadExtensionsWithoutOptionalGsonDependency() throws Exception {
URL classes = GsonImpl.class.getProtectionDomain().getCodeSource().getLocation();
Map<String, JsonUtil> extensions = new HashMap<>();

try (URLClassLoader classLoader = new URLClassLoader(new URL[] {classes}, JsonUtils.class.getClassLoader()) {
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith("com.google.gson.")) {
throw new ClassNotFoundException(name);
}
if (name.equals("org.apache.dubbo.common.json.impl.GsonImpl")) {
synchronized (getClassLoadingLock(name)) {
Class<?> loaded = findLoadedClass(name);
if (loaded == null) {
loaded = findClass(name);
}
if (resolve) {
resolveClass(loaded);
}
return loaded;
}
}
return super.loadClass(name, resolve);
}
}) {
Method loadExtensions =
JsonUtils.class.getDeclaredMethod("loadExtensions", String.class, ClassLoader.class, Map.class);
loadExtensions.setAccessible(true);
loadExtensions.invoke(null, null, classLoader, extensions);
}

Assertions.assertInstanceOf(FastJson2Impl.class, extensions.get("fastjson2"));
Assertions.assertFalse(extensions.containsKey("gson"));
}

private static Field jsonFieldCache;

/**
Expand Down
Loading