You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "oldshen (via GitHub)" <gi...@apache.org> on 2024/01/30 06:52:17 UTC
[I] native-image编译时,遇到BigInteger类编译失败(java.lang.StackOverflowError) [dubbo]
oldshen opened a new issue, #13706:
URL: https://github.com/apache/dubbo/issues/13706
<!-- If you need to report a security issue please visit https://github.com/apache/dubbo/security/policy -->
### Environment
* Dubbo version: 3.3.0-beta.1
* Java version: 17
### Steps to reproduce this issue
如果定义的接口返回类或参数类型中存在BigInteger类型,在native-image编译时,会抛出异常,提示`java.lang.StackOverflowError`
经排查是`AotUtils`中`registerSerializationType`方法的实现不严谨造成的,具体代码:
``` java
Arrays.stream(registerType.getDeclaredFields()).forEach((field -> registerSerializationType(field.getType(), hints)));
```
如果`registerType` 类型中存在以自己为类型的Field,就会陷入死循环,比如:
``` java
class A implements Serializable{
public static A empty=new A();
}
```
根据上面分析,我们查看BigInteger源码,其存在类似的如下代码:
``` java
public static final BigInteger ZERO = new BigInteger(new int[0], 0);
```
因此`AotUtils`遇到`BigInteger`时会抛出异常;
同理,如果存在两个类,互为对方的Field,也会抛出`java.lang.StackOverflowError`异常,例如:
```
class A implements Serializable{
private B b;
}
class B implements Serializable{
private A a;
}
```
为解决类似循环依赖造成的死循环,增加一个Set<Class<?>>类型的变量,用于去重判断,修改后的`AotUtils`代码如下
``` java
public class AotUtils {
private AotUtils() {
}
public static void registerSerializationForService(Class<?> serviceType, RuntimeHints hints) {
Set<Class<?>> classSet = new HashSet<>();
Arrays.stream(serviceType.getMethods()).forEach((method) -> {
Arrays.stream(method.getParameterTypes())
.filter(x -> !x.equals(serviceType))
.forEach((parameterType) -> registerSerializationType(classSet, parameterType, hints));
registerSerializationType(classSet, method.getReturnType(), hints);
});
classSet.clear();
}
private static void registerSerializationType(Set<Class<?>> classSet, Class<?> registerType, RuntimeHints hints) {
if (classSet.contains(registerType)) {
return;
}
classSet.add(registerType);
if (isPrimitive(registerType)) {
hints.serialization().registerType(TypeReference.of(ClassUtils.getBoxedClass(registerType)));
} else {
if (Serializable.class.isAssignableFrom(registerType)) {
hints.serialization().registerType(TypeReference.of(registerType));
Arrays.stream(registerType.getDeclaredFields())
.filter(x -> !x.equals(registerType))
.forEach((field -> registerSerializationType(classSet, field.getType(), hints)));
if (registerType.getSuperclass() != null) {
registerSerializationType(classSet, registerType.getSuperclass(), hints);
}
}
}
}
private static boolean isPrimitive(Class<?> cls) {
return cls.isPrimitive() || cls == Boolean.class || cls == Byte.class
|| cls == Character.class || cls == Short.class || cls == Integer.class
|| cls == Long.class || cls == Float.class || cls == Double.class
|| cls == String.class || cls == Date.class || cls == Class.class
|| cls.isEnum() || cls.getName().equals("java.lang.Record");
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [I] Failed to build in native-image mode [dubbo]
Posted by "CrazyHZM (via GitHub)" <gi...@apache.org>.
CrazyHZM closed issue #13706: Failed to build in native-image mode
URL: https://github.com/apache/dubbo/issues/13706
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org