Question

i got a problem with using XStream in my RCP Application. I've added plugin with Xstream libs and created object class containing info about books.

package Library.objects;
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("book")
public class Book {

   @XStreamAlias("id")
   private int id;

   @XStreamAlias("name")
   private String name;

   @XStreamAlias("author")
   private String author;

   @XStreamAlias("desc")
   private String desc;

   @XStreamAlias("status")
   private boolean status;

//some methods here
}

And in my other class i wanted to test if this lib works. So I created smth like this

package Library.parts;
public class test {
private Table table;
ArrayList<Book> books;
MockBooks MockElement;
private Text text;

public test() {
}

/**
 * Create contents of the view part.
 *  
 */
@PostConstruct
public void createControls(Composite parent) {
    parent.setLayout(null);
    Display display = parent.getDisplay();  
    final Shell shell = new Shell(display);



    MockElement = new MockBooks();
    //XMLBooks xml = new XMLBooks();
    XStream xstream = new XStream(new DomDriver());
    xstream.alias("book",Library.objects.Book.class);
    Book test = new Book(1,"test1","auth1","desc1","true");
    String result = xstream.toXML(test);
            Book test2 = (Book) xstream.fromXML(result);

            //other stuff in window
}

Parsing from object to xml works perfectly, but parsing from XML string (or file) gives me alle the time same error:

com.thoughtworks.xstream.mapper.CannotResolveClassException: Library.objects.Book

I got this problem only when I use RCP. In clean java applications xstream works fine. Any ideas why?

Was it helpful?

Solution

The "problem" here is that Eclipse RCP based upon OSGi (the Equinox Implementation). OSGi is a framework for creating modular and flexible Java applications. Each module has its own class loader (http://www.eclipsezone.com/articles/eclipse-vms/). This is different to a standard Java application, where all classes are loaded with the same class loader (in general).

So, what is the problem? Lets say, Module A has a class called foo.Bar and module C has also a class called foo.Bar. In standard Java, this is not possible - you can not have 2 Classes with the same (fully qualified) class name. So reflection works - you can use Reflection to "rebuild" foo.Bar.

But when there are x classes with that name, which one should be recreated?

This is the problem with XStream: It uses OSGi incompatible stuff. And this makes it incompatible with Eclipse RCP if you just add the Jar to the build path.

You need to add it as an OSGi bundle. Lucky you, someone did the work before. You can grab XStream as an OSGi-Bundle: http://mvnrepository.com/artifact/org.apache.servicemix.bundles/org.apache.servicemix.bundles.xstream/1.4_1


See also:

OTHER TIPS

XStream is available on Eclipse Orbit. Eclipse Orbit Projecthttp://www.eclipse.org/orbit/ There, you can download XStream as an Eclipse plugin and use in your Eclipse RCP projects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top