DATACMNS-533 - Discover declared functions on EvaluationContextExtension.
We new automatically register public static methods declared on classes extending EvaluationContextExtensionSupport as functions to be used within SpEL expressions. Related pull request: spring-projects/spring-data-jpa#101 Related ticket: DATAJPA-564
This commit is contained in:
committed by
Oliver Gierke
parent
d303f6ac37
commit
9ab9467386
@@ -168,11 +168,13 @@ public class ExtensionAwareEvaluationContextProvider implements EvaluationContex
|
||||
|
||||
for (EvaluationContextExtension ext : extensions) {
|
||||
|
||||
Map<String, Method> extFunctions = ext.getFunctions();
|
||||
|
||||
if (ext.getExtensionId() != null) {
|
||||
functions.put(ext.getExtensionId(), ext.getFunctions());
|
||||
functions.put(ext.getExtensionId(), extFunctions);
|
||||
}
|
||||
|
||||
functions.putAll(ext.getFunctions());
|
||||
functions.putAll(extFunctions);
|
||||
}
|
||||
|
||||
this.functions = functions;
|
||||
|
||||
@@ -16,9 +16,14 @@
|
||||
package org.springframework.data.repository.query.spi;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.ReflectionUtils.MethodCallback;
|
||||
|
||||
/**
|
||||
* A base class for {@link EvaluationContextExtension}s.
|
||||
*
|
||||
@@ -28,6 +33,31 @@ import java.util.Map;
|
||||
*/
|
||||
public abstract class EvaluationContextExtensionSupport implements EvaluationContextExtension {
|
||||
|
||||
private final Map<String, Method> declaredFunctions;
|
||||
|
||||
public EvaluationContextExtensionSupport() {
|
||||
|
||||
this.declaredFunctions = discoverDeclaredFunctions();
|
||||
}
|
||||
|
||||
private Map<String, Method> discoverDeclaredFunctions() {
|
||||
|
||||
final Map<String, Method> map = new HashMap<String, Method>();
|
||||
|
||||
ReflectionUtils.doWithMethods(getClass(), new MethodCallback() {
|
||||
|
||||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
|
||||
if (Modifier.isPublic(method.getModifiers()) && Modifier.isStatic(method.getModifiers())) {
|
||||
map.put(method.getName(), method);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return map.isEmpty() ? Collections.<String, Method> emptyMap() : Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.EvaluationContextExtension#getProperties()
|
||||
@@ -43,6 +73,6 @@ public abstract class EvaluationContextExtensionSupport implements EvaluationCon
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Method> getFunctions() {
|
||||
return Collections.emptyMap();
|
||||
return this.declaredFunctions;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user