Details
Description
I've found a very nasty bug in Groovy 2.5.16+.
It's really hard to extract a simple test case, so I'll try to explain what is going on as much as possible.
I have a @CompileStatic Groovy class hierarchy. Compilation succeeds, but a ClassCastException happens at runtime. The hierarchy is like this:
@Named @CompileStatic @Scope('session') abstract class GenericServicePurchaseBean<T extends GenericServicePurchaseDao> implements Serializable { T dao void setDao(final T dao) { this.dao = dao } protected abstract List<? extends ProductIdto> retrieveAvailableProducts() @CompileDynamic // see note below void resetState() { // ... retrieveAvailableProducts() // ... } }
@Named @CompileStatic @Scope('session') class FooPurchaseBean extends GenericServicePurchaseBean<FooPurchaseDao> { @Inject @Named('fooPurchaseDao') @Override void setDao(final FooPurchaseDao dao) { super.setDao(dao) } @Override protected List<? extends ProductIdto> retrieveAvailableProducts() { dao.loadAllAvailableProductsForNewPurchase(customer) } @Override void resetState() { super.resetState(); // ... } }
The error I get at runtime is this:
Caused by: java.lang.ClassCastException: class it.dcssrl.shop.web.purchase.view.dao.FooPurchaseDao$$EnhancerBySpringCGLIB$$271ceca2 cannot be cast to class it.dcssrl.shop.web.purchase.view.dao.BarPurchaseDao (it.dcssrl.shop.web.purchase.view.dao.FooPurchaseDao$$EnhancerBySpringCGLIB$$271ceca2 and it.dcssrl.shop.web.purchase.view.dao.BarPurchaseDao are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @5a075a) at it.dcssrl.shop.web.purchase.view.FooPurchaseBean.retrieveAvailableProducts(FooPurchaseBean.groovy:217) at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:234) at it.dcssrl.shop.web.purchase.view.GenericServicePurchaseBean.resetState(GenericServicePurchaseBean.groovy:682) at it.dcssrl.shop.web.purchase.view.FooPurchaseBean.resetState(FooPurchaseBean.groovy:148) [...]
If I remove the Groovy compiler optimization to enable Indy compilation, the error happens all the same, but the stack trace does not have the line about org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:234).
Please note that:
- this problem does not happen with Groovy 2.5.15
- it happens with Groovy 2.5.16, 2.5.17 and 2.5.18
- it DOES NOT happen if compiling/running the application within Eclipse/WTP and Groovy Eclipse plugin
- the resetState() method above in the base class is marked with @CompileDynamic because of another nasty problem I was not able to isolate, which otherwise produces another ClassCastException at runtime, although completely different from this one; this has been happening from 2.5.13 at least (it was not occurring with 2.5.9)
Any idea of what is going on? And of a possible not-too-dirty workaround (apart from downgrading to Groovy 2.5.15)?