本文整理汇总了Java中org.codehaus.groovy.runtime.MethodClosure.call方法的典型用法代码示例。如果您正苦于以下问题:Java MethodClosure.call方法的具体用法?Java MethodClosure.call怎么用?Java MethodClosure.call使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.codehaus.groovy.runtime.MethodClosure
的用法示例。
在下文中一共展示了MethodClosure.call方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: asCollection
import org.codehaus.groovy.runtime.MethodClosure; //导入方法依赖的package包/类
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);
}
}
示例2: interact
import org.codehaus.groovy.runtime.MethodClosure; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static synchronized void interact(MethodClosure function, Object... parameters) {
final List<ValueWidget<?>> widgets = widgetsFromAbbreviations(parameters);
for (ValueWidget<?> widget : widgets) {
widget.getComm().addMsgCallbackList(widget.new ValueChangeMsgCallbackHandler() {
private void processCode(Object... params) throws Exception {
Object call = function.call(getWidgetValues());
if (call instanceof String || call instanceof Number) {
label.setValue(call);
}
}
@Override
public void updateValue(Object value, Message message) {
try {
processCode();
} catch (Exception e) {
throw new IllegalStateException("Error occurred during updating interactive widget.", e);
}
}
private Object[] getWidgetValues() {
List<Object> ret = new ArrayList<>(widgets.size());
for (ValueWidget<?> wid : widgets) {
ret.add(wid.getValue());
}
return ret.toArray(new Object[ret.size()]);
}
});
logger.info("interact Widget: " + widget.getClass().getName());
}
widgets.forEach(Widget::display);
Object response = function.call(widgets.stream().map(ValueWidget::getValue).toArray());
if (response instanceof Widget) {
((Widget) response).display();
} else {
label = new Label();
label.setValue(response);
label.display();
}
}