Re: please help : GroovyCastException: why ? - introduce @ClassName annotation ?

2020-02-28 Thread MG

Glad the community could help you & things look Groovy now G-)

Cheers,

mg



On 28/02/2020 09:02, Mickaël SALMON wrote:

Thank you very much for your support.
Except this tricky use case for which I'm sure  will find a solution 
for refactoring the existing scripts, Groovy integration in my Java 
application is OK Now.


Chers, Mickael.



*From:* Jochen Theodorou 
*Sent:* Thursday, February 27, 2020 16:39
*To:* Mickaël SALMON 
*Subject:* Re: please help : GroovyCastException: why ?


Am 27.02.20 um 16:16 schrieb Mickaël SALMON:
> Okay.
>
> In fact, I don't understand which this code works in Java but not in
> Groovy (even if naming a variable with a class name, not in camel case,
> is weird ...).


That is because Groovy has an overlap of variable names and class names
Java does not have.

In Java a=b is clearely assigning the value of variable b to a. If a is
a class instead it will fail compilation. If there is a variable b and b
class a, then the variable will be taken. To ensure you do mean the
class, you have to write a=b.class

In Groovy a=b is different.here b could be also a class b. if there is
no local variable b, then b might be still a class or (dynamic)
property. Thus the compiler will first check if it is a class and then
proceed to make it a (dynamic) property access. In dynamic Groovy this
expression def a=b may never fail compilation, because even if there is
no class b and no local variable b and no static property b, there is
still a possibly dynamic property b. If it then does not exist at
runtime, you will get a MissingPropertyException. But this mechanism
proofed to be problematic for the users. It turned out to be a problem
for the performance. Which is why we said, that if the name b follows
the class naming conventions of upper-case and camel-case, it must be a
class name and otherwise not.

bye Jochen




Re: please help : GroovyCastException: why ?

2020-02-28 Thread Mickaël SALMON
Thank you very much for your support.
Except this tricky use case for which I'm sure  will find a solution for 
refactoring the existing scripts, Groovy integration in my Java application is 
OK Now.

Chers, Mickael.



From: Jochen Theodorou 
Sent: Thursday, February 27, 2020 16:39
To: Mickaël SALMON 
Subject: Re: please help : GroovyCastException: why ?



Am 27.02.20 um 16:16 schrieb Mickaël SALMON:
> Okay.
>
> In fact, I don't understand which this code works in Java but not in
> Groovy (even if naming a variable with a class name, not in camel case,
> is weird ...).


That is because Groovy has an overlap of variable names and class names
Java does not have.

In Java a=b is clearely assigning the value of variable b to a. If a is
a class instead it will fail compilation. If there is a variable b and b
class a, then the variable will be taken. To ensure you do mean the
class, you have to write a=b.class

In Groovy a=b is different.here b could be also a class b. if there is
no local variable b, then b might be still a class or (dynamic)
property. Thus the compiler will first check if it is a class and then
proceed to make it a (dynamic) property access. In dynamic Groovy this
expression def a=b may never fail compilation, because even if there is
no class b and no local variable b and no static property b, there is
still a possibly dynamic property b. If it then does not exist at
runtime, you will get a MissingPropertyException. But this mechanism
proofed to be problematic for the users. It turned out to be a problem
for the performance. Which is why we said, that if the name b follows
the class naming conventions of upper-case and camel-case, it must be a
class name and otherwise not.

bye Jochen


Re: please help : GroovyCastException: why ?

2020-02-27 Thread Mickaël SALMON
Okay.

In fact, I don't understand which this code works in Java but not in Groovy 
(even if naming a variable with a class name, not in camel case, is weird ...).

I made this test class to illustrate this :

package com.sylob.cochise.test;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

public class TestMyWeirdScript {

public static class MyClass {

private final String code;

public MyClass(String code) {
super();
this.code = code;
}

@Override
public String toString() {
return this.code;
}
}

public static String SCRIPT_SRC =
"import com.sylob.cochise.test.TestMyWeirdScript.MyClass;\r\n"
+ "MyClass instance = MyClass;\r\n" + "\r\n" + 
"System.out.println(instance);";

public static void main(String[] args) {
MyClass instance = new MyClass("code");

// throws : 
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object
// 'class com.sylob.cochise.test.TestMyWeirdScript$MyClass' with class 
'java.lang.Class' to
// class 'com.sylob.cochise.test.TestMyWeirdScript$MyClass'
TestMyWeirdScript.execGroovy(instance);

// Compilation & exec OK
TestMyWeirdScript.execJava(instance);

}

public static void execGroovy(MyClass instance) {
Binding binding = new Binding();
binding.setVariable("MyClass", instance);
new GroovyShell(binding).evaluate(TestMyWeirdScript.SCRIPT_SRC);
}

public static void execJava(MyClass MyClass) {
MyClass instance = MyClass;
System.out.println(instance);
}

}
Definitly no solution to handle this case, without refactoring existing scripts 
?

Cheers, Mickael.




From: Paul King 
Sent: Tuesday, February 25, 2020 11:57
To: users@groovy.apache.org 
Subject: Re: please help : GroovyCastException: why ?


It is still a bit hard to let you know the best way to proceed from just the 
sample you have shown without more context.
If I had a class Article and an instance Article (I am assuming from the 
binding), then I would either:

* use an alias for the class, i.e.:

import Article as MyArticle // then use MyArticle everywhere (in that script) 
where you want the class but you'd still need to be careful, some things won't 
work; or

* just reserve the Article binding variable name for the binding only, e.g.:

def article = binding.variables.get('Article') // at start of script
// ... use article instance through script ...
binding.variables.put('Article', article) // at end of script


Cheers, Paul.

On Tue, Feb 25, 2020 at 8:37 PM Mickaël SALMON 
mailto:m...@sylob.com>> wrote:
User scripts were coded Beanshell, I try to migrate to Groovy ...

From: Alessio Stalla mailto:alessiosta...@gmail.com>>
Sent: Tuesday, February 25, 2020 11:16
To: users@groovy.apache.org<mailto:users@groovy.apache.org> 
mailto:users@groovy.apache.org>>
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 
mailto:m...@sylob.com>> wrote:
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 mailto:mg...@arscreat.com>>
Sent: Monday, February 24, 2020 23:30
To: users@groovy.apache.org<mailto:users@groovy.apache.org> 
mailto:users@groovy.apache.org>>; Mickaël SALMON 
mailto:m...@sylob.com>>
Subject: Re: please help : GroovyCastException: why ?

Article articleTmp = (Article) Article;

in Groovy is the same as

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

in Java.

Article.getClass()

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

hth,
mg


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

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

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

Re: please help : GroovyCastException: why ?

2020-02-25 Thread Paul King
It is still a bit hard to let you know the best way to proceed from just
the sample you have shown without more context.
If I had a class Article and an instance Article (I am assuming from the
binding), then I would either:

* use an alias for the class, i.e.:

import Article as MyArticle // then use MyArticle everywhere (in that
script) where you want the class but you'd still need to be careful, some
things won't work; or

* just reserve the Article binding variable name for the binding only, e.g.:

def article = binding.variables.get('Article') // at start of script
// ... use article instance through script ...
binding.variables.put('Article', article) // at end of script


Cheers, Paul.

On Tue, Feb 25, 2020 at 8:37 PM Mickaël SALMON  wrote:

> User scripts were coded Beanshell, I try to migrate to Groovy ...
> --
> *From:* Alessio Stalla 
> *Sent:* Tuesday, February 25, 2020 11:16
> *To:* users@groovy.apache.org 
> *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  wrote:
>
> 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:* users@groovy.apache.org ; Mickaël SALMON <
> m...@sylob.com>
> *Subject:* Re: please help : GroovyCastException: why ?
>
> Article articleTmp = (Article) Article;
>
> in Groovy is the same as
>
> Article articleTmp = (Article) Article.getClass();
>
> in Java.
>
> Article.getClass()
>
> is of type Class, so casting it to Article will fail - what you
> want to do ist pass an Article instance, not the class, I presume...
>
> hth,
> mg
>
>
> On 24/02/2020 18:24, Mickaël SALMON wrote:
>
> Hello,
>
> I'm using last Groovy version (3.0.1) in my Java application to run user
> defined scripts (Java based).
> Here is how a script is executed (sorry for the formatting) :
>
>public Object eval(String scriptName, String script, Map 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));
>return groovyScript.run();
>}
>
> 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 ?
>
> Thanks.
>
>
>


Re: please help : GroovyCastException: why ?

2020-02-25 Thread Mickaël SALMON
User scripts were coded Beanshell, I try to migrate to Groovy ...

From: Alessio Stalla 
Sent: Tuesday, February 25, 2020 11:16
To: users@groovy.apache.org 
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 
mailto:m...@sylob.com>> wrote:
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 mailto:mg...@arscreat.com>>
Sent: Monday, February 24, 2020 23:30
To: users@groovy.apache.org<mailto:users@groovy.apache.org> 
mailto:users@groovy.apache.org>>; Mickaël SALMON 
mailto:m...@sylob.com>>
Subject: Re: please help : GroovyCastException: why ?

Article articleTmp = (Article) Article;

in Groovy is the same as

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

in Java.

Article.getClass()

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

hth,
mg


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

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

   public Object eval(String scriptName, String script, Map 
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));
   return groovyScript.run();
   }

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 ?

Thanks.




Re: please help : GroovyCastException: why ?

2020-02-25 Thread Alessio Stalla
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  wrote:

> 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:* users@groovy.apache.org ; Mickaël SALMON <
> m...@sylob.com>
> *Subject:* Re: please help : GroovyCastException: why ?
>
> Article articleTmp = (Article) Article;
>
> in Groovy is the same as
>
> Article articleTmp = (Article) Article.getClass();
>
> in Java.
>
> Article.getClass()
>
> is of type Class, so casting it to Article will fail - what you
> want to do ist pass an Article instance, not the class, I presume...
>
> hth,
> mg
>
>
> On 24/02/2020 18:24, Mickaël SALMON wrote:
>
> Hello,
>
> I'm using last Groovy version (3.0.1) in my Java application to run user
> defined scripts (Java based).
> Here is how a script is executed (sorry for the formatting) :
>
>public Object eval(String scriptName, String script, Map 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));
>return groovyScript.run();
>}
>
> 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 ?
>
> Thanks.
>
>
>


Re: please help : GroovyCastException: why ?

2020-02-25 Thread 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: users@groovy.apache.org ; 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.

Article.getClass()

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

hth,
mg


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

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

   public Object eval(String scriptName, String script, Map 
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));
   return groovyScript.run();
   }

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 ?

Thanks.




Re: please help : GroovyCastException: why ?

2020-02-24 Thread MG

Article articleTmp = (Article) Article;

in Groovy is the same as

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

in Java.

Article.getClass()

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


hth,
mg


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

Hello,

I'm using last Groovy version (3.0.1) in my Java application to run 
user defined scripts (Java based).

Here is how a script is executed (sorry for the formatting) :

   public Object eval(String scriptName, String script, MapObject> 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));
       return groovyScript.run();
   }

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 ?

Thanks.