You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2022/09/14 22:18:00 UTC

[jira] [Commented] (GROOVY-10757) Regression from 2.5.16+: ClassCastException at runtime

    [ https://issues.apache.org/jira/browse/GROOVY-10757?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17604997#comment-17604997 ] 

Eric Milles commented on GROOVY-10757:
--------------------------------------

Can you say where {{BarPurchaseDao}} fits into the picture?

You could try making the call to {{retrieveAvailableProducts}} from a static-compile method, like this:
{code:groovy}
@CompileStatic
abstract class GenericServicePurchaseBean ... {
  protected abstract List<? extends ProductIdto> retrieveAvailableProducts()
  private void resetProducts() {
    retrieveAvailableProducts()
  }
  @CompileDynamic
  void resetState() {
    // ...
    resetProducts()
    // ...
  }
}
{code}

Not ideal, but changes the call semantics for that one call.  I've done this before when a method makes multiple super.something() references so there is only one _super$n$something_ MOP method.

> Regression from 2.5.16+: ClassCastException at runtime
> ------------------------------------------------------
>
>                 Key: GROOVY-10757
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10757
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.5.16, 2.5.17, 2.5.18
>            Reporter: Mauro Molinari
>            Assignee: Eric Milles
>            Priority: Major
>
> 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:
> {code:groovy}
> @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()
>                 // ...
>         }
> }
> {code}
>  
> {code:groovy}
> @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();
>                 // ...
>         }
> }
> {code}
> The error I get at runtime is this:
> {noformat}
> 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)
>  [...]
> {noformat}
> 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)?



--
This message was sent by Atlassian Jira
(v8.20.10#820010)