Search code examples
javaeclipsearraylistrcpe4

Cast to the same type in two different plugins


My app is made out of 2 plugins.

For each plugin I declared a class called TYPE since I need a custom data-type for ArrayList's. The class TYPE is identical for either of the plugins.

The scenario is the following:

 Plugin1.TYPE - Plugin2.TYPE
 Plugin1.ArrayList<TYPE> - Plugin2.ArrayList<TYPE>

Now I'd like to assign the content of Plugin1.ArrayList in the Plugin2.ArrayList, but even though the class TYPE is identical, I get an issue with cast.

Basically, even if the two classes have the same content they are considered as different entities and so the cast exception pops up.

  plugin1.TYPE cannot be cast to plugin2.TYPE 

Any help? How can I deal with it?


Solution

  • Can you make a third plugin that inherits the other two? That is your best solution, because classloader hell is never, ever, fun.

    Alternatively, can you create a third plugin that contains all of your code that the other two can use?

    If you cannot use either of those solutions, and are absolutely stuck with the situation, your best bet is to use xstream. What you will have to do is write out the objects as XML in one plugin, pass the XML as a String to the second plugin, and have that plugin load your objects back into memory from there. Each plugin must use its own version of XStream, but that should work.

    (Note you may be able to simply serialize your objects rather than using XStream, that depends upon your webserver, and whether is is distributed across different running Java processes)

    Cheers,

          Emily