Skip to content

Commit

Permalink
fix(java): try replace classdef inplace
Browse files Browse the repository at this point in the history
  • Loading branch information
orisgarno committed Jan 21, 2025
1 parent a899245 commit 128b203
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
28 changes: 18 additions & 10 deletions java/fury-core/src/main/java/org/apache/fury/meta/ClassDef.java
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 @@ -105,13 +106,13 @@ public class ClassDef implements Serializable {
}
};

private final ClassSpec classSpec;
private final List<FieldInfo> fieldsInfo;
private final boolean isObjectType;
private ClassSpec classSpec;
private List<FieldInfo> fieldsInfo;
private boolean isObjectType;
// Unique id for class def. If class def are same between processes, then the id will
// be same too.
private final long id;
private final byte[] encoded;
private long id;
private byte[] encoded;
private transient List<Descriptor> descriptors;

ClassDef(
Expand Down Expand Up @@ -788,9 +789,16 @@ public static ClassDef buildClassDef(
return ClassDefEncoder.buildClassDef(classResolver, type, fields, isObjectType);
}

// public ClassDef replaceRootClassTo(
// Class<?> targetCls
// ) {
// this.classSpec.entireClassName = targetCls.getName();
// }
public void replaceRootClassTo(
ClassResolver classResolver,
ClassInfo targetCls
) {
ClassDef classDef = ClassDefEncoder.buildClassDefWithFieldInfos(classResolver, targetCls.getCls(), targetCls.classDef.getFieldsInfo(), true);
this.fieldsInfo = classDef.fieldsInfo;
this.classSpec = classDef.classSpec;
this.id = classDef.id;
this.encoded = classDef.encoded;
this.descriptors = classDef.descriptors;
this.isObjectType = 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;

/** 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 @@ -1678,8 +1678,12 @@ public ClassInfo readClassInfo(MemoryBuffer buffer, Class<?> targetClass) {
if (metaContextShareEnabled) {
ClassInfo classInfo = readClassInfoWithMetaShare(buffer, fury.getSerializationContext().getMetaContext());
ClassInfo targetClassInfo = getClassInfo(targetClass);
Serializer<Object> serializer = targetClassInfo.getSerializer();
classInfo.setSerializer(this, serializer);

buildClassDef(classInfo);
buildClassDef(targetClassInfo);

classInfo.classDef.replaceRootClassTo(this, targetClassInfo);

return classInfo;
}

Expand Down

0 comments on commit 128b203

Please sign in to comment.