groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mickaël SALMON>
Subject Re: please help : GroovyCastException: why ?
Date Tue, 25 Feb 2020 10:37:17 GMT
User scripts were coded Beanshell, I try to migrate to Groovy ...
From: Alessio Stalla <>
Sent: Tuesday, February 25, 2020 11:16
To: <>
Subject: Re: please help : GroovyCastException: why ?

If the user scripts are already existing, which version of Groovy were they targeting? I've
seen this behavior (of treating identifiers starting with an uppercase letter as potential
class names) for my entire life as a Groovy developer, which started several years ago.

On Tue, 25 Feb 2020 at 10:19, Mickaël SALMON <<>>
Unfortunatly yes, that's what I need to do, passing an Article instance into a variable named
"Article", not "article" like it should be ...
Because of existing user scripts which must continue to work without refactoring.

How can I deal with it ?
From: MG <<>>
Sent: Monday, February 24, 2020 23:30
To:<> <<>>;
Mickaël SALMON <<>>
Subject: Re: please help : GroovyCastException: why ?

Article articleTmp = (Article) Article;

in Groovy is the same as

Article articleTmp = (Article) Article.getClass();

in Java.


is of type Class<Article>, so casting it to Article will fail - what you want to do
ist pass an Article instance, not the class, I presume...


On 24/02/2020 18:24, Mickaël SALMON wrote:

I'm using last Groovy version (3.0.1) in my Java application to run user defined scripts (Java
Here is how a script is executed (sorry for the formatting) :

   public Object eval(String scriptName, String script, Map<String, Object> mapVariable)
                   throws CochiseException {
       groovy.lang.Script groovyScript = this.scriptCache.get(scriptName);
       if (groovyScript == null) {
           groovyScript = new GroovyShell().parse(script);
           this.scriptCache.put(scriptName, groovyScript);
       groovyScript.setBinding(new Binding(mapVariable));

I have the following exception when I pass the object "Article" of class "com.sylob.cochise.dm1.ejb.entite.article.Article"
in the Map of variables :
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'interface
com.sylob.cochise.dm1.ejb.entite.article.Article' with class 'java.lang.Class' to class 'com.sylob.cochise.dm1.ejb.entite.article.Article'

Here is the script :

import com.sylob.cochise.dm1.ejb.entite.article.Article;

println "CL import Article : " +  Article.class.getClassLoader()
println "CL var Article: " + Article.getClass().getClassLoader()

Article articleTmp = (Article) Article;
// ... some stuff

This not seem to be a classLoader problem, because same class loader is used to load both
com.sylob.cochise.dm1.ejb.entite.article.Article class in the script and "Article" object
in the calling application.
Also tested with 2.5.9, same error.

What's wrong here ?


View raw message