Java Code Examples for groovy.lang.Closure#setDelegate()

The following examples show how to use groovy.lang.Closure#setDelegate() . 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 Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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 2
Source File: ObjectGraphBuilder.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the current ReferenceResolver.<br>
 * It will assign DefaultReferenceResolver if null.<br>
 * It accepts a ReferenceResolver instance, a String or a Closure.
 */
public void setReferenceResolver(final Object referenceResolver) {
    if (referenceResolver instanceof ReferenceResolver) {
        this.referenceResolver = (ReferenceResolver) referenceResolver;
    } else if (referenceResolver instanceof String) {
        this.referenceResolver = nodeName -> (String) referenceResolver;
    } else if (referenceResolver instanceof Closure) {
        final ObjectGraphBuilder self = this;
        this.referenceResolver = nodeName -> {
            Closure cls = (Closure) referenceResolver;
            cls.setDelegate(self);
            return (String) cls.call(new Object[]{nodeName});
        };
    } else {
        this.referenceResolver = new DefaultReferenceResolver();
    }
}
 
Example 3
Source File: TestCaseScript.java    From mdw with Apache License 2.0 5 votes vote down vote up
public TestCaseProcess process(Closure<?> cl) throws TestException {
    cl.setResolveStrategy(Closure.DELEGATE_FIRST);
    cl.setDelegate(process);
    cl.call();
    if (getTestCaseRun().testCaseProcess == null)
        getTestCaseRun().testCaseProcess = process;
    return process;
}
 
Example 4
Source File: SecureASTCustomizerFactory.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onNodeChildren(final FactoryBuilderSupport builder, final Object node, final Closure childContent) {
    if (node instanceof SecureASTCustomizer) {
        Closure clone = (Closure) childContent.clone();
        clone.setDelegate(node);
        clone.setResolveStrategy(Closure.DELEGATE_FIRST);
        clone.call();
    }
    return false;
}
 
Example 5
Source File: JsonDelegate.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method for creating <code>JsonDelegate</code>s from closures.
 *
 * @param c closure representing JSON objects
 * @return an instance of <code>JsonDelegate</code>
 */
public static Map<String, Object> cloneDelegateAndGetContent(Closure<?> c) {
    JsonDelegate delegate = new JsonDelegate();
    Closure<?> cloned = (Closure<?>) c.clone();
    cloned.setDelegate(delegate);
    cloned.setResolveStrategy(Closure.DELEGATE_FIRST);
    cloned.call();

    return delegate.getContent();
}
 
Example 6
Source File: FactoryBuilderSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void registerExplicitProperty(String name, String groupName, Closure getter, Closure setter) {
    // set the delegate to FBS so the closure closes over the builder
    if (getter != null) getter.setDelegate(this);
    if (setter != null) setter.setDelegate(this);
    explicitProperties.put(name, new Closure[]{getter, setter});
    String methodNameBase = capitalize(name);
    if (getter != null) {
        getRegistrationGroup(groupName).add("get" + methodNameBase);
    }
    if (setter != null) {
        getRegistrationGroup(groupName).add("set" + methodNameBase);
    }
}
 
Example 7
Source File: ReflexMatchContext.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public void delay(Closure<?> config) {
   ReflexDelayContext ctx = new ReflexDelayContext(this);
   try (ReflexActionPop pop = new ReflexActionPop(ctx)) {
      config.setDelegate(ctx);
      config.call();
   } finally {
      addAction(ctx.delayed);
   }
}
 
Example 8
Source File: ConstantDsl.java    From jira-groovioli with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public IssueType findIssueType(@DelegatesTo(FindIssueTypeHandler.class) Closure closure) {
    FindIssueTypeHandler findIssueTypeHandler = new FindIssueTypeHandler();
    closure.setDelegate(findIssueTypeHandler);
    closure.call();

    ConstantsManager constantsManager = (ConstantsManager) closure.getProperty("constantsManager");
    Collection<IssueType> allIssueTypeObjects =  constantsManager.getAllIssueTypeObjects();
    for (IssueType issueType : allIssueTypeObjects) {
        if (issueType.getName().equals(findIssueTypeHandler.name)) {
            return issueType;
        }
    }
    return null;
}
 
Example 9
Source File: ThorntailExtension.java    From thorntail with Apache License 2.0 5 votes vote down vote up
/**
 * Closure for applying properties to the Swarm configuration.
 */
public void properties(Closure<Properties> closure) {
    ConfigObject config = new ConfigObject();
    closure.setResolveStrategy(Closure.DELEGATE_ONLY);
    closure.setDelegate(config);
    closure.call();
    config.flatten(this.properties);
}
 
Example 10
Source File: SwarmExtension.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
public void properties(Closure<Properties> closure) {
    ConfigObject config = new ConfigObject();
    closure.setResolveStrategy(Closure.DELEGATE_ONLY);
    closure.setDelegate(config);
    closure.call();
    config.flatten(this.properties);
}
 
Example 11
Source File: JsonDelegate.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method for creating <code>JsonDelegate</code>s from closures currying an object
 * argument.
 *
 * @param c closure representing JSON objects
 * @param o an object curried to the closure
 * @return an instance of <code>JsonDelegate</code>
 */
public static Map<String, Object> curryDelegateAndGetContent(Closure<?> c, Object o) {
    JsonDelegate delegate = new JsonDelegate();
    Closure<?> curried = c.curry(o);
    curried.setDelegate(delegate);
    curried.setResolveStrategy(Closure.DELEGATE_FIRST);
    curried.call();

    return delegate.getContent();
}
 
Example 12
Source File: StreamingJsonBuilder.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void curryDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c, Object o, boolean first, JsonGenerator generator) {
    StreamingJsonDelegate delegate = new StreamingJsonDelegate(w, first, generator);
    Closure curried = c.curry(o);
    curried.setDelegate(delegate);
    curried.setResolveStrategy(Closure.DELEGATE_FIRST);
    curried.call();
}
 
Example 13
Source File: ClojureBuild.java    From clojurephant with Apache License 2.0 4 votes vote down vote up
public void compiler(Closure<?> configureAction) {
  configureAction.setResolveStrategy(Closure.DELEGATE_FIRST);
  configureAction.setDelegate(compiler);
  configureAction.call(compiler);
}
 
Example 14
Source File: Url.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
/**
 * The query parameters part of the contract.
 * @param consumer function to manipulate the query parameters
 */
public void queryParameters(@DelegatesTo(QueryParameters.class) Closure consumer) {
	this.queryParameters = new QueryParameters();
	consumer.setDelegate(this.queryParameters);
	consumer.call();
}
 
Example 15
Source File: JUnit4GroovyMockery.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
void closureInit(Closure cl, Object delegate) {
    cl.setDelegate(delegate);
    cl.call();
}
 
Example 16
Source File: JUnit4GroovyMockery.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
void closureInit(Closure cl, Object delegate) {
    cl.setDelegate(delegate);
    cl.call();
}
 
Example 17
Source File: Input.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
/**
 * The message headers part of the contract.
 * @param consumer function to manipulate the message headers
 */
public void messageHeaders(@DelegatesTo(Headers.class) Closure consumer) {
	this.messageHeaders = new Headers();
	consumer.setDelegate(this.messageHeaders);
	consumer.call();
}
 
Example 18
Source File: ZWaveActionContext.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public void ordered(Closure<?> closure) {
   ZWaveSendOrdered ctx = new ZWaveSendOrdered(GroovyCapabilityDefinition.CapabilityHandlerContext.getContext(this));
   closure.setResolveStrategy(Closure.DELEGATE_FIRST);
   closure.setDelegate(ctx);
   closure.call();
}
 
Example 19
Source File: Response.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
/**
 * Allows to set matchers for the body.
 * @param consumer function to manipulate the URL
 */
public void bodyMatchers(@DelegatesTo(ResponseBodyMatchers.class) Closure consumer) {
	this.bodyMatchers = new ResponseBodyMatchers();
	consumer.setDelegate(this.bodyMatchers);
	consumer.call();
}
 
Example 20
Source File: HttpBuilder.java    From http-builder-ng with Apache License 2.0 3 votes vote down vote up
/**
 * Creates an `HttpBuilder` configured with the provided configuration closure, using the `defaultFactory` as the client factory.
 *
 * The configuration closure delegates to the {@link HttpObjectConfig} interface, which is an extension of the {@link HttpConfig} interface -
 * configuration properties from either may be applied to the global client configuration here. See the documentation for those interfaces for
 * configuration property details.
 *
 * [source,groovy]
 * ----
 * def factory = { c -> new ApacheHttpBuilder(c); } as Function
 *
 * def http = HttpBuilder.configure(factory){
 *     request.uri = 'http://localhost:10101'
 * }
 * ----
 *
 * @param factory the {@link HttpObjectConfig} factory function ({@link JavaHttpBuilder} or {@link groovyx.net.http.ApacheHttpBuilder})
 * @param closure the configuration closure (delegated to {@link HttpObjectConfig})
 * @return the configured `HttpBuilder`
 */
public static HttpBuilder configure(final Function<HttpObjectConfig, ? extends HttpBuilder> factory, @DelegatesTo(HttpObjectConfig.class) final Closure closure) {
    HttpObjectConfig impl = new HttpObjectConfigImpl();
    closure.setDelegate(impl);
    closure.setResolveStrategy(Closure.DELEGATE_FIRST);
    closure.call();
    return factory.apply(impl);
}