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č <dinko.srkoc@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č <dinko.srkoc@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 <ericksn@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 <orubel@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")



On Tue, Jun 16, 2015 at 6:57 PM, Paul Henry <paul.henry@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