theigl commented on issue #1854:
URL: https://github.com/apache/fury/issues/1854#issuecomment-2376148388
There was an important interface missing on the proxy: `IWriteReplace` that
exposes the `writeReplace` method. This code now serializes fine with JDK
serialization:
```java
static class ProxyFactory {
static <T> T createProxy(final Class<T> type) {
return new JdkProxyFactory().createProxy(type);
}
public interface IWriteReplace {
Object writeReplace() throws ObjectStreamException;
}
static final class JdkProxyFactory {
@SuppressWarnings("unchecked")
<T> T createProxy(final Class<T> type) {
final JdkHandler handler = new JdkHandler(type);
try {
final ClassLoader cl =
Thread.currentThread().getContextClassLoader();
return (T) Proxy.newProxyInstance(cl, new
Class[]{type, IWriteReplace.class, Serializable.class}, handler);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Could not create
proxy for type [" + type.getName() + "]", e);
}
}
static class JdkHandler implements InvocationHandler,
IWriteReplace, Serializable {
private final String typeName;
private JdkHandler(Class<?> type) {
typeName = type.getName();
}
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
if (isWriteReplaceMethod(method)) {
return writeReplace();
}
return null;
}
public Object writeReplace() throws
ObjectStreamException {
return new ProxyReplacement(typeName);
}
static boolean isWriteReplaceMethod(final Method
method) {
return (method.getReturnType() == Object.class)
&&
(method.getParameterTypes().length == 0) &&
method.getName().equals("writeReplace");
}
}
public static final class ProxyReplacement implements
Serializable {
private final String type;
public ProxyReplacement(final String type) {
this.type = type;
}
private Object readResolve() throws
ObjectStreamException {
try {
final Class<?> clazz =
Class.forName(type, false, Thread.currentThread().getContextClassLoader());
return ProxyFactory.createProxy(clazz);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]