/** * Sends a message and execute continuation when reply became available. * * @param message message to send * @param closure closure to execute when reply became available * @return The message that came in reply to the original send. */ @SuppressWarnings({"AssignmentToMethodParameter"}) public final <T> MessageStream sendAndContinue(final T message, Closure closure) { closure = (Closure) closure.clone(); closure.setDelegate(this); closure.setResolveStrategy(Closure.DELEGATE_FIRST); return send(message, new DataCallback(closure, parallelGroup)); }
public void setProperty(String property, Object newValue) { if ("delegate".equals(property)) { setDelegate(newValue); } else if ("metaClass".equals(property)) { setMetaClass((MetaClass) newValue); } else if ("resolveStrategy".equals(property)) { setResolveStrategy(((Number) newValue).intValue()); } else if ("directive".equals(property)) { setDirective(((Number) newValue).intValue()); } else { switch (resolveStrategy) { case DELEGATE_FIRST: setPropertyDelegateFirst(property, newValue); break; case DELEGATE_ONLY: InvokerHelper.setProperty(this.delegate, property, newValue); break; case OWNER_ONLY: InvokerHelper.setProperty(this.owner, property, newValue); break; case TO_SELF: super.setProperty(property, newValue); break; default: setPropertyOwnerFirst(property, newValue); } } }
Object callClosure(Object entity, Closure callable) { if (cloneFirst) { callable = (Closure) callable.clone(); } callable.setResolveStrategy(Closure.DELEGATE_FIRST); callable.setDelegate(entity); return callable.call(); }
@Override public void doWithWebDescriptor(GPathResult webXml) { if (pluginBean.isReadableProperty(DO_WITH_WEB_DESCRIPTOR)) { Closure c = (Closure) plugin.getProperty(DO_WITH_WEB_DESCRIPTOR); c.setResolveStrategy(Closure.DELEGATE_FIRST); c.setDelegate(this); c.call(webXml); } }
private void invokeClosureNode(Object args) { if (args instanceof Closure) { Closure callable = (Closure) args; callable.setDelegate(this); callable.setResolveStrategy(Closure.DELEGATE_FIRST); callable.call(); } }
private static DocumentDefinitions applyDefinitionsClosure( @DelegatesTo(value = DocumentDefinitions.class, strategy = Closure.DELEGATE_FIRST) Closure<?> closure) { Closure<?> docDefClosure = (Closure<?>) closure.clone(); docDefClosure.setResolveStrategy(Closure.DELEGATE_FIRST); DocumentDefinitions definitions = new DocumentDefinitions(); docDefClosure.setDelegate(definitions); docDefClosure.call(); return definitions; }
private Object evaluateCondition(Closure condition) { condition.setDelegate(new PreconditionContext()); condition.setResolveStrategy(Closure.DELEGATE_ONLY); try { return condition.call(); } catch (Exception e) { throw new ExtensionException("Failed to evaluate @Requires condition", e); } }
@SuppressWarnings("rawtypes") public List evaluateMappings(Closure closure) { UrlMappingBuilder builder = new UrlMappingBuilder(null, servletContext); closure.setDelegate(builder); closure.setResolveStrategy(Closure.DELEGATE_FIRST); closure.call(applicationContext); builder.urlDefiningMode = false; configureUrlMappingDynamicObjects(closure); return builder.getUrlMappings(); }
/** * @param closure . * @param obj . * @return . */ protected Object swapActiveObject(final Closure<Object> closure, final Object obj) { final Object prevObject = this.activeObject; this.activeObject = obj; final Map<String, Method> prevMethods = this.activeMethods; this.activeMethods = new HashMap<String, Method>(); final Method[] methods = this.activeObject.getClass().getMethods(); for (final Method method : methods) { this.activeMethods.put(method.getName(), method); } closure.setResolveStrategy(Closure.OWNER_ONLY); final Object res = closure.call(); activeObject = prevObject; this.activeMethods = prevMethods; return res; }
protected String evaluateNameForValue(Object value, GrailsWebRequest webRequest) { if (value == null) { return null; } String name; if (value instanceof Closure) { Closure callable = (Closure) value; final Closure cloned = (Closure) callable.clone(); cloned.setDelegate(webRequest); cloned.setResolveStrategy(Closure.DELEGATE_FIRST); Object result = cloned.call(); name = result != null ? result.toString() : null; } else if (value instanceof Map) { Map httpMethods = (Map) value; name = (String) httpMethods.get(webRequest.getCurrentRequest().getMethod()); } else { name = value.toString(); } return name != null ? name.trim() : null; }
@Override public void doWithWebDescriptor(final Element webXml) { if (!pluginBean.isReadableProperty(DO_WITH_WEB_DESCRIPTOR)) { return; } final Closure c = (Closure) plugin.getProperty(DO_WITH_WEB_DESCRIPTOR); c.setResolveStrategy(Closure.DELEGATE_FIRST); c.setDelegate(this); DefaultGroovyMethods.use( this, DOMCategory.class, new Closure<Object>(this) { private static final long serialVersionUID = 1; @Override public Object call(Object... args) { return c.call(webXml); } }); }
/** * This method is called when a bean definition node is called. * * @param beanName the name of the bean to define * @param args the arguments to the bean. The first argument is the class name, the last argument * is sometimes a closure. All the arguments in between are constructor arguments. * @return the bean definition wrapper */ private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) { boolean hasClosureArgument = (args[args.length - 1] instanceof Closure); if (args[0] instanceof Class) { Class<?> beanClass = (Class<?>) args[0]; if (args.length >= 1) { if (hasClosureArgument) { if (args.length - 1 != 1) { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper( beanName, beanClass, resolveConstructorArguments(args, 1, args.length - 1)); } else { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, beanClass); } } else { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper( beanName, beanClass, resolveConstructorArguments(args, 1, args.length)); } } } else if (args[0] instanceof RuntimeBeanReference) { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); this.currentBeanDefinition .getBeanDefinition() .setFactoryBeanName(((RuntimeBeanReference) args[0]).getBeanName()); } else if (args[0] instanceof Map) { // named constructor arguments if (args.length > 1 && args[1] instanceof Class) { List constructorArgs = resolveConstructorArguments( args, 2, hasClosureArgument ? args.length - 1 : args.length); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class) args[1], constructorArgs); Map namedArgs = (Map) args[0]; for (Object o : namedArgs.keySet()) { String propName = (String) o; setProperty(propName, namedArgs.get(propName)); } } // factory method syntax else { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); // First arg is the map containing factoryBean : factoryMethod Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next(); // If we have a closure body, that will be the last argument. // In between are the constructor args int constructorArgsTest = hasClosureArgument ? 2 : 1; // If we have more than this number of args, we have constructor args if (args.length > constructorArgsTest) { // factory-method requires args int endOfConstructArgs = (hasClosureArgument ? args.length - 1 : args.length); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper( beanName, null, resolveConstructorArguments(args, 1, endOfConstructArgs)); } else { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); } this.currentBeanDefinition .getBeanDefinition() .setFactoryBeanName(factoryBeanEntry.getKey().toString()); this.currentBeanDefinition .getBeanDefinition() .setFactoryMethodName(factoryBeanEntry.getValue().toString()); } } else if (args[0] instanceof Closure) { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); this.currentBeanDefinition.getBeanDefinition().setAbstract(true); } else { List constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length - 1 : args.length); currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, constructorArgs); } if (hasClosureArgument) { Closure callable = (Closure) args[args.length - 1]; callable.setDelegate(this); callable.setResolveStrategy(Closure.DELEGATE_FIRST); callable.call(new Object[] {currentBeanDefinition}); } GroovyBeanDefinitionWrapper beanDefinition = currentBeanDefinition; this.currentBeanDefinition = null; beanDefinition .getBeanDefinition() .setAttribute(GroovyBeanDefinitionWrapper.class.getName(), beanDefinition); getRegistry().registerBeanDefinition(beanName, beanDefinition.getBeanDefinition()); return beanDefinition; }
public Object eval( @SuppressWarnings("rawtypes") HashMap map, @SuppressWarnings("rawtypes") Closure cl) { cl.setDelegate(this); cl.setResolveStrategy(Closure.DELEGATE_FIRST); return cl.call(); }
public void setResolveStrategy(int resolveStrategy) { ((Closure) getOwner()).setResolveStrategy(resolveStrategy); second.setResolveStrategy(resolveStrategy); }
public Object eval(HashMap map, Closure cl) { cl.setDelegate(this); cl.setResolveStrategy(Closure.DELEGATE_FIRST); return cl.call(); }