groovy

XStream cannot loadXML from GroovyScriptEngine created classes

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.0-JSR-6
  • Fix Version/s: 1.5.5, 1.6-beta-1
  • Component/s: groovy-jdk
  • Labels:
    None
  • Environment:
    Windows XP
  • Patch Submitted:
    Yes
  • Number of attachments :
    1

Description

GroovyScriptEngine does not expose it's ClassLoader and therefore objects created and serialized using the GSE cannot be read back in.

eg.
...

Class objectClass = new GroovyScriptEngine().getClassByName(path);
Object obj = objectClass.newInstance();

XStream xstream = new XStream();
FileOutputStream out = new FileOutputStream(new File("myobj.xml"));
xstream.toXML(obj, out);
...
// input stream defs removed
obj = xstream.fromXML(xmlIn);

... causes com.thoughtworks.xstream.alias.CannotResolveClassException:, even if you set new GroovyClassLoader() as xstream's classloader it still cannot resolve the class.

Exposing the ClassLoader would enable this. Attached is a GSE that allows this.

Activity

Hide
Paul King added a comment -

Added a getter for GroovyClassLoader. Tested with this script:

// below can't be a script as XStream only seems to like objects with noarg constructors
def path = "SomeGroovyClass"

def engine = new GroovyScriptEngine(".")
def objClass = engine.loadScriptByName(path)
def obj = objClass.newInstance()
println 'obj: ' + obj.dump()

def xstream = new com.thoughtworks.xstream.XStream()

new File("obj.xml").withOutputStream { out ->
    xstream.toXML(obj, out)
}
obj = null

xstream.classLoader = engine.groovyClassLoader
new File("obj.xml").withInputStream { ins ->
    obj = xstream.fromXML(ins)
}

println 'obj: ' + obj.dump()

Groovy would actually let you peak inside and get the private groovyLoader variable but this minor API addition lets both Java and Groovy get the class loader.

Show
Paul King added a comment - Added a getter for GroovyClassLoader. Tested with this script:
// below can't be a script as XStream only seems to like objects with noarg constructors
def path = "SomeGroovyClass"

def engine = new GroovyScriptEngine(".")
def objClass = engine.loadScriptByName(path)
def obj = objClass.newInstance()
println 'obj: ' + obj.dump()

def xstream = new com.thoughtworks.xstream.XStream()

new File("obj.xml").withOutputStream { out ->
    xstream.toXML(obj, out)
}
obj = null

xstream.classLoader = engine.groovyClassLoader
new File("obj.xml").withInputStream { ins ->
    obj = xstream.fromXML(ins)
}

println 'obj: ' + obj.dump()
Groovy would actually let you peak inside and get the private groovyLoader variable but this minor API addition lets both Java and Groovy get the class loader.
Hide
Paul King added a comment -

Forgot to mention. For future reference, a patch file is much better than a copy of a file. A patch against the current head is nicer still. Thanks though for making the suggestion.

Show
Paul King added a comment - Forgot to mention. For future reference, a patch file is much better than a copy of a file. A patch against the current head is nicer still. Thanks though for making the suggestion.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: