Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: 1.6
-
Fix Version/s: None
-
Component/s: class generator
-
Labels:None
-
Environment:XP or linux. Reproduces with a simple java program using embeddable groovy.
-
Testcase included:yes
-
Number of attachments :
Description
If a script is simply "Category" where the binding has capability to resolve a variable of that name, Groovy returns "interface groovy.lang.Category" instead of the value of the variable "Category" from the given binding object. Here's a java code that reproduces this issue:
import groovy.lang.Binding; import groovy.lang.GroovyShell; import groovy.lang.Script; public class testCategory { public static void main(String[] args) { Binding binding = new Binding() { public Object getVariable(String key) { if (key.equals("Category")) { return new Long(210); } return super.getVariable(key); } }; GroovyShell shell = new GroovyShell(Thread.currentThread().getContextClassLoader()); String expr = "Category"; Script scr = shell.parse(expr); System.out.println(expr); scr.setBinding(binding); Object value = scr.run(); System.out.println(value); } }
The output of the above code is expected to be:
Category
210
instead, what we get is:
Category
interface groovy.lang.Category
since in Groovy class names and variables share a common namespace and since the tight integration with the VM requires us to know class names at compile time, we have to define rules when a vanilla name is a class and when not. So we defined that at compile time a vanilla name that does not refer to a local variable or field is first looked up as a class and if that fails the name is seen as dynamic property. Variables in the binding are here to be seen as dynamic properties. So if the class is preferred over the property it is no error. Now in case of a script there is an alternative syntax you can use. You could do this.Category, binding.Category or this.binding.Category. In case of classes there is no alternative syntax, so changing the static lookup order does not make sense.