use of groovy.lang.MissingMethodException in project gradle by gradle.
the class ExtensibleDynamicObjectTest method canCallGroovyDynamicMethods.
@Test
public void canCallGroovyDynamicMethods() {
DynamicGroovyBean bean = new DynamicGroovyBean();
DynamicObject object = new ExtensibleDynamicObject(bean, DynamicGroovyBean.class, ThreadGlobalInstantiator.getOrCreate());
Integer doubled = (Integer) object.invokeMethod("bar", 1);
assertThat(doubled, equalTo(2));
try {
object.invokeMethod("xxx", 1, 2, 3);
fail();
} catch (MissingMethodException e) {
assertThat(e.getMessage(), equalTo("Could not find method xxx() for arguments [1, 2, 3] on object of type " + DynamicGroovyBean.class.getName() + "."));
}
}
use of groovy.lang.MissingMethodException in project groovy-core by groovy.
the class GroovyScriptEngineImpl method eval.
// package-privates
Object eval(Class scriptClass, final ScriptContext ctx) throws ScriptException {
// Only initialize once.
if (null == ctx.getAttribute("context", ScriptContext.ENGINE_SCOPE)) {
// add context to bindings
ctx.setAttribute("context", ctx, ScriptContext.ENGINE_SCOPE);
// direct output to ctx.getWriter
// If we're wrapping with a PrintWriter here,
// enable autoFlush because otherwise it might not get done!
final Writer writer = ctx.getWriter();
ctx.setAttribute("out", (writer instanceof PrintWriter) ? writer : new PrintWriter(writer, true), ScriptContext.ENGINE_SCOPE);
// Not going to do this after all (at least for now).
// Scripts can use context.{reader, writer, errorWriter}.
// That is a modern version of System.{in, out, err} or Console.{reader, writer}().
//
// // New I/O names consistent with ScriptContext and java.io.Console.
//
// ctx.setAttribute("writer", writer, ScriptContext.ENGINE_SCOPE);
//
// // Direct errors to ctx.getErrorWriter
// final Writer errorWriter = ctx.getErrorWriter();
// ctx.setAttribute("errorWriter", (errorWriter instanceof PrintWriter) ?
// errorWriter :
// new PrintWriter(errorWriter),
// ScriptContext.ENGINE_SCOPE);
//
// // Get input from ctx.getReader
// // We don't wrap with BufferedReader here because we expect that if
// // the host wants that they do it. Either way Groovy scripts will
// // always have readLine because the GDK supplies it for Reader.
// ctx.setAttribute("reader", ctx.getReader(), ScriptContext.ENGINE_SCOPE);
}
// Fix for GROOVY-3669: Can't use several times the same JSR-223 ScriptContext for differents groovy script
if (ctx.getWriter() != null) {
ctx.setAttribute("out", new PrintWriter(ctx.getWriter(), true), ScriptContext.ENGINE_SCOPE);
}
/*
* We use the following Binding instance so that global variable lookup
* will be done in the current ScriptContext instance.
*/
Binding binding = new Binding(ctx.getBindings(ScriptContext.ENGINE_SCOPE)) {
@Override
public Object getVariable(String name) {
synchronized (ctx) {
int scope = ctx.getAttributesScope(name);
if (scope != -1) {
return ctx.getAttribute(name, scope);
}
}
throw new MissingPropertyException(name, getClass());
}
@Override
public void setVariable(String name, Object value) {
synchronized (ctx) {
int scope = ctx.getAttributesScope(name);
if (scope == -1) {
scope = ScriptContext.ENGINE_SCOPE;
}
ctx.setAttribute(name, value, scope);
}
}
};
try {
// then simply return that class
if (!Script.class.isAssignableFrom(scriptClass)) {
return scriptClass;
} else {
// it's a script
Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
// save all current closures into global closures map
Method[] methods = scriptClass.getMethods();
for (Method m : methods) {
String name = m.getName();
globalClosures.put(name, new MethodClosure(scriptObject, name));
}
MetaClass oldMetaClass = scriptObject.getMetaClass();
/*
* We override the MetaClass of this script object so that we can
* forward calls to global closures (of previous or future "eval" calls)
* This gives the illusion of working on the same "global" scope.
*/
scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {
@Override
public Object invokeMethod(Object object, String name, Object args) {
if (args == null) {
return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
}
if (args instanceof Tuple) {
return invokeMethod(object, name, ((Tuple) args).toArray());
}
if (args instanceof Object[]) {
return invokeMethod(object, name, (Object[]) args);
} else {
return invokeMethod(object, name, new Object[] { args });
}
}
@Override
public Object invokeMethod(Object object, String name, Object[] args) {
try {
return super.invokeMethod(object, name, args);
} catch (MissingMethodException mme) {
return callGlobal(name, args, ctx);
}
}
@Override
public Object invokeStaticMethod(Object object, String name, Object[] args) {
try {
return super.invokeStaticMethod(object, name, args);
} catch (MissingMethodException mme) {
return callGlobal(name, args, ctx);
}
}
});
return scriptObject.run();
}
} catch (Exception e) {
throw new ScriptException(e);
} finally {
// Fix for GROOVY-3669: Can't use several times the same JSR-223 ScriptContext for different groovy script
// Groovy's scripting engine implementation adds those two variables in the binding
// but should clean up afterwards
ctx.removeAttribute("context", ScriptContext.ENGINE_SCOPE);
ctx.removeAttribute("out", ScriptContext.ENGINE_SCOPE);
}
}
use of groovy.lang.MissingMethodException in project gremlin by tinkerpop.
the class GremlinGroovyScriptEngine method callGlobal.
private Object callGlobal(final String name, final Object[] args, final ScriptContext ctx) {
Closure closure = globalClosures.get(name);
if (closure != null) {
return closure.call(args);
}
Object value = ctx.getAttribute(name);
if (value instanceof Closure) {
return ((Closure) value).call(args);
} else {
throw new MissingMethodException(name, getClass(), args);
}
}
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);
}
}
use of groovy.lang.MissingMethodException in project groovy by apache.
the class InvokerHelper method invokePogoMethod.
static Object invokePogoMethod(Object object, String methodName, Object arguments) {
GroovyObject groovy = (GroovyObject) object;
boolean intercepting = groovy instanceof GroovyInterceptable;
try {
// if it's a pure interceptable object (even intercepting toString(), clone(), ...)
if (intercepting) {
return groovy.invokeMethod(methodName, asUnwrappedArray(arguments));
}
//else try a statically typed method or a GDK method
return groovy.getMetaClass().invokeMethod(object, methodName, asArray(arguments));
} catch (MissingMethodException e) {
if (e instanceof MissingMethodExecutionFailed) {
throw (MissingMethodException) e.getCause();
} else if (!intercepting && e.getMethod().equals(methodName) && object.getClass() == e.getType()) {
// in case there's nothing else, invoke the object's own invokeMethod()
return groovy.invokeMethod(methodName, asUnwrappedArray(arguments));
} else {
throw e;
}
}
}
Aggregations