Java Code Examples for groovy.lang.Closure#getMaximumNumberOfParameters()
The following examples show how to use
groovy.lang.Closure#getMaximumNumberOfParameters() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClosureBackedAction.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
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; } }
Example 2
Source File: ClosureBackedAction.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
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 (e.getType().equals(closure.getClass()) && e.getMethod().equals("doCall")) { throw new InvalidActionClosureException(closure, delegate); } } }
Example 3
Source File: DefaultJsonGenerator.java From groovy with Apache License 2.0 | 6 votes |
protected ClosureConverter(Class<?> type, Closure<?> closure) { if (type == null) { throw new NullPointerException("Type parameter must not be null"); } if (closure == null) { throw new NullPointerException("Closure parameter must not be null"); } int paramCount = closure.getMaximumNumberOfParameters(); if (paramCount < 1) { throw new IllegalArgumentException("Closure must accept at least one parameter"); } Class<?> param1 = closure.getParameterTypes()[0]; if (!param1.isAssignableFrom(type)) { throw new IllegalArgumentException("Expected first parameter to be of type: " + type.toString()); } if (paramCount > 1) { Class<?> param2 = closure.getParameterTypes()[1]; if (!param2.isAssignableFrom(String.class)) { throw new IllegalArgumentException("Expected second parameter to be of type: " + String.class.toString()); } } this.type = type; this.closure = closure; this.paramCount = paramCount; }
Example 4
Source File: ClosureBackedAction.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
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; } }
Example 5
Source File: ClosureBackedAction.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
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 (e.getType().equals(closure.getClass()) && e.getMethod().equals("doCall")) { throw new InvalidActionClosureException(closure, delegate); } } }
Example 6
Source File: ClosureFunction.java From http-builder-ng with Apache License 2.0 | 5 votes |
public ClosureFunction(final Closure<OUT> closure) { this.closure = closure; this.size = closure.getMaximumNumberOfParameters(); if(size != 1) { throw new IllegalArgumentException("Closure needs to accept a single argument"); } }
Example 7
Source File: JmxBuilderModelMBean.java From groovy with Apache License 2.0 | 5 votes |
public void handleNotification(Notification notification, Object handback) { AttributeChangeNotification note = (AttributeChangeNotification) notification; Map event = (Map) handback; if (event != null) { Object del = event.get("managedObject"); Object callback = event.get("callback"); if (callback != null && callback instanceof Closure) { Closure closure = (Closure) callback; closure.setDelegate(del); if (closure.getMaximumNumberOfParameters() == 1) closure.call(buildAttributeNotificationPacket(note)); else closure.call(); } } }
Example 8
Source File: JmxEventListener.java From groovy with Apache License 2.0 | 5 votes |
/** * This is the implemented method for NotificationListener. It is called by an event emitter to dispatch * JMX events to listeners. Here it handles internal JmxBuilder events. * * @param notification the notification object passed to closure used to handle JmxBuilder events. * @param handback - In this case, the handback is the closure to execute when the event is handled. */ public void handleNotification(Notification notification, Object handback) { Map event = (Map) handback; if (event != null) { Object del = event.get("managedObject"); Object callback = event.get("callback"); if (callback != null && callback instanceof Closure) { Closure closure = (Closure) callback; closure.setDelegate(del); if (closure.getMaximumNumberOfParameters() == 1) closure.call(buildOperationNotificationPacket(notification)); else closure.call(); } } }
Example 9
Source File: Memoize.java From groovy with Apache License 2.0 | 5 votes |
MemoizeFunction(final MemoizeCache<Object, ?> cache, Closure<V> closure) { super(closure.getOwner()); this.cache = coerce(cache); this.closure = closure; parameterTypes = closure.getParameterTypes(); maximumNumberOfParameters = closure.getMaximumNumberOfParameters(); }
Example 10
Source File: CurriedClosure.java From groovy with Apache License 2.0 | 5 votes |
/** * Creates the curried closure. * * @param index the position where the parameters should be injected (-ve for lazy) * @param uncurriedClosure the closure to be called after the curried parameters are injected * @param arguments the supplied parameters */ public CurriedClosure(int index, Closure<V> uncurriedClosure, Object... arguments) { super(uncurriedClosure.clone()); curriedParams = arguments; this.index = index; final int origMaxLen = uncurriedClosure.getMaximumNumberOfParameters(); maximumNumberOfParameters = origMaxLen - arguments.length; Class[] classes = uncurriedClosure.getParameterTypes(); Class lastType = classes.length == 0 ? null : classes[classes.length-1]; if (lastType != null && lastType.isArray()) { varargType = lastType; } if (!isVararg()) { // perform some early param checking for non-vararg case if (index < 0) { // normalise this.index += origMaxLen; minParamsExpected = 0; } else { minParamsExpected = index + arguments.length; } if (maximumNumberOfParameters < 0) { throw new IllegalArgumentException("Can't curry " + arguments.length + " arguments for a closure with " + origMaxLen + " parameters."); } if (index < 0) { if (index < -origMaxLen || index > -arguments.length) throw new IllegalArgumentException("To curry " + arguments.length + " argument(s) expect index range " + (-origMaxLen) + ".." + (-arguments.length) + " but found " + index); } else if (index > maximumNumberOfParameters) { throw new IllegalArgumentException("To curry " + arguments.length + " argument(s) expect index range 0.." + maximumNumberOfParameters + " but found " + index); } } else { minParamsExpected = 0; } }
Example 11
Source File: ClosureUtils.java From groovy with Apache License 2.0 | 4 votes |
/** * Does the Closure have a single char-like (char or Character) argument. * @param c a Closure * @return true if it has exactly one argument and the type is char or Character */ public static boolean hasSingleCharacterArg(Closure c) { if (c.getMaximumNumberOfParameters() != 1) return false; String typeName = c.getParameterTypes()[0].getName(); return typeName.equals("char") || typeName.equals("java.lang.Character"); }
Example 12
Source File: ClosureUtils.java From groovy with Apache License 2.0 | 4 votes |
/** * Does the Closure have a single String argument. * @param c a Closure * @return true if it has exactly one argument and the type is String */ public static boolean hasSingleStringArg(Closure c) { if (c.getMaximumNumberOfParameters() != 1) return false; String typeName = c.getParameterTypes()[0].getName(); return typeName.equals("java.lang.String"); }
Example 13
Source File: BooleanClosureWrapper.java From groovy with Apache License 2.0 | 4 votes |
public BooleanClosureWrapper(Closure wrapped) { this.wrapped = wrapped; this.bmi = new BooleanReturningMethodInvoker("call"); numberOfArguments = wrapped.getMaximumNumberOfParameters(); }
Example 14
Source File: ComposedClosure.java From groovy with Apache License 2.0 | 4 votes |
public ComposedClosure(Closure first, Closure<V> second) { super(first.clone()); this.first = (Closure) getOwner(); this.second = (Closure<V>) second.clone(); maximumNumberOfParameters = first.getMaximumNumberOfParameters(); }
Example 15
Source File: Node.java From groovy with Apache License 2.0 | 4 votes |
private static <T> T callClosureForNode(Closure<T> closure, Object node, int level) { if (closure.getMaximumNumberOfParameters() == 2) { return closure.call(node, level); } return closure.call(node); }