Reputation: 5387
I'm working for the first time with Groovy
. I have a simple script, that I want to run on my Java server.
I build the script here MyScript in the Groovy web console, and when I run it, it returns what I expect [1,10]
.
minValue=1;
maxValue=10;
listValues=[];
enumMap=[];
rangeType="RANGE"; //RANGE,LIST,ENUM,NONE
Object test(int arg){
return getRange();
}
Object[] getRange(){
if (rangeType=="NONE"){
return [];
}
if (rangeType=="RANGE"){
return [minValue,maxValue];
}
if (rangeType=="LIST"){
return listValues;
}
if (rangeType=="ENUM"){
return enumMap;
}
}
println(test(1));
On my Java server I invoke the test
method this way
Script groovyScript = new GroovyShell().parse(script);
return groovyScript.invokeMethod("test", valueSuccess);
Although the script runs fine in the web console, when I run it on my server it gives me the following exception:
groovy.lang.MissingPropertyException: No such property: rangeType for class: Script1
The exact same script once runs without a problem, once it throws an exception. How can that be? It's not even complex, and the variable declarations should be correct, aren't they?
Upvotes: 1
Views: 124
Reputation: 2678
I would like you to import
the field annotation
package and correct the decalartion of variables.Specify some datatype
for them along with the @Field annotation
to access the variable anywhere in the script.
import groovy.transform.Field
@Field int minValue = 1;
@Field int maxValue = 10;
@Field List listValues= [];
@Field Map enumMap = [:];
@Field def rangeType = "RANGE"; //RANGE,LIST,ENUM,NONE
reference link for creating and accessing the global variables in Groovy
Upvotes: 1