Search in sources :

Example 16 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project groovy by apache.

the class DefaultTypeTransformation method asCollection.

public static Collection asCollection(Object value) {
    if (value == null) {
        return Collections.EMPTY_LIST;
    } else if (value instanceof Collection) {
        return (Collection) value;
    } else if (value instanceof Map) {
        Map map = (Map) value;
        return map.entrySet();
    } else if (value.getClass().isArray()) {
        return arrayAsCollection(value);
    } else if (value instanceof MethodClosure) {
        MethodClosure method = (MethodClosure) value;
        IteratorClosureAdapter adapter = new IteratorClosureAdapter(method.getDelegate());
        method.call(adapter);
        return adapter.asList();
    } else if (value instanceof String || value instanceof GString) {
        return StringGroovyMethods.toList((CharSequence) value);
    } else if (value instanceof File) {
        try {
            return ResourceGroovyMethods.readLines((File) value);
        } catch (IOException e) {
            throw new GroovyRuntimeException("Error reading file: " + value, e);
        }
    } else if (value instanceof Class && ((Class) value).isEnum()) {
        Object[] values = (Object[]) InvokerHelper.invokeMethod(value, "values", EMPTY_OBJECT_ARRAY);
        return Arrays.asList(values);
    } else {
        // let's assume it's a collection of 1
        return Collections.singletonList(value);
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) Collection(java.util.Collection) CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) NullObject(org.codehaus.groovy.runtime.NullObject) IteratorClosureAdapter(org.codehaus.groovy.runtime.IteratorClosureAdapter) GString(groovy.lang.GString) IOException(java.io.IOException) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) GString(groovy.lang.GString) Map(java.util.Map) File(java.io.File)

Example 17 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project cuba by cuba-platform.

the class SecurityImpl method evaluateConstraintScript.

@Override
public Object evaluateConstraintScript(Entity entity, String groovyScript) {
    Map<String, Object> context = new HashMap<>();
    context.put("__entity__", entity);
    context.put("parse", new MethodClosure(this, "parseValue"));
    context.put("userSession", userSessionSource.getUserSession());
    fillGroovyConstraintsContext(context);
    return scripting.evaluateGroovy(groovyScript.replace("{E}", "__entity__"), context);
}
Also used : MethodClosure(org.codehaus.groovy.runtime.MethodClosure)

Example 18 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project cuba by cuba-platform.

the class ExceptionReportServiceBean method sendExceptionReport.

@Override
public void sendExceptionReport(String supportEmail, Map<String, Object> binding) {
    try {
        Map<String, Object> map = new HashMap<>();
        map.putAll(binding);
        map.put("toHtml", new MethodClosure(HtmlUtils.class, "convertToHtml"));
        String body = getExceptionReportBody(map);
        String subject = getExceptionReportSubject(map);
        EmailInfo info = new EmailInfo(supportEmail, subject, body);
        User user = userSessionSource.getUserSession().getUser();
        if (user.getEmail() != null) {
            info.setFrom(user.getEmail());
        }
        emailer.sendEmail(info);
    } catch (Exception e) {
        log.error("Error sending exception report", e);
        throw new RuntimeException("Error sending exception report");
    }
}
Also used : User(com.haulmont.cuba.security.entity.User) HashMap(java.util.HashMap) HtmlUtils(com.haulmont.bali.util.HtmlUtils) EmailInfo(com.haulmont.cuba.core.global.EmailInfo) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) IOException(java.io.IOException)

Example 19 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project rest-assured by rest-assured.

the class HTTPBuilder method buildDefaultResponseHandlers.

/**
 * Creates default response handlers for {@link Status#SUCCESS success} and
 * {@link Status#FAILURE failure} status codes.  This is used to populate
 * the handler map when a new HTTPBuilder instance is created.
 *
 * @return the default response handler map.
 * @see #defaultSuccessHandler(HttpResponseDecorator, Object)
 * @see #defaultFailureHandler(HttpResponseDecorator)
 */
protected Map<Object, Closure> buildDefaultResponseHandlers() {
    Map<Object, Closure> map = new StringHashMap<Closure>();
    map.put(Status.SUCCESS, new MethodClosure(this, "defaultSuccessHandler"));
    map.put(Status.FAILURE, new MethodClosure(this, "defaultFailureHandler"));
    return map;
}
Also used : MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) MethodClosure(org.codehaus.groovy.runtime.MethodClosure)

Example 20 with MethodClosure

use of org.codehaus.groovy.runtime.MethodClosure in project ffx by mjschnie.

the class ModelingShell method initContext.

/**
 * Initialize access to Force Field X variables and methods from with the
 * Shell.
 */
private void initContext() {
    setVariable("dat", mainPanel.getHierarchy());
    setVariable("cmd", mainPanel);
    setVariable("vis", mainPanel.getGraphics3D());
    setVariable("sh", this);
    setVariable("active", mainPanel.getHierarchy().getActive());
    setVariable("logger", logger);
    // Timer
    setVariable("time", new MethodClosure(this, "time"));
    // File
    setVariable("open", new MethodClosure(mainPanel, "openWait"));
    setVariable("convertWait", new MethodClosure(mainPanel, "convertWait"));
    setVariable("save", new MethodClosure(mainPanel, "saveAsXYZ"));
    setVariable("saveAsXYZ", new MethodClosure(mainPanel, "saveAsXYZ"));
    setVariable("saveAsP1", new MethodClosure(mainPanel, "saveAsP1"));
    setVariable("saveAsPDB", new MethodClosure(mainPanel, "saveAsPDB"));
    setVariable("close", new MethodClosure(mainPanel, "closeWait"));
    setVariable("closeAll", new MethodClosure(mainPanel, "closeAll"));
    // Select
    setVariable("select", new MethodClosure(this, "select"));
    // Display and View menus.
    if (!headless) {
        GraphicsCanvas graphics = mainPanel.getGraphics3D();
        // Display
        int index = 0;
        for (ViewModel view : ViewModel.values()) {
            setVariable(view.name(), view);
            index++;
            if (index > 8) {
                break;
            }
        }
        setVariable("view", new MethodClosure(graphics, "viewWait"));
        // Color
        index = 0;
        for (ColorModel color : ColorModel.values()) {
            setVariable(color.name(), color);
            index++;
            if (index > 6) {
                break;
            }
        }
        setVariable("color", new MethodClosure(graphics, "colorWait"));
    }
    // Algorithms
    setVariable("returnEnergy", new MethodClosure(this, "returnEnergy"));
    setVariable("energy", new MethodClosure(this, "energy"));
    setVariable("analyze", new MethodClosure(this, "analyze"));
    setVariable("minimize", new MethodClosure(this, "minimize"));
    setVariable("minimize_2", new MethodClosure(this, "minimize_2"));
    setVariable("md", new MethodClosure(this, "md"));
    setVariable("potential", new MethodClosure(this, "potential"));
    setVariable("poledit", new MethodClosure(this, "poledit"));
    setVariable("superpose", new MethodClosure(this, "superpose"));
    // Obtain UIUtils object
    setVariable("getAlgorithmUtils", new MethodClosure(this, "getUIAlgorithmUtils"));
    setVariable("getPotentialsUtils", new MethodClosure(this, "getUIPotentialsUtils"));
}
Also used : ColorModel(ffx.potential.bonded.RendererCache.ColorModel) ViewModel(ffx.potential.bonded.RendererCache.ViewModel) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Point(java.awt.Point)

Aggregations

MethodClosure (org.codehaus.groovy.runtime.MethodClosure)28 IOException (java.io.IOException)5 CachedClass (org.codehaus.groovy.reflection.CachedClass)5 GeneratedClosure (org.codehaus.groovy.runtime.GeneratedClosure)4 Test (org.junit.Test)4 Binding (groovy.lang.Binding)3 Closure (groovy.lang.Closure)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 MetaClass (groovy.lang.MetaClass)3 MissingMethodException (groovy.lang.MissingMethodException)3 MissingPropertyException (groovy.lang.MissingPropertyException)3 Script (groovy.lang.Script)3 Tuple (groovy.lang.Tuple)3 PrintWriter (java.io.PrintWriter)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 CompiledScript (javax.script.CompiledScript)3 ScriptException (javax.script.ScriptException)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 InvokerInvocationException (org.codehaus.groovy.runtime.InvokerInvocationException)3