You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Paul Henry <pa...@futrix.com> on 2015/06/17 03:57:44 UTC

No Such Property - When executing script from Java.

Hi All,

I am trying to run a groovy script from Java and getting the following error

<quote>Exception in thread "main"
org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No
such property: formFilled for class: Bugger</quote>

My script looks like this.

<quote>
class Action {
    String actionName
    Action(actionName) {
        this.actionName = actionName;
    }
    void execute() {
        println "Doing $actionName"
    }
}

formFilled = new Action("Form Filled")

mailSent = new Action("Mail Sent")

def invoke(action) {
    this."$action".execute()
}

//----- These are runnable when run as a script.
formFilled.execute()
invoke("mailSent")
</quote>

If i execute the script directly then the last two lines execute and
produce the expected Strings printed to the standard out.

<quote>
Doing Form Filled
Doing Mail Sent
</quote>

I am trying to run it from Java with something like the following.

<quote>
Class scriptClass = new GroovyClassLoader().parseClass(new
File(Bugger.groovy));
Object scriptInstance = scriptClass.newInstance();
scriptClass.getDeclaredMethod("invoke", new Class[]
{}).invoke(scriptInstance, new Object[] {"formFilled"});
</quote>

But I keep getting the No Such property exception. Ive looked around for
solutions, but either Im missing something so basic its generally
understood, or i haven't found the right part of the internet.

So Two questions.

1) why is the property not available / hidden. I don't understand why its
not available to a method within what I expect is the class.

2) what change do I have to make to my code snippets to get it to work.

(Note: I've created a simple example of the problem, rather than post my
actual code. I am using groovy to define a DSL for describing actions. We
then parse a script in that DSL defining different actions. During
operation we want to trigger individual actions to execute on certain
occurrences)

Cheers

-Paul



Paul Henry
Senior Technical Developer |
79 Boulcott Street, Level 2, Wellington 6011, New Zealand
<https://maps.google.com/maps?q=79+Boulcott+Street,+Level+2,+Wellington+6011,+New+Zealand>

tel +64 4 499 1327 | mob +64 22 161 8700
paul.henry@futrix.com | www.futrix.com | LinkedIn
<http://www.linkedin.com/company/futrix-ltd>

Re: No Such Property - When executing script from Java.

Posted by Dinko Srkoč <di...@gmail.com>.
On 19 June 2015 at 03:09, Erick Nelson <er...@gmail.com> wrote:
>
> Well, i know see what is causing my problem , but i can't say I understand why it is a problem.
> Maybe a misunderstanding I have on exaction how @Field works?
>
> I was declaring my object like this....
>
>      @Field Action mailSent
>      mailSent = new Action("Mail Sent")
>
> or like this...
>
>      @Field mailSent
>      mailSent = new Action("Mail Sent")
>
> Both of these ways of declare an instance of Action causes an error.
>
> When I change it to your way ...
>
>      @Field mailSent = new Action("Mail Sent")
>
> or this...
>
>      @Field Action mailSent = new Action("Mail Sent")
>
> The problem goes away when run from java.
>
> I don't see any problems when running as Groovy with any of the above declarations.
>

    @Field mailSent
    mailSent = new Action("Mail Sent")

Even though `mailSent` is a field it is still initialised in the `run`
method, thus is `null` when you call ìnvoke` from java.

    @Field mailSent = new Action("Mail Sent")

This time `mailSent` is initialised during script object construction.

Cheers,
Dinko

>
> Here is my full stacktrace
>
> Exception in thread "main" java.lang.NullPointerException: Cannot invoke method execute() on null object
> at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:88)
> at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
> at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:114)
> at TestGroovy.invoke(TestGroovy.groovy:48)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:483)
> at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
> at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
> at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207)
> at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
> at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:807)
> at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:44)
> at groovy.lang.Script.invokeMethod(Script.java:78)
> at MyTest.main(MyTest.java:13)
>
>
> On Thu, Jun 18, 2015 at 3:37 PM, Paul Henry <pa...@futrix.com> wrote:
>>
>> Hi All,
>>
>> Thanks for the help. Using the @Field annotation has solved the issue. (And it works for me both within Eclipse on Windows, and deployed within Tomcat on Windows)
>>
>> Looking at the docs on the @Field Annotation also helped me to understand why this was happening. If I understand correctly when a script is compiled into a Script class, locally defined properties are actually placed inside the generated run method, and are not compiled to properties of the Script class itself. The @Field annotation changes that behaviour and makes them private fields of the script class. Which then means that other methods within the script class can reference them.
>>
>> Makes sense now that I know what was going on, but was kinda frustrating at the time...
>>
>>
>>
>>
>>
>>
>> Paul Henry
>> Senior Technical Developer |
>> 79 Boulcott Street, Level 2, Wellington 6011, New Zealand
>> tel +64 4 499 1327 | mob +64 22 161 8700
>> paul.henry@futrix.com | www.futrix.com | LinkedIn
>>
>>
>>
>> On 19 June 2015 at 03:36, Erick Nelson <er...@gmail.com> wrote:
>>>
>>> I'll try from a linux box then... later today. Maybe it has something to do with running in Eclipse....
>>>
>>> On Thu, Jun 18, 2015 at 1:35 AM, Dinko Srkoč <di...@gmail.com> wrote:
>>>>
>>>> On 17 June 2015 at 18:08, Erick Nelson <er...@gmail.com> wrote:
>>>>>
>>>>> Yes, removing the &execute and changing invoke back to...
>>>>>
>>>>> void invoke (String action) {
>>>>>     this."${action}".execute()
>>>>> }
>>>>>
>>>>> does work , from Groovy, but when called from Java ....
>>>>>
>>>>>         File file = new File("src", "TestGroovy.groovy");
>>>>>         Class groovyClass = new GroovyClassLoader().parseClass(file);
>>>>>         GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
>>>>>         groovyObject.invokeMethod("invoke", new String [] { "mailSent" });
>>>>>
>>>>> I get an error ...
>>>>>
>>>>> Exception in thread "main" java.lang.NullPointerException: Cannot invoke method execute() on null object. This is thrown from the this."${action}".execute() line.
>>>>>
>>>>> It is only when I use the &execute that it works in both Java and Groovy.
>>>>
>>>>
>>>> Hmmm. It works on my machine (my favourite line ;-) ):
>>>>
>>>> -----------------------------------------
>>>> $ java -version
>>>> java version "1.7.0_76"
>>>> Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
>>>> Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
>>>> $ cat Bugger.groovy
>>>> import groovy.transform.Field
>>>>
>>>> class Action {
>>>>     String actionName
>>>>     Action(actionName) {
>>>>         this.actionName = actionName
>>>>     }
>>>>     void execute() {
>>>>         println "Doing $actionName"
>>>>     }
>>>> }
>>>>
>>>> @Field formFilled = new Action("Form Filled")
>>>>
>>>> @Field mailSent = new Action("Mail Sent")
>>>>
>>>> def invoke(action) {
>>>>     this."$action".execute()
>>>> }
>>>> $ cat Main.java
>>>> import java.io.File;
>>>> import groovy.lang.GroovyClassLoader;
>>>> import groovy.lang.GroovyObject;
>>>>
>>>> public class Main {
>>>>
>>>>     public static void main (String [] args) {
>>>>         try {
>>>>             File file = new File("Bugger.groovy");
>>>>             Class grClass = new GroovyClassLoader().parseClass(file);
>>>>             GroovyObject groovyInstance = (GroovyObject) grClass.newInstance();
>>>>             groovyInstance.invokeMethod("invoke", new String[] { "mailSent"});
>>>>         } catch (Exception e) {
>>>>             e.printStackTrace();
>>>>         }
>>>>     }
>>>> }
>>>> $ javac -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar Main.java
>>>> $ java -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar:. Main
>>>> Doing Mail Sent
>>>> $
>>>> -----------------------------------------
>>>>
>>>> Cheers,
>>>> Dinko
>>>>
>>>
>>
>

Re: No Such Property - When executing script from Java.

Posted by Erick Nelson <er...@gmail.com>.
Well, i know see what is causing my problem , but i can't say I understand
why it is a problem.
Maybe a misunderstanding I have on exaction how @Field works?

I was declaring my object like this....

     @Field Action mailSent
     mailSent = new Action("Mail Sent")

or like this...

     @Field mailSent
     mailSent = new Action("Mail Sent")

Both of these ways of declare an instance of Action causes an error.

When I change it to your way ...

     @Field mailSent = new Action("Mail Sent")

or this...

     @Field Action mailSent = new Action("Mail Sent")

The problem goes away when run from java.

I don't see any problems when running as Groovy with any of the above
declarations.

Here is my full stacktrace

Exception in thread "main" java.lang.NullPointerException: Cannot invoke
method execute() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:88)
at
org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at
org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:114)
at TestGroovy.invoke(TestGroovy.groovy:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:807)
at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:44)
at groovy.lang.Script.invokeMethod(Script.java:78)
at MyTest.main(MyTest.java:13)


On Thu, Jun 18, 2015 at 3:37 PM, Paul Henry <pa...@futrix.com> wrote:

> Hi All,
>
> Thanks for the help. Using the @Field annotation has solved the issue.
> (And it works for me both within Eclipse on Windows, and deployed within
> Tomcat on Windows)
>
> Looking at the docs on the @Field Annotation also helped me to understand
> why this was happening. If I understand correctly when a script is compiled
> into a Script class, locally defined properties are actually placed inside
> the generated run method, and are not compiled to properties of the Script
> class itself. The @Field annotation changes that behaviour and makes them
> private fields of the script class. Which then means that other methods
> within the script class can reference them.
>
> Makes sense now that I know what was going on, but was kinda frustrating
> at the time...
>
>
>
>
>
>
> Paul Henry
> Senior Technical Developer |
> 79 Boulcott Street, Level 2, Wellington 6011, New Zealand
> <https://maps.google.com/maps?q=79+Boulcott+Street,+Level+2,+Wellington+6011,+New+Zealand>
>
> tel +64 4 499 1327 | mob +64 22 161 8700
> paul.henry@futrix.com | www.futrix.com | LinkedIn
> <http://www.linkedin.com/company/futrix-ltd>
>
>
> On 19 June 2015 at 03:36, Erick Nelson <er...@gmail.com> wrote:
>
>> I'll try from a linux box then... later today. Maybe it has something to
>> do with running in Eclipse....
>>
>> On Thu, Jun 18, 2015 at 1:35 AM, Dinko Srkoč <di...@gmail.com>
>> wrote:
>>
>>> On 17 June 2015 at 18:08, Erick Nelson <er...@gmail.com> wrote:
>>>
>>>> Yes, removing the &execute and changing invoke back to...
>>>>
>>>> void invoke (String action) {
>>>>     this."${action}".execute()
>>>> }
>>>>
>>>> does work , from Groovy, but when called from Java ....
>>>>
>>>>         File file = new File("src", "TestGroovy.groovy");
>>>>         Class groovyClass = new GroovyClassLoader().parseClass(file);
>>>>         GroovyObject groovyObject = (GroovyObject)
>>>> groovyClass.newInstance();
>>>>         groovyObject.invokeMethod("invoke", new String [] { "mailSent"
>>>> });
>>>>
>>>> I get an error ...
>>>>
>>>> Exception in thread "main" java.lang.NullPointerException: Cannot
>>>> invoke method execute() on null object. This is thrown from the
>>>> this."${action}".execute() line.
>>>>
>>>> It is only when I use the &execute that it works in both Java and
>>>> Groovy.
>>>>
>>>
>>> Hmmm. It works on my machine (my favourite line ;-) ):
>>>
>>> -----------------------------------------
>>> $ java -version
>>> java version "1.7.0_76"
>>> Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
>>> Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
>>> $ cat Bugger.groovy
>>> import groovy.transform.Field
>>>
>>> class Action {
>>>     String actionName
>>>     Action(actionName) {
>>>         this.actionName = actionName
>>>     }
>>>     void execute() {
>>>         println "Doing $actionName"
>>>     }
>>> }
>>>
>>> @Field formFilled = new Action("Form Filled")
>>>
>>> @Field mailSent = new Action("Mail Sent")
>>>
>>> def invoke(action) {
>>>     this."$action".execute()
>>> }
>>> $ cat Main.java
>>> import java.io.File;
>>> import groovy.lang.GroovyClassLoader;
>>> import groovy.lang.GroovyObject;
>>>
>>> public class Main {
>>>
>>>     public static void main (String [] args) {
>>>         try {
>>>             File file = new File("Bugger.groovy");
>>>             Class grClass = new GroovyClassLoader().parseClass(file);
>>>             GroovyObject groovyInstance = (GroovyObject)
>>> grClass.newInstance();
>>>             groovyInstance.invokeMethod("invoke", new String[] {
>>> "mailSent"});
>>>         } catch (Exception e) {
>>>             e.printStackTrace();
>>>         }
>>>     }
>>> }
>>> $ javac -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar Main.java
>>> $ java -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar:. Main
>>> Doing Mail Sent
>>> $
>>> -----------------------------------------
>>>
>>> Cheers,
>>> Dinko
>>>
>>>
>>
>

Re: No Such Property - When executing script from Java.

Posted by Paul Henry <pa...@futrix.com>.
Hi All,

Thanks for the help. Using the @Field annotation has solved the issue. (And
it works for me both within Eclipse on Windows, and deployed within Tomcat
on Windows)

Looking at the docs on the @Field Annotation also helped me to understand
why this was happening. If I understand correctly when a script is compiled
into a Script class, locally defined properties are actually placed inside
the generated run method, and are not compiled to properties of the Script
class itself. The @Field annotation changes that behaviour and makes them
private fields of the script class. Which then means that other methods
within the script class can reference them.

Makes sense now that I know what was going on, but was kinda frustrating at
the time...






Paul Henry
Senior Technical Developer |
79 Boulcott Street, Level 2, Wellington 6011, New Zealand
<https://maps.google.com/maps?q=79+Boulcott+Street,+Level+2,+Wellington+6011,+New+Zealand>

tel +64 4 499 1327 | mob +64 22 161 8700
paul.henry@futrix.com | www.futrix.com | LinkedIn
<http://www.linkedin.com/company/futrix-ltd>


On 19 June 2015 at 03:36, Erick Nelson <er...@gmail.com> wrote:

> I'll try from a linux box then... later today. Maybe it has something to
> do with running in Eclipse....
>
> On Thu, Jun 18, 2015 at 1:35 AM, Dinko Srkoč <di...@gmail.com>
> wrote:
>
>> On 17 June 2015 at 18:08, Erick Nelson <er...@gmail.com> wrote:
>>
>>> Yes, removing the &execute and changing invoke back to...
>>>
>>> void invoke (String action) {
>>>     this."${action}".execute()
>>> }
>>>
>>> does work , from Groovy, but when called from Java ....
>>>
>>>         File file = new File("src", "TestGroovy.groovy");
>>>         Class groovyClass = new GroovyClassLoader().parseClass(file);
>>>         GroovyObject groovyObject = (GroovyObject)
>>> groovyClass.newInstance();
>>>         groovyObject.invokeMethod("invoke", new String [] { "mailSent"
>>> });
>>>
>>> I get an error ...
>>>
>>> Exception in thread "main" java.lang.NullPointerException: Cannot invoke
>>> method execute() on null object. This is thrown from the
>>> this."${action}".execute() line.
>>>
>>> It is only when I use the &execute that it works in both Java and Groovy.
>>>
>>
>> Hmmm. It works on my machine (my favourite line ;-) ):
>>
>> -----------------------------------------
>> $ java -version
>> java version "1.7.0_76"
>> Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
>> Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
>> $ cat Bugger.groovy
>> import groovy.transform.Field
>>
>> class Action {
>>     String actionName
>>     Action(actionName) {
>>         this.actionName = actionName
>>     }
>>     void execute() {
>>         println "Doing $actionName"
>>     }
>> }
>>
>> @Field formFilled = new Action("Form Filled")
>>
>> @Field mailSent = new Action("Mail Sent")
>>
>> def invoke(action) {
>>     this."$action".execute()
>> }
>> $ cat Main.java
>> import java.io.File;
>> import groovy.lang.GroovyClassLoader;
>> import groovy.lang.GroovyObject;
>>
>> public class Main {
>>
>>     public static void main (String [] args) {
>>         try {
>>             File file = new File("Bugger.groovy");
>>             Class grClass = new GroovyClassLoader().parseClass(file);
>>             GroovyObject groovyInstance = (GroovyObject)
>> grClass.newInstance();
>>             groovyInstance.invokeMethod("invoke", new String[] {
>> "mailSent"});
>>         } catch (Exception e) {
>>             e.printStackTrace();
>>         }
>>     }
>> }
>> $ javac -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar Main.java
>> $ java -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar:. Main
>> Doing Mail Sent
>> $
>> -----------------------------------------
>>
>> Cheers,
>> Dinko
>>
>>
>

Re: No Such Property - When executing script from Java.

Posted by Erick Nelson <er...@gmail.com>.
I'll try from a linux box then... later today. Maybe it has something to do
with running in Eclipse....

On Thu, Jun 18, 2015 at 1:35 AM, Dinko Srkoč <di...@gmail.com> wrote:

> On 17 June 2015 at 18:08, Erick Nelson <er...@gmail.com> wrote:
>
>> Yes, removing the &execute and changing invoke back to...
>>
>> void invoke (String action) {
>>     this."${action}".execute()
>> }
>>
>> does work , from Groovy, but when called from Java ....
>>
>>         File file = new File("src", "TestGroovy.groovy");
>>         Class groovyClass = new GroovyClassLoader().parseClass(file);
>>         GroovyObject groovyObject = (GroovyObject)
>> groovyClass.newInstance();
>>         groovyObject.invokeMethod("invoke", new String [] { "mailSent" });
>>
>> I get an error ...
>>
>> Exception in thread "main" java.lang.NullPointerException: Cannot invoke
>> method execute() on null object. This is thrown from the
>> this."${action}".execute() line.
>>
>> It is only when I use the &execute that it works in both Java and Groovy.
>>
>
> Hmmm. It works on my machine (my favourite line ;-) ):
>
> -----------------------------------------
> $ java -version
> java version "1.7.0_76"
> Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
> Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
> $ cat Bugger.groovy
> import groovy.transform.Field
>
> class Action {
>     String actionName
>     Action(actionName) {
>         this.actionName = actionName
>     }
>     void execute() {
>         println "Doing $actionName"
>     }
> }
>
> @Field formFilled = new Action("Form Filled")
>
> @Field mailSent = new Action("Mail Sent")
>
> def invoke(action) {
>     this."$action".execute()
> }
> $ cat Main.java
> import java.io.File;
> import groovy.lang.GroovyClassLoader;
> import groovy.lang.GroovyObject;
>
> public class Main {
>
>     public static void main (String [] args) {
>         try {
>             File file = new File("Bugger.groovy");
>             Class grClass = new GroovyClassLoader().parseClass(file);
>             GroovyObject groovyInstance = (GroovyObject)
> grClass.newInstance();
>             groovyInstance.invokeMethod("invoke", new String[] {
> "mailSent"});
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>     }
> }
> $ javac -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar Main.java
> $ java -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar:. Main
> Doing Mail Sent
> $
> -----------------------------------------
>
> Cheers,
> Dinko
>
>

Re: No Such Property - When executing script from Java.

Posted by Dinko Srkoč <di...@gmail.com>.
On 17 June 2015 at 18:08, Erick Nelson <er...@gmail.com> wrote:

> Yes, removing the &execute and changing invoke back to...
>
> void invoke (String action) {
>     this."${action}".execute()
> }
>
> does work , from Groovy, but when called from Java ....
>
>         File file = new File("src", "TestGroovy.groovy");
>         Class groovyClass = new GroovyClassLoader().parseClass(file);
>         GroovyObject groovyObject = (GroovyObject)
> groovyClass.newInstance();
>         groovyObject.invokeMethod("invoke", new String [] { "mailSent" });
>
> I get an error ...
>
> Exception in thread "main" java.lang.NullPointerException: Cannot invoke
> method execute() on null object. This is thrown from the
> this."${action}".execute() line.
>
> It is only when I use the &execute that it works in both Java and Groovy.
>

Hmmm. It works on my machine (my favourite line ;-) ):

-----------------------------------------
$ java -version
java version "1.7.0_76"
Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
$ cat Bugger.groovy
import groovy.transform.Field

class Action {
    String actionName
    Action(actionName) {
        this.actionName = actionName
    }
    void execute() {
        println "Doing $actionName"
    }
}

@Field formFilled = new Action("Form Filled")

@Field mailSent = new Action("Mail Sent")

def invoke(action) {
    this."$action".execute()
}
$ cat Main.java
import java.io.File;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

public class Main {

    public static void main (String [] args) {
        try {
            File file = new File("Bugger.groovy");
            Class grClass = new GroovyClassLoader().parseClass(file);
            GroovyObject groovyInstance = (GroovyObject)
grClass.newInstance();
            groovyInstance.invokeMethod("invoke", new String[] {
"mailSent"});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
$ javac -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar Main.java
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.4.3.jar:. Main
Doing Mail Sent
$
-----------------------------------------

Cheers,
Dinko

Re: No Such Property - When executing script from Java.

Posted by Erick Nelson <er...@gmail.com>.
Yes, removing the &execute and changing invoke back to...

void invoke (String action) {
    this."${action}".execute()
}

does work , from Groovy, but when called from Java ....

        File file = new File("src", "TestGroovy.groovy");
        Class groovyClass = new GroovyClassLoader().parseClass(file);
        GroovyObject groovyObject = (GroovyObject)
groovyClass.newInstance();
        groovyObject.invokeMethod("invoke", new String [] { "mailSent" });

I get an error ...

Exception in thread "main" java.lang.NullPointerException: Cannot invoke
method execute() on null object. This is thrown from the
this."${action}".execute() line.

It is only when I use the &execute that it works in both Java and Groovy.

On Wed, Jun 17, 2015 at 1:17 AM, Dinko Srkoč <di...@gmail.com> wrote:

> ups ...
>
> the line "... the `def` of type declaration ..." should be "... the `def`
> or type declaration ..."
>
> So, it's `s/ of / or /`
>
> Sorry about that,
> Dinko
>
> On 17 June 2015 at 10:12, Dinko Srkoč <di...@gmail.com> wrote:
>
>> I think the only required change in the script is to add the `@Field`
>> annotation. The `.&execute` part doesn't seem necessary.
>>
>> It might be worth mentioning that adding the annotation changes how we
>> name things. We're not talking about properties any more, but fields
>> (although the `formFilled` wasn't actually a script's property, but rather
>> a binding, because it lacks the `def` of type declaration prefix).
>>
>> There is another thing that should be fixed in the original code - this
>> time it's the Java snippet:
>>
>>     scriptClass.getDeclaredMethod("invoke", new Class[] {})
>>
>> should be:
>>
>>     scriptClass.getDeclaredMethod("invoke", new Class[] {Object.class})
>>
>> otherwise the `getDeclareMethod` won't be able to find the script's
>> `invoke` method, as the method accepts a parameter.
>>
>> Cheers,
>> Dinko
>>
>>
>> On 17 June 2015 at 08:36, Erick Nelson <er...@gmail.com> wrote:
>>
>>> try this for your groovy script...
>>>
>>> import groovy.transform.Field
>>>
>>> class Action {
>>>     String actionName
>>>     void execute () {
>>>         println "Doing '$actionName'"
>>>     }
>>> }
>>>
>>> @Field formFilled = new Action(actionName: "Form Filled").&execute
>>> @Field mailSent = new Action(actionName: "Mail Sent").&execute
>>>
>>> void invoke (String action) {
>>>     "${action}"()
>>> }
>>>
>>> On Tue, Jun 16, 2015 at 7:35 PM, Owen Rubel <or...@gmail.com> wrote:
>>>
>>>> The 'Bugger' exception is an internationalized error for people in
>>>> Great Britain to let them know they are truly fucked.
>>>>
>>>> Seriously though, in the script you sent, it doesn't look like you
>>>> declared 'formFilled'.
>>>>
>>>> At a minimum, declare it with def:
>>>>
>>>> def formFilled = new Action("Form Filled")
>>>>
>>>>
>>>> Owen Rubel
>>>> 415-971-0976
>>>> orubel@gmail.com
>>>>
>>>> On Tue, Jun 16, 2015 at 6:57 PM, Paul Henry <pa...@futrix.com>
>>>> wrote:
>>>>
>>>>> Hi All,
>>>>>
>>>>> I am trying to run a groovy script from Java and getting the following
>>>>> error
>>>>>
>>>>> <quote>Exception in thread "main"
>>>>> org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No
>>>>> such property: formFilled for class: Bugger</quote>
>>>>>
>>>>> My script looks like this.
>>>>>
>>>>> <quote>
>>>>> class Action {
>>>>>     String actionName
>>>>>     Action(actionName) {
>>>>>         this.actionName = actionName;
>>>>>     }
>>>>>     void execute() {
>>>>>         println "Doing $actionName"
>>>>>     }
>>>>> }
>>>>>
>>>>> formFilled = new Action("Form Filled")
>>>>>
>>>>> mailSent = new Action("Mail Sent")
>>>>>
>>>>> def invoke(action) {
>>>>>     this."$action".execute()
>>>>> }
>>>>>
>>>>> //----- These are runnable when run as a script.
>>>>> formFilled.execute()
>>>>> invoke("mailSent")
>>>>> </quote>
>>>>>
>>>>> If i execute the script directly then the last two lines execute and
>>>>> produce the expected Strings printed to the standard out.
>>>>>
>>>>> <quote>
>>>>> Doing Form Filled
>>>>> Doing Mail Sent
>>>>> </quote>
>>>>>
>>>>> I am trying to run it from Java with something like the following.
>>>>>
>>>>> <quote>
>>>>> Class scriptClass = new GroovyClassLoader().parseClass(new
>>>>> File(Bugger.groovy));
>>>>> Object scriptInstance = scriptClass.newInstance();
>>>>> scriptClass.getDeclaredMethod("invoke", new Class[]
>>>>> {}).invoke(scriptInstance, new Object[] {"formFilled"});
>>>>> </quote>
>>>>>
>>>>> But I keep getting the No Such property exception. Ive looked around
>>>>> for solutions, but either Im missing something so basic its generally
>>>>> understood, or i haven't found the right part of the internet.
>>>>>
>>>>> So Two questions.
>>>>>
>>>>> 1) why is the property not available / hidden. I don't understand why
>>>>> its not available to a method within what I expect is the class.
>>>>>
>>>>> 2) what change do I have to make to my code snippets to get it to work.
>>>>>
>>>>> (Note: I've created a simple example of the problem, rather than post
>>>>> my actual code. I am using groovy to define a DSL for describing actions.
>>>>> We then parse a script in that DSL defining different actions. During
>>>>> operation we want to trigger individual actions to execute on certain
>>>>> occurrences)
>>>>>
>>>>> Cheers
>>>>>
>>>>> -Paul
>>>>>
>>>>>
>>>>>
>>>>> Paul Henry
>>>>> Senior Technical Developer |
>>>>> 79 Boulcott Street, Level 2, Wellington 6011, New Zealand
>>>>> <https://maps.google.com/maps?q=79+Boulcott+Street,+Level+2,+Wellington+6011,+New+Zealand>
>>>>>
>>>>> tel +64 4 499 1327 | mob +64 22 161 8700
>>>>> paul.henry@futrix.com | www.futrix.com | LinkedIn
>>>>> <http://www.linkedin.com/company/futrix-ltd>
>>>>>
>>>>>
>>>>
>>>
>>
>

Re: No Such Property - When executing script from Java.

Posted by Dinko Srkoč <di...@gmail.com>.
ups ...

the line "... the `def` of type declaration ..." should be "... the `def`
or type declaration ..."

So, it's `s/ of / or /`

Sorry about that,
Dinko

On 17 June 2015 at 10:12, Dinko Srkoč <di...@gmail.com> wrote:

> I think the only required change in the script is to add the `@Field`
> annotation. The `.&execute` part doesn't seem necessary.
>
> It might be worth mentioning that adding the annotation changes how we
> name things. We're not talking about properties any more, but fields
> (although the `formFilled` wasn't actually a script's property, but rather
> a binding, because it lacks the `def` of type declaration prefix).
>
> There is another thing that should be fixed in the original code - this
> time it's the Java snippet:
>
>     scriptClass.getDeclaredMethod("invoke", new Class[] {})
>
> should be:
>
>     scriptClass.getDeclaredMethod("invoke", new Class[] {Object.class})
>
> otherwise the `getDeclareMethod` won't be able to find the script's
> `invoke` method, as the method accepts a parameter.
>
> Cheers,
> Dinko
>
>
> On 17 June 2015 at 08:36, Erick Nelson <er...@gmail.com> wrote:
>
>> try this for your groovy script...
>>
>> import groovy.transform.Field
>>
>> class Action {
>>     String actionName
>>     void execute () {
>>         println "Doing '$actionName'"
>>     }
>> }
>>
>> @Field formFilled = new Action(actionName: "Form Filled").&execute
>> @Field mailSent = new Action(actionName: "Mail Sent").&execute
>>
>> void invoke (String action) {
>>     "${action}"()
>> }
>>
>> On Tue, Jun 16, 2015 at 7:35 PM, Owen Rubel <or...@gmail.com> wrote:
>>
>>> The 'Bugger' exception is an internationalized error for people in Great
>>> Britain to let them know they are truly fucked.
>>>
>>> Seriously though, in the script you sent, it doesn't look like you
>>> declared 'formFilled'.
>>>
>>> At a minimum, declare it with def:
>>>
>>> def formFilled = new Action("Form Filled")
>>>
>>>
>>> Owen Rubel
>>> 415-971-0976
>>> orubel@gmail.com
>>>
>>> On Tue, Jun 16, 2015 at 6:57 PM, Paul Henry <pa...@futrix.com>
>>> wrote:
>>>
>>>> Hi All,
>>>>
>>>> I am trying to run a groovy script from Java and getting the following
>>>> error
>>>>
>>>> <quote>Exception in thread "main"
>>>> org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No
>>>> such property: formFilled for class: Bugger</quote>
>>>>
>>>> My script looks like this.
>>>>
>>>> <quote>
>>>> class Action {
>>>>     String actionName
>>>>     Action(actionName) {
>>>>         this.actionName = actionName;
>>>>     }
>>>>     void execute() {
>>>>         println "Doing $actionName"
>>>>     }
>>>> }
>>>>
>>>> formFilled = new Action("Form Filled")
>>>>
>>>> mailSent = new Action("Mail Sent")
>>>>
>>>> def invoke(action) {
>>>>     this."$action".execute()
>>>> }
>>>>
>>>> //----- These are runnable when run as a script.
>>>> formFilled.execute()
>>>> invoke("mailSent")
>>>> </quote>
>>>>
>>>> If i execute the script directly then the last two lines execute and
>>>> produce the expected Strings printed to the standard out.
>>>>
>>>> <quote>
>>>> Doing Form Filled
>>>> Doing Mail Sent
>>>> </quote>
>>>>
>>>> I am trying to run it from Java with something like the following.
>>>>
>>>> <quote>
>>>> Class scriptClass = new GroovyClassLoader().parseClass(new
>>>> File(Bugger.groovy));
>>>> Object scriptInstance = scriptClass.newInstance();
>>>> scriptClass.getDeclaredMethod("invoke", new Class[]
>>>> {}).invoke(scriptInstance, new Object[] {"formFilled"});
>>>> </quote>
>>>>
>>>> But I keep getting the No Such property exception. Ive looked around
>>>> for solutions, but either Im missing something so basic its generally
>>>> understood, or i haven't found the right part of the internet.
>>>>
>>>> So Two questions.
>>>>
>>>> 1) why is the property not available / hidden. I don't understand why
>>>> its not available to a method within what I expect is the class.
>>>>
>>>> 2) what change do I have to make to my code snippets to get it to work.
>>>>
>>>> (Note: I've created a simple example of the problem, rather than post
>>>> my actual code. I am using groovy to define a DSL for describing actions.
>>>> We then parse a script in that DSL defining different actions. During
>>>> operation we want to trigger individual actions to execute on certain
>>>> occurrences)
>>>>
>>>> Cheers
>>>>
>>>> -Paul
>>>>
>>>>
>>>>
>>>> Paul Henry
>>>> Senior Technical Developer |
>>>> 79 Boulcott Street, Level 2, Wellington 6011, New Zealand
>>>> <https://maps.google.com/maps?q=79+Boulcott+Street,+Level+2,+Wellington+6011,+New+Zealand>
>>>>
>>>> tel +64 4 499 1327 | mob +64 22 161 8700
>>>> paul.henry@futrix.com | www.futrix.com | LinkedIn
>>>> <http://www.linkedin.com/company/futrix-ltd>
>>>>
>>>>
>>>
>>
>

Re: No Such Property - When executing script from Java.

Posted by Dinko Srkoč <di...@gmail.com>.
I think the only required change in the script is to add the `@Field`
annotation. The `.&execute` part doesn't seem necessary.

It might be worth mentioning that adding the annotation changes how we name
things. We're not talking about properties any more, but fields (although
the `formFilled` wasn't actually a script's property, but rather a binding,
because it lacks the `def` of type declaration prefix).

There is another thing that should be fixed in the original code - this
time it's the Java snippet:

    scriptClass.getDeclaredMethod("invoke", new Class[] {})

should be:

    scriptClass.getDeclaredMethod("invoke", new Class[] {Object.class})

otherwise the `getDeclareMethod` won't be able to find the script's
`invoke` method, as the method accepts a parameter.

Cheers,
Dinko


On 17 June 2015 at 08:36, Erick Nelson <er...@gmail.com> wrote:

> try this for your groovy script...
>
> import groovy.transform.Field
>
> class Action {
>     String actionName
>     void execute () {
>         println "Doing '$actionName'"
>     }
> }
>
> @Field formFilled = new Action(actionName: "Form Filled").&execute
> @Field mailSent = new Action(actionName: "Mail Sent").&execute
>
> void invoke (String action) {
>     "${action}"()
> }
>
> On Tue, Jun 16, 2015 at 7:35 PM, Owen Rubel <or...@gmail.com> wrote:
>
>> The 'Bugger' exception is an internationalized error for people in Great
>> Britain to let them know they are truly fucked.
>>
>> Seriously though, in the script you sent, it doesn't look like you
>> declared 'formFilled'.
>>
>> At a minimum, declare it with def:
>>
>> def formFilled = new Action("Form Filled")
>>
>>
>> Owen Rubel
>> 415-971-0976
>> orubel@gmail.com
>>
>> On Tue, Jun 16, 2015 at 6:57 PM, Paul Henry <pa...@futrix.com>
>> wrote:
>>
>>> Hi All,
>>>
>>> I am trying to run a groovy script from Java and getting the following
>>> error
>>>
>>> <quote>Exception in thread "main"
>>> org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No
>>> such property: formFilled for class: Bugger</quote>
>>>
>>> My script looks like this.
>>>
>>> <quote>
>>> class Action {
>>>     String actionName
>>>     Action(actionName) {
>>>         this.actionName = actionName;
>>>     }
>>>     void execute() {
>>>         println "Doing $actionName"
>>>     }
>>> }
>>>
>>> formFilled = new Action("Form Filled")
>>>
>>> mailSent = new Action("Mail Sent")
>>>
>>> def invoke(action) {
>>>     this."$action".execute()
>>> }
>>>
>>> //----- These are runnable when run as a script.
>>> formFilled.execute()
>>> invoke("mailSent")
>>> </quote>
>>>
>>> If i execute the script directly then the last two lines execute and
>>> produce the expected Strings printed to the standard out.
>>>
>>> <quote>
>>> Doing Form Filled
>>> Doing Mail Sent
>>> </quote>
>>>
>>> I am trying to run it from Java with something like the following.
>>>
>>> <quote>
>>> Class scriptClass = new GroovyClassLoader().parseClass(new
>>> File(Bugger.groovy));
>>> Object scriptInstance = scriptClass.newInstance();
>>> scriptClass.getDeclaredMethod("invoke", new Class[]
>>> {}).invoke(scriptInstance, new Object[] {"formFilled"});
>>> </quote>
>>>
>>> But I keep getting the No Such property exception. Ive looked around for
>>> solutions, but either Im missing something so basic its generally
>>> understood, or i haven't found the right part of the internet.
>>>
>>> So Two questions.
>>>
>>> 1) why is the property not available / hidden. I don't understand why
>>> its not available to a method within what I expect is the class.
>>>
>>> 2) what change do I have to make to my code snippets to get it to work.
>>>
>>> (Note: I've created a simple example of the problem, rather than post my
>>> actual code. I am using groovy to define a DSL for describing actions. We
>>> then parse a script in that DSL defining different actions. During
>>> operation we want to trigger individual actions to execute on certain
>>> occurrences)
>>>
>>> Cheers
>>>
>>> -Paul
>>>
>>>
>>>
>>> Paul Henry
>>> Senior Technical Developer |
>>> 79 Boulcott Street, Level 2, Wellington 6011, New Zealand
>>> <https://maps.google.com/maps?q=79+Boulcott+Street,+Level+2,+Wellington+6011,+New+Zealand>
>>>
>>> tel +64 4 499 1327 | mob +64 22 161 8700
>>> paul.henry@futrix.com | www.futrix.com | LinkedIn
>>> <http://www.linkedin.com/company/futrix-ltd>
>>>
>>>
>>
>

Re: No Such Property - When executing script from Java.

Posted by Erick Nelson <er...@gmail.com>.
try this for your groovy script...

import groovy.transform.Field

class Action {
    String actionName
    void execute () {
        println "Doing '$actionName'"
    }
}

@Field formFilled = new Action(actionName: "Form Filled").&execute
@Field mailSent = new Action(actionName: "Mail Sent").&execute

void invoke (String action) {
    "${action}"()
}

On Tue, Jun 16, 2015 at 7:35 PM, Owen Rubel <or...@gmail.com> wrote:

> The 'Bugger' exception is an internationalized error for people in Great
> Britain to let them know they are truly fucked.
>
> Seriously though, in the script you sent, it doesn't look like you
> declared 'formFilled'.
>
> At a minimum, declare it with def:
>
> def formFilled = new Action("Form Filled")
>
>
> Owen Rubel
> 415-971-0976
> orubel@gmail.com
>
> On Tue, Jun 16, 2015 at 6:57 PM, Paul Henry <pa...@futrix.com> wrote:
>
>> Hi All,
>>
>> I am trying to run a groovy script from Java and getting the following
>> error
>>
>> <quote>Exception in thread "main"
>> org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No
>> such property: formFilled for class: Bugger</quote>
>>
>> My script looks like this.
>>
>> <quote>
>> class Action {
>>     String actionName
>>     Action(actionName) {
>>         this.actionName = actionName;
>>     }
>>     void execute() {
>>         println "Doing $actionName"
>>     }
>> }
>>
>> formFilled = new Action("Form Filled")
>>
>> mailSent = new Action("Mail Sent")
>>
>> def invoke(action) {
>>     this."$action".execute()
>> }
>>
>> //----- These are runnable when run as a script.
>> formFilled.execute()
>> invoke("mailSent")
>> </quote>
>>
>> If i execute the script directly then the last two lines execute and
>> produce the expected Strings printed to the standard out.
>>
>> <quote>
>> Doing Form Filled
>> Doing Mail Sent
>> </quote>
>>
>> I am trying to run it from Java with something like the following.
>>
>> <quote>
>> Class scriptClass = new GroovyClassLoader().parseClass(new
>> File(Bugger.groovy));
>> Object scriptInstance = scriptClass.newInstance();
>> scriptClass.getDeclaredMethod("invoke", new Class[]
>> {}).invoke(scriptInstance, new Object[] {"formFilled"});
>> </quote>
>>
>> But I keep getting the No Such property exception. Ive looked around for
>> solutions, but either Im missing something so basic its generally
>> understood, or i haven't found the right part of the internet.
>>
>> So Two questions.
>>
>> 1) why is the property not available / hidden. I don't understand why its
>> not available to a method within what I expect is the class.
>>
>> 2) what change do I have to make to my code snippets to get it to work.
>>
>> (Note: I've created a simple example of the problem, rather than post my
>> actual code. I am using groovy to define a DSL for describing actions. We
>> then parse a script in that DSL defining different actions. During
>> operation we want to trigger individual actions to execute on certain
>> occurrences)
>>
>> Cheers
>>
>> -Paul
>>
>>
>>
>> Paul Henry
>> Senior Technical Developer |
>> 79 Boulcott Street, Level 2, Wellington 6011, New Zealand
>> <https://maps.google.com/maps?q=79+Boulcott+Street,+Level+2,+Wellington+6011,+New+Zealand>
>>
>> tel +64 4 499 1327 | mob +64 22 161 8700
>> paul.henry@futrix.com | www.futrix.com | LinkedIn
>> <http://www.linkedin.com/company/futrix-ltd>
>>
>>
>

Re: No Such Property - When executing script from Java.

Posted by Owen Rubel <or...@gmail.com>.
The 'Bugger' exception is an internationalized error for people in Great
Britain to let them know they are truly fucked.

Seriously though, in the script you sent, it doesn't look like you declared
'formFilled'.

At a minimum, declare it with def:

def formFilled = new Action("Form Filled")


Owen Rubel
415-971-0976
orubel@gmail.com

On Tue, Jun 16, 2015 at 6:57 PM, Paul Henry <pa...@futrix.com> wrote:

> Hi All,
>
> I am trying to run a groovy script from Java and getting the following
> error
>
> <quote>Exception in thread "main"
> org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No
> such property: formFilled for class: Bugger</quote>
>
> My script looks like this.
>
> <quote>
> class Action {
>     String actionName
>     Action(actionName) {
>         this.actionName = actionName;
>     }
>     void execute() {
>         println "Doing $actionName"
>     }
> }
>
> formFilled = new Action("Form Filled")
>
> mailSent = new Action("Mail Sent")
>
> def invoke(action) {
>     this."$action".execute()
> }
>
> //----- These are runnable when run as a script.
> formFilled.execute()
> invoke("mailSent")
> </quote>
>
> If i execute the script directly then the last two lines execute and
> produce the expected Strings printed to the standard out.
>
> <quote>
> Doing Form Filled
> Doing Mail Sent
> </quote>
>
> I am trying to run it from Java with something like the following.
>
> <quote>
> Class scriptClass = new GroovyClassLoader().parseClass(new
> File(Bugger.groovy));
> Object scriptInstance = scriptClass.newInstance();
> scriptClass.getDeclaredMethod("invoke", new Class[]
> {}).invoke(scriptInstance, new Object[] {"formFilled"});
> </quote>
>
> But I keep getting the No Such property exception. Ive looked around for
> solutions, but either Im missing something so basic its generally
> understood, or i haven't found the right part of the internet.
>
> So Two questions.
>
> 1) why is the property not available / hidden. I don't understand why its
> not available to a method within what I expect is the class.
>
> 2) what change do I have to make to my code snippets to get it to work.
>
> (Note: I've created a simple example of the problem, rather than post my
> actual code. I am using groovy to define a DSL for describing actions. We
> then parse a script in that DSL defining different actions. During
> operation we want to trigger individual actions to execute on certain
> occurrences)
>
> Cheers
>
> -Paul
>
>
>
> Paul Henry
> Senior Technical Developer |
> 79 Boulcott Street, Level 2, Wellington 6011, New Zealand
> <https://maps.google.com/maps?q=79+Boulcott+Street,+Level+2,+Wellington+6011,+New+Zealand>
>
> tel +64 4 499 1327 | mob +64 22 161 8700
> paul.henry@futrix.com | www.futrix.com | LinkedIn
> <http://www.linkedin.com/company/futrix-ltd>
>
>