[groovy-user] Newbie problems: How to get top level Binding object from a compiled groovy class?

289 views
Skip to first unread message

Takapa

unread,
Aug 22, 2009, 3:22:43 AM8/22/09
to us...@groovy.codehaus.org
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.

Roshan Dawrani

unread,
Aug 22, 2009, 11:26:14 AM8/22/09
to us...@groovy.codehaus.org
Normal groovy classes don't have any binding. Groovy scripts have.

Even without your java code that is integrating with your groovy class, if you first test the following groovy class alone, you will find it fails with the same error:

public class CommonFunctions {
Reply all
Reply to author
Forward
0 new messages