Description
Search before asking
- I had searched in the issues and found no similar issues.
Version
0.10.0
Component(s)
Java
Minimal reproduce step
package org.apache.fury;
import org.apache.fury.config.Language;
public class Main1 {
enum Kind {
X,
Y,
Z
}
public static class SomeClass {
private Kind kind;
}
public static void main(String[] args) {
Fury fury = Fury.builder().withLanguage(Language.XLANG)
.withRefTracking(true)
.ignoreBasicTypesRef(true)
.ignoreTimeRef(true)
.ignoreStringRef(false)
.build();
fury.register(SomeClass.class,"MyTypeResolver");
fury.register(Kind.class,"MyKind");
SomeClass someClass = new SomeClass();
byte[] bytes = fury.serialize(someClass);
Object obj = fury.deserialize(bytes);
System.out.println(obj);
}
}
Running this code results in a ClassUnregisteredException. However, if we simply swap the registration order (register Kind.class before SomeClass.class), the code works fine.
What did you expect to see?
I expect to be able to register classes in any order before starting actual serialization/deserialization operations. The registration phase should not require a specific dependency order, especially for nested types like enums within classes. This would make the library more user-friendly, especially when working with complex class hierarchies with many interdependencies.
What did you see instead?
An exception is thrown complaining that the enum class is not registered, even though it is registered right after the class that uses it:
Exception in thread "main" org.apache.fury.exception.ClassUnregisteredException: Class org.apache.fury.Main1$Kind is not registered
at org.apache.fury.resolver.XtypeResolver.buildClassInfo(XtypeResolver.java:301)
at org.apache.fury.resolver.XtypeResolver.getClassInfo(XtypeResolver.java:262)
at org.apache.fury.serializer.StructSerializer.getGenericType(StructSerializer.java:104)
at org.apache.fury.serializer.StructSerializer.lambda$buildFieldGenerics$2(StructSerializer.java:94)
[stack trace truncated]
Anything Else?
After step-by-step debugging, I have traced the exact cause of the issue:
-
When registering
SomeClass
, the code flow entersXtypeResolver.register(Class<?> type, Serializer<?> serializer, String namespace, String typeName, int xtypeId)
around line 201. -
In this method, since
SomeClass
is not an enum, it executes:classInfo.serializer = new StructSerializer(fury, type);
-
Inside
StructSerializer
constructor, it calls:fieldGenerics = buildFieldGenerics(fury, TypeRef.of(cls), fieldAccessors);
-
This method attempts to build generic type information for each field in the class, including the
kind
field which is of typeKind
(enum). -
For each field, it calls
getGenericType(Fury fury, TypeRef<T> type, FieldAccessor fieldAccessor)
which contains:if (resolver.isMonomorphic(cls)) { t.setSerializer(fury.getXtypeResolver().getClassInfo(cls).getSerializer()); return t; }
-
When processing the
kind
field,cls
becomesKind
enum, which is detected as monomorphic. -
This leads to calling
fury.getXtypeResolver().getClassInfo(cls)
for theKind
class:public ClassInfo getClassInfo(Class<?> cls) { ClassInfo classInfo = classInfoMap.get(cls); if (classInfo == null) { classInfo = buildClassInfo(cls); } return classInfo; }
-
Since
Kind
is not yet registered (we register it afterSomeClass
),classInfo
is null, and it callsbuildClassInfo(cls)
. -
Inside
buildClassInfo(cls)
around line 301, it checks if the enum is an inner enum of another enum, and since it's not, it throws:Class<Enum> enclosingClass = (Class<Enum>) cls.getEnclosingClass(); if (enclosingClass != null && enclosingClass.isEnum()) { serializer = new EnumSerializer(fury, (Class<Enum>) cls); xtypeId = getClassInfo(enclosingClass).xtypeId; } else { throw new ClassUnregisteredException(cls); }
-
If we reverse the registration order (register
Kind
first, thenSomeClass
), the error disappears becausegetClassInfo(cls)
can find the already registeredKind
class inclassInfoMap
.
The issue is that the library eagerly analyzes fields during class registration, rather than deferring this analysis until serialization actually begins. This creates an implicit requirement to register types in dependency order, which becomes problematic with complex class hierarchies.
Are you willing to submit a PR?
- I'm willing to submit a PR!