Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.
Open
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author: yuanyuan
* @date: 2023/9/25 8:22 下午
*/
public class SerializeUtils {

public static Object serializeTransform(Object source, ClassLoader targetClassLoader) {
private static Object _serializeTransform(Object source, ClassLoader targetClassLoader) {
try {
if (null == source
|| source.getClass().getClassLoader() == targetClassLoader
|| targetClassLoader.loadClass(source.getClass().getName()).getClassLoader() == source
.getClass().getClassLoader()) {
return source;
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Object target;
ClassLoader currentContextClassloader = Thread.currentThread().getContextClassLoader();
try {
Expand All @@ -56,4 +70,22 @@ public static Object serializeTransform(Object source, ClassLoader targetClassLo
}
return target;
}

public static Object serializeTransform(Object originalSource, ClassLoader targetClassLoader) {
if (originalSource.getClass().isArray()) {
Object[] sources = (Object[]) originalSource;
if (sources.length > 0) {
Object[] targets = (Object[]) Array.newInstance(sources.getClass()
.getComponentType(), sources.length);
for (int i = 0; i < sources.length; i++) {
targets[i] = _serializeTransform(sources[i], targetClassLoader);
}
return (Object) targets;
} else {
return originalSource;
}
} else {
return _serializeTransform(originalSource, targetClassLoader);
}
}
}