Groovy web console

subscribe to the feed Subscribe
to this
site

Using transient with @ExternalizeMethods AST

Published 4 months ago by bitsnaps with tags ast ExternalizeMethods transient
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.transform.*

@ExternalizeMethods
@ToString
class Person {
    String name
    transient int age
}

def p = new Person(name:'Ali', age: 1)
def baos = new ByteArrayOutputStream()
p.writeExternal(new ObjectOutputStream(baos))
def p2 = new Person()
p2.readExternal( new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())))
assert p2.toString()=='Person(Ali, 0)'