public Object invokeMethod(String name, Object obj) { if (obj instanceof Object[]) { final Object[] args = (Object[]) obj; if (args.length == 1 && args[0] instanceof Closure) { registerStaticMethod(name, (Closure) args[0]); return null; } } throw new MissingMethodException(name, getClass(), obj instanceof Object[] ? (Object[]) obj : new Object[]{obj}); } }
/** * {@link GroovyObject#invokeMethod(String, Object)} path as fallback. * This method is called by the handle as exception handler in case the * selected method causes a MissingMethodExecutionFailed, where * we will just give through the exception, and a normal * MissingMethodException where we call {@link GroovyObject#invokeMethod(String, Object)} * if receiver class, the type transported by the exception and the name * for the method stored in the exception and our current method name * are equal. * Should those conditions not apply we just rethrow the exception. */ public static Object invokeGroovyObjectInvoker(MissingMethodException e, Object receiver, String name, Object[] args) { if (e instanceof MissingMethodExecutionFailed) { throw (MissingMethodException)e.getCause(); } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) { //TODO: we should consider calling this one directly for MetaClassImpl, // then we save the new method selection // in case there's nothing else, invoke the object's own invokeMethod() return ((GroovyObject)receiver).invokeMethod(name, args); } else { throw e; } }
if (methodMissing instanceof ClosureMetaMethod && iie.getCause() instanceof MissingMethodException) { MissingMethodException mme = (MissingMethodException) iie.getCause(); throw new MissingMethodExecutionFailed(mme.getMethod(), mme.getClass(), mme.getArguments(),mme.isStatic(),mme); throw new MissingMethodExecutionFailed(mme.getMethod(), mme.getClass(), mme.getArguments(),mme.isStatic(),mme); else throw mme;
protected Object doCall(Object[] args) { String methodName = String.valueOf(args[0]); for (Closure methodMissingDelegate : methodMissingDelegates) { try { return methodMissingDelegate.call(args); } catch (MissingMethodException mme) { if (!methodName.equals(mme.getMethod())) throw mme; } } if (originalMethodMissingDelegate != null) { try { return originalMethodMissingDelegate.call(args); } catch (MissingMethodException mme) { if (!methodName.equals(mme.getMethod())) throw mme; } } Object[] argsCopy = new Object[args.length - 1]; System.arraycopy(args, 1, argsCopy, 0, argsCopy.length); throw new MissingMethodException(methodName, CompositeBuilder.class, argsCopy); } });
/** * Groovy override to pass things off to the delegate. * @param name The name of the method. * @param args The method arguments. * @return The return value of the method. */ public Object methodMissing(String name, Object args) { try { return InvokerHelper.invokeMethod(getDelegate(), name, args); } catch (MissingMethodException mme) { throw new MissingMethodException(name, getClass(), mme.getArguments()); } }
@Override protected void clearError() { for (PrintStream stream : delegates) { try { GroovyRuntimeUtil.invokeMethod(stream, "clearError"); } catch (MissingMethodException e) { // method doesn't exist in JDK 1.5 if (!"clearError".equals(e.getMethod())) { throw e; } } } }
@Override protected DynamicInvokeResult invokeOpaqueMethod(MetaClass metaClass, String name, Object[] arguments) { try { try { return DynamicInvokeResult.found(groovyObject.invokeMethod(name, arguments)); } catch (InvokerInvocationException e) { if (e.getCause() instanceof RuntimeException) { throw (RuntimeException) e.getCause(); } throw e; } } catch (MissingMethodException e) { if (!e.getMethod().equals(name) || !Arrays.equals(e.getArguments(), arguments)) { throw e; } // Else, ignore } return DynamicInvokeResult.notFound(); } }
private 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 (!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)); } throw e; } }
private Object invokeImpl(final Object thiz, final String name, final Object args[]) throws ScriptException, NoSuchMethodException { if (name == null) { throw new NullPointerException("Method name can not be null"); } try { if (thiz != null) { return InvokerHelper.invokeMethod(thiz, name, args); } } catch (MissingMethodException mme) { throw new NoSuchMethodException(mme.getMessage()); } catch (Exception e) { throw new ScriptException(e); } return callGlobal(name, args); }
/** * Groovy override to pass things off to the delegate. * @param name The name of the method. * @param args The method arguments. * @return The return value of the method. */ public Object methodMissing(String name, Object args) { try { return InvokerHelper.invokeMethod(getDelegate(), name, args); } catch (MissingMethodException mme) { throw new MissingMethodException(name, getClass(), mme.getArguments()); } }
/** * This method is the workhorse of the builder. * * @param methodName the name of the method being invoked * @param name the name of the node * @param args the arguments passed into the node * @return the object from the factory */ private Object doInvokeMethod(String methodName, Object name, Object args) { Reference explicitResult = new Reference(); if (checkExplicitMethod(methodName, args, explicitResult)) { return explicitResult.get(); } else { try { return dispatchNodeCall(name, args); } catch(MissingMethodException mme) { if(mme.getMethod().equals(methodName) && methodMissingDelegate != null) { return methodMissingDelegate.call(methodName, args); } throw mme; } } }
protected DynamicInvokeResult invokeOpaqueMethod(MetaClass metaClass, String name, Object[] arguments) { MetaMethod methodMissingMethod = findMethodMissingMethod(metaClass); if (methodMissingMethod != null) { try { try { return DynamicInvokeResult.found(methodMissingMethod.invoke(bean, new Object[] {name, arguments})); } catch (InvokerInvocationException e) { if (e.getCause() instanceof MissingMethodException) { throw (MissingMethodException) e.getCause(); } throw e; } } catch (MissingMethodException e) { if (!e.getMethod().equals(name) || !Arrays.equals(e.getArguments(), arguments)) { throw e; } // Ignore } } return DynamicInvokeResult.notFound(); } }
public void execute(T delegate) { if (closure == null) { return; } try { if (configureableAware && delegate instanceof Configurable) { ((Configurable) delegate).configure(closure); } else { Closure copy = (Closure) closure.clone(); copy.setResolveStrategy(resolveStrategy); copy.setDelegate(delegate); if (copy.getMaximumNumberOfParameters() == 0) { copy.call(); } else { copy.call(delegate); } } } catch (groovy.lang.MissingMethodException e) { if (Objects.equal(e.getType(), closure.getClass()) && Objects.equal(e.getMethod(), "doCall")) { throw new InvalidActionClosureException(closure, delegate); } throw e; } }
private Object invokeImpl(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { if (name == null) { throw new NullPointerException("method name is null"); } try { if (thiz != null) { return InvokerHelper.invokeMethod(thiz, name, args); } else { return callGlobal(name, args); } } catch (MissingMethodException mme) { throw new NoSuchMethodException(mme.getMessage()); } catch (Exception e) { throw new ScriptException(e); } }
public Object invokeMethod(String name, Object obj) { if (obj instanceof Object[]) { Object args[] = (Object[]) obj; if (args.length == 1 && args[0] instanceof Closure) { registerSubclassInstanceMethod(name, klazz, (Closure) args[0]); return null; } } throw new MissingMethodException(name, getClass(), new Object[]{obj}); } }
public final Object callCurrent(GroovyObject receiver, Object[] args) throws Throwable { if (checkCall(receiver)) { try { try { return metaClass.invokeMethod(array.owner, receiver, name, args, false, true); } catch (MissingMethodException e) { if (e instanceof MissingMethodExecutionFailed) { throw (MissingMethodException)e.getCause(); } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) { // in case there's nothing else, invoke the object's own invokeMethod() return ((GroovyObject)receiver).invokeMethod(name, args); } else { throw e; } } } catch (GroovyRuntimeException gre) { throw ScriptBytecodeAdapter.unwrap(gre); } } else { return CallSiteArray.defaultCallCurrent(this, receiver, args); } } }
private Object invokeMissingMethod(Object instance, String methodName, Object[] arguments, RuntimeException original) { if (methodMissing != null) { try { return methodMissing.invoke(instance, new Object[]{methodName, arguments}); } catch (InvokerInvocationException iie) { if (methodMissing instanceof ClosureMetaMethod && iie.getCause() instanceof MissingMethodException) { MissingMethodException mme = (MissingMethodException) iie.getCause(); throw new MissingMethodExecutionFailed (mme.getMethod(), mme.getClass(), mme.getArguments(),mme.isStatic(),mme); } throw iie; } } else if (original != null) throw original; else throw new MissingMethodExceptionNoStack(methodName, theClass, arguments, false); }
/** * Invoke a method (or closure in the binding) defined. * * @param name method to call * @param args arguments to pass to the method * @return value */ public Object invokeMethod(String name, Object args) { try { return super.invokeMethod(name, args); } // if the method was not found in the current scope (the script's methods) // let's try to see if there's a method closure with the same name in the binding catch (MissingMethodException mme) { try { if (name.equals(mme.getMethod())) { Object boundClosure = getProperty(name); if (boundClosure instanceof Closure) { return ((Closure) boundClosure).call((Object[])args); } else { throw mme; } } else { throw mme; } } catch (MissingPropertyException mpe) { throw mme; } } }
private Object invokeImpl(final Object thiz, final String name, final Object args[]) throws ScriptException, NoSuchMethodException { if (name == null) { throw new NullPointerException("Method name can not be null"); } try { if (thiz != null) { return InvokerHelper.invokeMethod(thiz, name, args); } } catch (MissingMethodException mme) { throw new NoSuchMethodException(mme.getMessage()); } catch (Exception e) { throw new ScriptException(e); } return callGlobal(name, args); }
private Object callGlobal(String name, Object[] args, ScriptContext ctx) { Closure<?> closure = globalClosures.get(name); if (closure != null) { return closure.call(args); } else { // Look for closure valued variable in the // given ScriptContext. If available, call it. Object value = ctx.getAttribute(name); if (value instanceof Closure) { return ((Closure) value).call(args); } // else fall thru.. } throw new MissingMethodException(name, getClass(), args); }