Hello all,
I'm attempting to directly embed groovy in my application but I am new to this and having some difficulty understanding how scoping works in Groovy compiled classes. I've already embedded Rhino with very few problems but I want to give my users their choice of scripting options (vanilla JSR 223 based interfacing is not a good fit).
First, I wish to create a set of functions that can be accessed anywhere in the code via a static member variable JC.* , next I want to submit a series of scripting expressions ad-hoc that can use the exposed functions.
MyFunctions.groovy (limited to one method for this example):
public class CommonFunctions {
/** HOW DO I GET THE TOP LEVEL BINDING OBJECT FROM A SUBCLASS ? */
def variableExists(variableName) { return getBinding().getVariables().get(variableName) != null;}
}
Now I create a class on this script and add it to to my bindings object:
import java.io.File;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.GroovyShell;
public class Test {
public static void main(String[] args) {
try {
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
ClassLoader parent = Test.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File("D:/CommonFunctions.groovy"));
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
binding.setVariable("CF", groovyObject);
shell.evaluate("aa =5;");
shell.evaluate("bDoesAAVariableExist = CF.variableExists(aa);");
shell.evaluate("if (bDoesAAVariableExist) {System.out.println('aa exists');} else { System.out.println('aa not exists'); }");
} catch (Exception e) {
e.printStackTrace();
}
}
}
The following exception is thrown
groovy.lang.MissingMethodException: No signature of method: CommonFunctions.getBinding() is applicable for argument types: () values: []
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:143)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:147)
at CommonFunctions.variableExists(CommonFunctions.groovy:3)
at CommonFunctions$variableExists.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at Script2.run(Script2.groovy:1)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:561)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:536)
at Test.main(Test.java:18)
So, if I wish to compile a groovy script that will have access to the top level bindings, how to I access the top level bindings from my sub-class?
Additionally, is there an equivilent of the 'with' statement in Groovy that would let my subclass call functions and closures from the top level 'Binding' with a prefix, if not, what is the prefix of to use to reference variables/functions/closures/etc that have been added to the top level 'binding'?
Chris.
test.zip
View this message in context:
Newbie problems: How to get top level Binding object from a compiled groovy class?
Sent from the
groovy - user mailing list archive at Nabble.com.