Solution for the ClassCastException due to ClassLoader issue

后端 未结 5 578
既然无缘
既然无缘 2020-12-03 19:06

I have two ClassLoaders which loads the same class. So, obviously these can\'t cast to one another. But I need to access an object created in the other ClassLoader.

5条回答
  •  情歌与酒
    2020-12-03 19:48

    Reflection isn't that bad, and is appropriate here.
    Is this a Maven plugin, BTW?

    You'll want something like:

    Mojo mojo = (Mojo)descriptor.getClass().getMethod("getMojo").invoke(descriptor);
    

    I'm leaving out a lot - particularly exception handling - but this should lead you to the Javadoc you need. It's quite good, but read carefully.

    If you also have two Mojo classes, the cast will break, and you'll have to do more reflection to do whatever you need to do with the evil-twin Mojo.

提交回复
热议问题