use of groovy.lang.MissingMethodException in project gremlin by tinkerpop.
the class GremlinGroovyScriptEngine method eval.
Object eval(final Class scriptClass, final ScriptContext context) throws ScriptException {
this.checkClearCache();
context.setAttribute("context", context, ScriptContext.ENGINE_SCOPE);
java.io.Writer writer = context.getWriter();
context.setAttribute("out", writer instanceof PrintWriter ? writer : new PrintWriter(writer), ScriptContext.ENGINE_SCOPE);
Binding binding = new Binding() {
public Object getVariable(String name) {
synchronized (context) {
int scope = context.getAttributesScope(name);
if (scope != -1) {
return context.getAttribute(name, scope);
}
throw new MissingPropertyException(name, getClass());
}
}
public void setVariable(String name, Object value) {
synchronized (context) {
int scope = context.getAttributesScope(name);
if (scope == -1) {
scope = ScriptContext.ENGINE_SCOPE;
}
context.setAttribute(name, value, scope);
}
}
};
try {
Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
Method[] methods = scriptClass.getMethods();
Map<String, MethodClosure> closures = new HashMap<String, MethodClosure>();
for (Method m : methods) {
String name = m.getName();
closures.put(name, new MethodClosure(scriptObject, name));
}
globalClosures.putAll(closures);
final MetaClass oldMetaClass = scriptObject.getMetaClass();
scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {
public Object invokeMethod(Object object, String name, Object args) {
if (args == null) {
return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
} else if (args instanceof Tuple) {
return invokeMethod(object, name, ((Tuple) args).toArray());
} else if (args instanceof Object[]) {
return invokeMethod(object, name, (Object[]) args);
} else {
return invokeMethod(object, name, new Object[] { args });
}
}
public Object invokeMethod(Object object, String name, Object[] args) {
try {
return super.invokeMethod(object, name, args);
} catch (MissingMethodException mme) {
return callGlobal(name, args, context);
}
}
public Object invokeStaticMethod(Object object, String name, Object[] args) {
try {
return super.invokeStaticMethod(object, name, args);
} catch (MissingMethodException mme) {
return callGlobal(name, args, context);
}
}
});
return scriptObject.run();
} catch (Exception e) {
throw new ScriptException(e);
}
}
Aggregations