有没有办法序列化一个expando子类,可以使用动态添加的属性检索它。用例子;
class Sexpando extends Expando implements Serializable{
//String testProp
static final long serialVersionUID = -2056428816613381087L
String toString() {
"an object of Sexpando - $serialVersionUID"
}
}
和
class SexpandoTest {
static main(args) {
def s = new Sexpando()
s.testProp = "small test string"
println s.properties
def file = new File('objects.dta')
def out = file.newOutputStream()
def oos = new ObjectOutputStream(out)
oos.writeObject(s)
oos.close()
def retrieved = []
file.eachObject { retrieved << it }
retrieved.each { println it.properties }
}}
我得到了输出:
[testProp:small test string]
[:]
我也尝试过与Sexpando对象的原始 testProp 字段相同的示例(上面已注释掉)
可以从HERE
检查Groovy的原始Expando.java感谢您的任何建议!