Skip to content

Commit

Permalink
feat(java): ReplaceResolveSerializer deep copy (#1925)
Browse files Browse the repository at this point in the history

## What does this PR do?
Adjusting the deep copy of ReplaceResolveSerializer.

obj -> writeReplace -> copy -> readResolve -> newObj

<!-- Describe the purpose of this PR. -->

## Related issues
#1849
<!--
Is there any related issue? Please attach here.

- #xxxx0
- #xxxx1
- #xxxx2
-->

## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?

## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
  • Loading branch information
zhaommmmomo authored Nov 4, 2024
1 parent bce372e commit 2ed6adc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* will skip classname writing if object returned by `writeReplace` is different from current class.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class ReplaceResolveSerializer extends AbstractObjectSerializer {
public class ReplaceResolveSerializer extends Serializer {
private static final Logger LOG = LoggerFactory.getLogger(ReplaceResolveSerializer.class);

/**
Expand Down Expand Up @@ -139,6 +139,19 @@ Object writeReplace(Object o) {
}
}
}

Object readResolve(Object o) {
if (readResolveFunc != null) {
return readResolveFunc.apply(o);
} else {
try {
return readResolveMethod.invoke(o);
} catch (Exception e) {
Platform.throwException(e);
throw new IllegalStateException(e);
}
}
}
}

private static final ClassValue<ReplaceResolveInfo> REPLACE_RESOLVE_INFO_CACHE =
Expand Down Expand Up @@ -308,26 +321,41 @@ public Object read(MemoryBuffer buffer) {

private Object readObject(MemoryBuffer buffer) {
Class cls = classResolver.readClassInternal(buffer);
MethodInfoCache jdkMethodInfoCache = getMethodInfoCache(cls);
Object o = jdkMethodInfoCache.objectSerializer.read(buffer);
ReplaceResolveInfo replaceResolveInfo = jdkMethodInfoCache.info;
if (replaceResolveInfo.readResolveMethod == null) {
return o;
}
return replaceResolveInfo.readResolve(o);
}

@Override
public Object copy(Object originObj) {
ReplaceResolveInfo replaceResolveInfo = jdkMethodInfoWriteCache.info;
if (replaceResolveInfo.writeReplaceMethod == null) {
return jdkMethodInfoWriteCache.objectSerializer.copy(originObj);
}
Object newObj = originObj;
newObj = replaceResolveInfo.writeReplace(newObj);
if (needToCopyRef) {
fury.reference(originObj, newObj);
}
MethodInfoCache jdkMethodInfoCache = getMethodInfoCache(newObj.getClass());
newObj = jdkMethodInfoCache.objectSerializer.copy(newObj);
replaceResolveInfo = jdkMethodInfoCache.info;
if (replaceResolveInfo.readResolveMethod != null) {
newObj = replaceResolveInfo.readResolve(newObj);
}
return newObj;
}

private MethodInfoCache getMethodInfoCache(Class<?> cls) {
MethodInfoCache jdkMethodInfoCache = classClassInfoHolderMap.get(cls);
if (jdkMethodInfoCache == null) {
jdkMethodInfoCache = newJDKMethodInfoCache(cls, fury);
classClassInfoHolderMap.put(cls, jdkMethodInfoCache);
}
Object o = jdkMethodInfoCache.objectSerializer.read(buffer);
ReplaceResolveInfo replaceResolveInfo = jdkMethodInfoCache.info;
Method readResolveMethod = replaceResolveInfo.readResolveMethod;
if (readResolveMethod != null) {
if (replaceResolveInfo.readResolveFunc != null) {
return replaceResolveInfo.readResolveFunc.apply(o);
}
try {
return readResolveMethod.invoke(o);
} catch (Exception e) {
Platform.throwException(e);
throw new IllegalStateException("unreachable");
}
} else {
return o;
}
return jdkMethodInfoCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ public void testCopyReplaceCircularClass(Fury fury) {
fury.registerSerializer(o.getClass(), ReplaceResolveSerializer.class);
copyCheck(fury, o);
}
CustomReplaceClass2 o = new CustomReplaceClass2(false, 6);
copyCheck(fury, o);
}

public static class CustomReplaceClass3 implements Serializable {
Expand Down

0 comments on commit 2ed6adc

Please sign in to comment.