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);
}
}
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);
}
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");
}
}
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;
}
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"));
}
Aggregations