我有一个具有第三方不可序列化属性的类,需要将其发送给使用此类的一种方法的UDAF。
由于无法序列化的属性,我无法添加“可序列化的实现”,而且我无法进行子类包装,因为该属性需要在其构造函数中使用参数...
有什么主意吗?
public class ClassWithNoSerializableProperty implements Serializable {
private NoSerializable property;
public ClassWithNoSerializableProperty (String text) {
property = new NoSerializable(text);
}
}
public class NoSerializable {
protected String text;
public NoSerializable(String text) {
this.text = text;
}
}
答案 0 :(得分:1)
使用此类包装NoSerializable
的构造函数
SerializedWrapper<NoSerializable> property = new SerializedWrapper(() -> new NoSerializable(text));
// call this to use it.
property.get();
/**
* Makes an unserializable class serializable through lazy initialization.
*/
public class SerializedWrapper<T> implements Serializable {
private SerializedConstructor<T> constructor;
private transient T instance;
/**
* Creates a serializable wrapper for something.
*/
public SerializedWrapper(SerializedConstructor<T> constructor) {
this.constructor = constructor;
}
/**
* Gets or creates an instance of T.
*/
public T get() {
if (instance == null){
instance = constructor.get();
}
return instance;
}
}
/**
* Dummy interface so we don't have to do (Consumer<T> & Serializable)
*/
public interface SerializedConstructor<T> implements Serializable, Consumer<T> {}