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

The following examples show how to use groovy.lang.Closure#setResolveStrategy() . 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 (Objects.equal(e.getType(), closure.getClass()) && Objects.equal(e.getMethod(), "doCall")) {
            throw new InvalidActionClosureException(closure, delegate);
        }
        throw e;
    }
}
 
Example 2
Source File: TestCaseScript.java    From mdw with Apache License 2.0 6 votes vote down vote up
public Verifiable verify(TestCaseProcess[] processes, Closure<?> cl) throws TestException {
    getTestCaseRun().setPreFilter(before -> substitute(before));

    if (cl != null) {
        cl.setResolveStrategy(Closure.DELEGATE_FIRST);
        cl.setDelegate(process);
        cl.call();
    }
    if (process.getExpectedResults() == null)
        process.setExpectedResults(getDefaultExpectedResults());
    if (process.getExcludeVariables() != null && !process.getExcludeVariables().isEmpty())
        processes[0].setExcludeVariables(process.getExcludeVariables());

    process.setSuccess(getTestCaseRun().verifyProcess(processes, process.getExpectedResults()));
    return process;
}
 
Example 3
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 4
Source File: TestCaseScript.java    From mdw with Apache License 2.0 5 votes vote down vote up
public TestCaseResponse response(Closure<?> cl) throws TestException {
    if (response == null)
        response = new TestCaseResponse();
    if (cl != null) {
        cl.setResolveStrategy(Closure.DELEGATE_FIRST);
        cl.setDelegate(response);
        cl.call();
    }
    return response;
}
 
Example 5
Source File: ThorntailExtension.java    From thorntail with Apache License 2.0 5 votes vote down vote up
public void environment(Closure<Properties> closure) {
    ConfigObject config = new ConfigObject();
    closure.setResolveStrategy(Closure.DELEGATE_ONLY);
    closure.setDelegate(config);
    closure.call();
    config.flatten(this.environment);
}
 
Example 6
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 7
Source File: TestCaseScript.java    From mdw with Apache License 2.0 5 votes vote down vote up
public TestCaseProcess process(String target, Closure<?> cl) throws TestException {
    process = new TestCaseProcess(getTestCaseRun().getProcess(target));
    if (cl != null) {
        cl.setResolveStrategy(Closure.DELEGATE_FIRST);
        cl.setDelegate(process);
        cl.call();
    }
    if (getTestCaseRun().testCaseProcess == null)
        getTestCaseRun().testCaseProcess = process;
    return process;
}
 
Example 8
Source File: TestCaseScript.java    From mdw with Apache License 2.0 5 votes vote down vote up
public ApiRequest request(String target, Map<String,String> options, Closure<?> cl) throws TestException {
    int dotPostmanSlash = target.lastIndexOf(".postman/");
    if (dotPostmanSlash == -1)
        throw new TestException("Bad API test path: " + target);
    String assetPath = target.substring(0, dotPostmanSlash + 8);
    Asset asset = asset(assetPath);
    if (asset == null)
        throw new TestException("API test case asset not found: " + assetPath);
    TestCase apiTestCase = new TestCase(asset.getPackageName(), new AssetInfo(asset.getName()));
    try {
        String itemPath = asset.getPackageName() + "/" + asset.getName() + "/"
                + target.substring(dotPostmanSlash + 9).replace('/', '~');
        TestCaseItem item = ServiceLocator.getTestingServices().getTestCaseItem(itemPath);
        if (item == null)
            throw new TestException("Test case item not found: " + itemPath);
        apiTestCase.addItem(item);
        apiRequest = new ApiRequest(apiTestCase, options);
        if (cl != null) {
            cl.setResolveStrategy(Closure.DELEGATE_FIRST);
            cl.setDelegate(apiRequest);
            cl.call();
        }

        return apiRequest;
    }
    catch (ServiceException ex) {
        throw new TestException(ex.getMessage(), ex);
    }
}
 
Example 9
Source File: HttpBuilder.java    From http-builder-ng with Apache License 2.0 5 votes vote down vote up
private ChainedHttpConfig configureRequest(final Class<?> type, final HttpVerb verb, final Closure closure) {
    final HttpConfigs.BasicHttpConfig myConfig = HttpConfigs.requestLevel(getObjectConfig());
    closure.setDelegate(myConfig);
    closure.setResolveStrategy(Closure.DELEGATE_FIRST);
    closure.call();

    myConfig.getChainedRequest().setVerb(verb);
    myConfig.getChainedResponse().setType(type);

    return myConfig;
}
 
Example 10
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 11
Source File: MatcherUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static <T> Closure<T> cloneWithDelegate(final Closure<T> predicate, final Object delegate) {
    Closure<T> clone = (Closure<T>) predicate.clone();
    clone.setDelegate(delegate);
    clone.setResolveStrategy(Closure.DELEGATE_FIRST);
    return clone;
}
 
Example 12
Source File: DefaultClojureSourceSet.java    From clojurephant with Apache License 2.0 5 votes vote down vote up
@Override
public ClojureSourceSet clojure(Closure<?> configureAction) {
  configureAction.setResolveStrategy(Closure.DELEGATE_FIRST);
  configureAction.setDelegate(clojure);
  configureAction.call(clojure);
  return this;
}
 
Example 13
Source File: ClosureRenderer.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void setUpdate(Closure update) {
    if (update != null) {
        update.setDelegate(this);
        update.setResolveStrategy(Closure.DELEGATE_FIRST);
    }
    this.update = update;
}
 
Example 14
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 15
Source File: ClosureBackedRuleAction.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void execute(T subject, List<?> inputs) {
    Closure copy = (Closure) closure.clone();
    copy.setResolveStrategy(Closure.DELEGATE_FIRST);
    copy.setDelegate(subject);

    Object[] argList = new Object[inputs.size() + 1];
    argList[0] = subject;
    int i = 1;
    for (Object arg : inputs) {
        argList[i++] = arg;
    }
    copy.call(argList);
}
 
Example 16
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 17
Source File: ClosureBackedRuleAction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void execute(T subject, List<?> inputs) {
    Closure copy = (Closure) closure.clone();
    copy.setResolveStrategy(Closure.DELEGATE_FIRST);
    copy.setDelegate(subject);

    Object[] argList = new Object[inputs.size() + 1];
    argList[0] = subject;
    int i = 1;
    for (Object arg : inputs) {
        argList[i++] = arg;
    }
    copy.call(argList);
}
 
Example 18
Source File: TestCaseScript.java    From mdw with Apache License 2.0 4 votes vote down vote up
/**
 * responder closure call is delayed until stub server calls back
 */
public TestCaseAdapterStub adapter(Closure<Boolean> matcher, Closure<String> responder, Closure<?> init) throws TestException {
    final TestCaseAdapterStub adapterStub = new TestCaseAdapterStub(matcher, responder);
    if (init != null) {
        init.setResolveStrategy(Closure.DELEGATE_FIRST);
        init.setDelegate(adapterStub);
        init.call();
    }
    if (responder == null) {
        adapterStub.setResponder(new Closure<String>(this, adapterStub) {
            @Override
            public String call(Object request) {
                // binding for request
                final TestCaseRun testCaseRun = getTestCaseRun();
                if (adapterStub.getResponse().contains("${")) {
                    try {
                        Binding binding = getBinding();
                        if (request.toString().startsWith("{")) {
                            Object req = new JsonSlurper().parseText(request.toString());
                            binding.setVariable("request", req);
                        }
                        else {
                            GPathResult gpathRequest = new XmlSlurper().parseText(request.toString());
                            binding.setVariable("request", gpathRequest);
                        }
                        CompilerConfiguration compilerCfg = new CompilerConfiguration();
                        compilerCfg.setScriptBaseClass(DelegatingScript.class.getName());
                        GroovyShell shell = new GroovyShell(TestCaseScript.class.getClassLoader(), binding, compilerCfg);
                        shell.setProperty("out", testCaseRun.getLog());
                        DelegatingScript script = (DelegatingScript) shell.parse("return \"\"\"" + adapterStub.getResponse() + "\"\"\"");
                        script.setDelegate(TestCaseScript.this);
                        return script.run().toString();
                    }
                    catch (Exception ex) {
                        getTestCaseRun().getLog().println("Cannot perform stub substitutions for request: " + request);
                        ex.printStackTrace(getTestCaseRun().getLog());
                    }
                }
                return adapterStub.getResponse();
            }
        });
    }
    return adapterStub;
}
 
Example 19
Source File: ClojureScriptBuild.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 20
Source File: ZigbeeActionContext.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public void ordered(Closure<?> closure) {
   ZigbeeSendOrdered ctx = new ZigbeeSendOrdered(GroovyCapabilityDefinition.CapabilityHandlerContext.getContext(this));
   closure.setResolveStrategy(Closure.DELEGATE_FIRST);
   closure.setDelegate(ctx);
   closure.call();
}