mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
修复ServiceLoaderUtil.loadFirstAvailable在JDK24+后未捕获异常导致的报错问题(pr#4098@Github)
This commit is contained in:
@@ -49,11 +49,20 @@ public class JdkServiceLoaderUtil {
|
||||
*/
|
||||
public static <T> T loadFirstAvailable(final Class<T> clazz) {
|
||||
final Iterator<T> iterator = load(clazz).iterator();
|
||||
while (iterator.hasNext()) {
|
||||
while (true) {
|
||||
final T instance;
|
||||
try {
|
||||
return iterator.next();
|
||||
} catch (final ServiceConfigurationError ignore) {
|
||||
// ignore
|
||||
// 注意:JDK 24+ 下 hasNext() 和 next() 均可能触发 NoClassDefFoundError
|
||||
if (!iterator.hasNext()) {
|
||||
break;
|
||||
}
|
||||
instance = iterator.next();
|
||||
} catch (ServiceConfigurationError | NoClassDefFoundError e) {
|
||||
// 安全忽略当前实现,尝试下一个
|
||||
continue;
|
||||
}
|
||||
if (instance != null) {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user