【问题标题】:Groovy Expando SerializableGroovy Expando 可序列化
【发布时间】:2011-05-06 07:54:31
【问题描述】:

有没有办法序列化可以通过动态添加的属性检索的 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

感谢您的建议!

【问题讨论】:

    标签: java groovy serializable expando


    【解决方案1】:

    我不认为这是可能的,它是一个long standing feature request,但正如 Jochen 所说,闭包应该被序列化成什么是有问题的......

    【讨论】:

    • 嗯,好吧!所以我理解这个问题。我还检查了 GroovyObjectSupport.java 并看到 metaClass 字段是瞬态的。所以不能序列化。 MOP 对序列化具有挑战性:D 再次感谢您
    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2012-03-27
    • 2015-01-30
    相关资源
    最近更新 更多