如何在UDAF中使用第三方不可序列化对象? [英] How to use a third party non serializable object in a UDAF?
本文介绍了如何在UDAF中使用第三方不可序列化对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有第三方不可序列化属性的类,需要将其发送给使用此类的一种方法的UDAF.
I have a class with a third party non serializable property that I need to send to a UDAF that use one method of this class.
由于无法序列化的属性,我无法添加可序列化的实现",而且我无法进行子类包装,因为该属性需要在其构造函数中使用参数...
I can't add the "implements Serializable" beacuse the non serializable property, and I cant do a child class wrapper because the property nees a parameter in their constructor...
有什么主意吗?
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;
}
}
推荐答案
使用此类包装 NoSerializable
的构造函数
Use this class to wrap NoSerializable
's constructor
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> {}
这篇关于如何在UDAF中使用第三方不可序列化对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文