Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): deserialize one pojo into another type #2012

Merged
merged 6 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion java/fury-core/src/main/java/org/apache/fury/Fury.java
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ public <T> T deserializeJavaObject(MemoryBuffer buffer, Class<T> cls) {
if (nextReadRefId >= NOT_NULL_VALUE_FLAG) {
ClassInfo classInfo;
if (shareMeta) {
classInfo = classResolver.readClassInfo(buffer);
classInfo = classResolver.readClassInfo(buffer, cls);
} else {
classInfo = classResolver.getClassInfo(cls);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.fury.memory.Platform;
import org.apache.fury.reflect.ReflectionUtils;
import org.apache.fury.reflect.TypeRef;
import org.apache.fury.resolver.ClassInfo;
import org.apache.fury.resolver.ClassResolver;
import org.apache.fury.serializer.CompatibleSerializer;
import org.apache.fury.serializer.NonexistentClass;
Expand Down Expand Up @@ -787,4 +788,12 @@ public static ClassDef buildClassDef(
ClassResolver classResolver, Class<?> type, List<Field> fields, boolean isObjectType) {
return ClassDefEncoder.buildClassDef(classResolver, type, fields, isObjectType);
}

public static ClassDef replaceRootClassTo(
ClassResolver classResolver,
ClassInfo targetCls
) {
return ClassDefEncoder.buildClassDefWithFieldInfos(
classResolver, targetCls.getCls(), targetCls.classDef.getFieldsInfo(), targetCls.classDef.isObjectType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ static List<FieldInfo> buildFieldsInfo(ClassResolver resolver, List<Field> field
/** Build class definition from fields of class. */
static ClassDef buildClassDef(
ClassResolver classResolver, Class<?> type, List<Field> fields, boolean isObjectType) {
List<FieldInfo> fieldInfos = buildFieldsInfo(classResolver, fields);
return buildClassDefWithFieldInfos(classResolver, type, buildFieldsInfo(classResolver, fields), isObjectType);
}

static ClassDef buildClassDefWithFieldInfos(
ClassResolver classResolver, Class<?> type, List<ClassDef.FieldInfo> fieldInfos, boolean isObjectType) {
Map<String, List<FieldInfo>> classLayers = getClassFields(type, fieldInfos);
fieldInfos = new ArrayList<>(fieldInfos.size());
classLayers.values().forEach(fieldInfos::addAll);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.fury.type.TypeUtils;

public class ClassSpec {
public final String entireClassName;
public String entireClassName;
Copy link
Collaborator

@chaokunyang chaokunyang Jan 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep ClassDef immutable abd return a new ClassDef instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure ofc, im still stuck facing exception here and there.


/** Whether current class is enum of component is enum if current class is array. */
public final boolean isEnum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ClassInfo {
// use primitive to avoid boxing
// class id must be less than Integer.MAX_VALUE/2 since we use bit 0 as class id flag.
short classId;
ClassDef classDef;
public ClassDef classDef;
boolean needToWriteClassDef;

ClassInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,33 @@ public ClassInfo readClassInfo(MemoryBuffer buffer) {
return classInfo;
}

public ClassInfo readClassInfo(MemoryBuffer buffer, Class<?> targetClass) {
if (metaContextShareEnabled) {
ClassInfo classInfo = readClassInfoWithMetaShare(buffer, fury.getSerializationContext().getMetaContext());

// replace target class if needed
if (!targetClass.getName().equals(classInfo.getCls().getName())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!targetClass.getName().equals(classInfo.getCls().getName())) {
if (targetClass != classInfo.getCls()) {

this will be much faster

ClassInfo targetClassInfo = getClassInfo(targetClass);
buildClassDef(targetClassInfo);
classInfo.classDef = ClassDef.replaceRootClassTo(this, targetClassInfo);
classInfo.serializer = createSerializer(targetClass);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line won't work as you expect. It just create a serializer for raw targetClass without using type info from peer class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll look into that first

}

return classInfo;
}

int header = buffer.readVarUint32Small14();
ClassInfo classInfo;
if ((header & 0b1) != 0) {
classInfo = readClassInfoFromBytes(buffer, classInfoCache, header);
classInfoCache = classInfo;
} else {
classInfo = getOrUpdateClassInfo((short) (header >> 1));
}
currentReadClass = classInfo.cls;
return classInfo;
}

/**
* Read class info from java data <code>buffer</code>. `classInfoCache` is used as a cache to
* reduce map lookup to load class from binary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ void testTargetHasLessFieldComparedToSourceClass() throws InterruptedException {
ClassCompleteField<String> subclass = new ClassCompleteField<>("subclass", "subclass2");
ClassCompleteField<ClassCompleteField<String>> classCompleteField =
new ClassCompleteField<>(subclass, subclass);
byte[] serialized = getFury(ClassCompleteField.class).serializeJavaObject(classCompleteField);
byte[] serialized = getFury().serializeJavaObject(classCompleteField);
ClassMissingField<ClassMissingField<String>> classMissingField =
getFury(ClassMissingField.class).deserializeJavaObject(serialized, ClassMissingField.class);
getFury().deserializeJavaObject(serialized, ClassMissingField.class);

assertEq(classCompleteField, classMissingField);
}
Expand All @@ -73,10 +73,10 @@ void testTargetHasMoreFieldComparedToSourceClass() throws InterruptedException {

ClassMissingField<String> subclass = new ClassMissingField<>("subclass");
ClassMissingField classMissingField = new ClassMissingField(subclass);
byte[] serialized = getFury(ClassMissingField.class).serializeJavaObject(classMissingField);
byte[] serialized = getFury().serializeJavaObject(classMissingField);

ClassCompleteField classCompleteField =
getFury(ClassCompleteField.class)
getFury()
.deserializeJavaObject(serialized, ClassCompleteField.class);

assertEq(classCompleteField, classMissingField);
Expand Down
Loading