任何有興趣的解決方案:
/**
* {@code FunctionalInterfaceResolver} intercepts method calls with EL lambda expressions as
* arguments which need to coerce to Java 8 functional interfaces.
*
* A custom `ELResolver` is always consulted before the default resolvers.
*
* Example usage:
* ```` java
* final ELProcessor elp = new ELProcessor();
* elp.getELManager().addELResolver(new FunctionalInterfaceResolver());
* final Object result = elp.eval("p.findResources(o->o.getContentType() eq 'image/png')");
* ````
*
* @author <a href="mailto:[email protected]">Ronald K. Muller</a>
*/
public final class FunctionalInterfaceResolver extends ELResolver {
@Override
public Object invoke(final ELContext context, final Object base, final Object method,
final Class<?>[] paramTypes, final Object[] params) {
if (context == null || base == null || !(method instanceof String) || params == null) {
return null;
}
// takes about 5ms. Try out caching if it becomes a bottleneck
for (int i = 0; i < params.length; ++i) {
if (params[i] instanceof javax.el.LambdaExpression) {
for (Method m : base.getClass().getMethods()) {
if (m.getName().equals(method) && m.getParameterCount() == params.length) {
final Class[] types = m.getParameterTypes();
if (types[i].isAnnotationPresent(FunctionalInterface.class)) {
params[i] = coerceToFunctionalInterface(context,
(LambdaExpression)params[i], types[i]);
}
}
}
}
}
return null;
}
@Override
public Class<?> getType(ELContext context, Object base, Object property) {
return null;
}
@Override
public void setValue(ELContext context, Object base, Object property, Object value) {
}
@Override
public boolean isReadOnly(ELContext context, Object base, Object property) {
return false;
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
return null;
}
@Override
public Class<?> getCommonPropertyType(ELContext context, Object base) {
return String.class;
}
@Override
public Object convertToType(ELContext context, Object obj, Class<?> targetType) {
if (obj instanceof LambdaExpression &&
targetType.isAnnotationPresent(FunctionalInterface.class)) {
context.setPropertyResolved(obj, targetType);
return coerceToFunctionalInterface(context, (LambdaExpression)obj, targetType);
}
return null;
}
private Object coerceToFunctionalInterface(
final ELContext context, final LambdaExpression elLambda, final Class<?> targetType) {
assert targetType.isAnnotationPresent(FunctionalInterface.class);
return Proxy.newProxyInstance(targetType.getClassLoader(),
new Class[]{targetType}, (Object obj, Method method, Object[] args) -> {
// a FunctionalInterface has exactly one abstract method
if (Modifier.isAbstract(method.getModifiers())) {
// the "functional method"
return elLambda.invoke(context, args);
} else if ("toString".equals(method.getName())) {
return "Proxy[" + targetType.getName() + ", wrapping " +
elLambda.getClass().getName() + ']';
} else {
throw new AssertionError("Method not expected: " + method.getName());
}
});
}
@Override
public Object getValue(ELContext context, Object base, Object property) {
return null;
}
}
感謝。自EL3.0以來(使用Java 8語法)支持lambdas這一事實使我感到困惑,並且同樣適用於支持Streams(包括使用lambda語法的篩選器操作)。但是,只檢查了實現,所有這些功能都是使用Java 7進行模擬的。 – rmuller