INT-866 added "method" variable to the SpEL context and also refactored ExpressionBinding and ExpressionSource (work in progress).
This commit is contained in:
@@ -32,30 +32,36 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ExpressionBinding {
|
||||
|
||||
/**
|
||||
* Name of the variable in the context that refers to the method name.
|
||||
* <p>The default is "method".
|
||||
*/
|
||||
String methodNameVariableName() default ExpressionSource.DEFAULT_METHOD_NAME_VARIABLE_NAME;
|
||||
|
||||
/**
|
||||
* Names of the arguments as a comma-separated list. If not provided, the
|
||||
* names will be discovered automatically if enabled by the compiler settings.
|
||||
* These names will be used as the keys in the argument Map.
|
||||
*/
|
||||
String argNames() default "";
|
||||
String argumentVariableNames() default "";
|
||||
|
||||
/**
|
||||
* Name of the variable in the context that refers to the Map of arguments.
|
||||
* <p>The default is "args".
|
||||
*/
|
||||
String argumentMapName() default ExpressionSource.DEFAULT_ARGUMENT_MAP_NAME;
|
||||
String argumentMapVariableName() default ExpressionSource.DEFAULT_ARGUMENT_MAP_VARIABLE_NAME;
|
||||
|
||||
/**
|
||||
* Name of the variable in the context that refers to the return value, if any.
|
||||
* <p>The default is "return".
|
||||
*/
|
||||
String returnValueName() default ExpressionSource.DEFAULT_RETURN_VALUE_NAME;
|
||||
String returnValueVariableName() default ExpressionSource.DEFAULT_RETURN_VALUE_VARIABLE_NAME;
|
||||
|
||||
/**
|
||||
* Name of the variable in the context that refers to any exception thrown
|
||||
* by the method invocation that is being intercepted.
|
||||
* <p>The default is "exception".
|
||||
*/
|
||||
String exceptionName() default ExpressionSource.DEFAULT_EXCEPTION_NAME;
|
||||
String exceptionVariableName() default ExpressionSource.DEFAULT_EXCEPTION_VARIABLE_NAME;
|
||||
|
||||
}
|
||||
|
||||
@@ -27,11 +27,13 @@ import java.lang.reflect.Method;
|
||||
*/
|
||||
interface ExpressionSource {
|
||||
|
||||
static final String DEFAULT_ARGUMENT_MAP_NAME = "args";
|
||||
static final String DEFAULT_METHOD_NAME_VARIABLE_NAME = "method";
|
||||
|
||||
static final String DEFAULT_RETURN_VALUE_NAME = "return";
|
||||
static final String DEFAULT_ARGUMENT_MAP_VARIABLE_NAME = "args";
|
||||
|
||||
static final String DEFAULT_EXCEPTION_NAME = "exception";
|
||||
static final String DEFAULT_RETURN_VALUE_VARIABLE_NAME = "return";
|
||||
|
||||
static final String DEFAULT_EXCEPTION_VARIABLE_NAME = "exception";
|
||||
|
||||
|
||||
/**
|
||||
@@ -46,30 +48,36 @@ interface ExpressionSource {
|
||||
*/
|
||||
String[] getHeaderExpressions(Method method);
|
||||
|
||||
/**
|
||||
* Returns the variable name to be associated with the intercepted
|
||||
* method's name.
|
||||
*/
|
||||
String getMethodNameVariableName(Method method);
|
||||
|
||||
/**
|
||||
* Returns the variable names to be associated with the intercepted method
|
||||
* invocation's argument array.
|
||||
*/
|
||||
String[] getArgumentNames(Method method);
|
||||
String[] getArgumentVariableNames(Method method);
|
||||
|
||||
/**
|
||||
* Returns the variable name to use in the evaluation context for the Map
|
||||
* of arguments. The keys in this map will be determined by the result of
|
||||
* the {@link #getArgumentNames(Method)} method.
|
||||
*/
|
||||
String getArgumentMapName(Method method);
|
||||
String getArgumentMapVariableName(Method method);
|
||||
|
||||
/**
|
||||
* Returns the variable name to use in the evaluation context for any
|
||||
* return value resulting from the method invocation.
|
||||
*/
|
||||
String getReturnValueName(Method method);
|
||||
String getReturnValueVariableName(Method method);
|
||||
|
||||
/**
|
||||
* Returns the variable name to use in the evaluation context for any
|
||||
* exception thrown from the method invocation.
|
||||
*/
|
||||
String getExceptionName(Method method);
|
||||
String getExceptionVariableName(Method method);
|
||||
|
||||
/**
|
||||
* Returns the channel name to which Messages should be published
|
||||
|
||||
@@ -81,7 +81,8 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
|
||||
context.addPropertyAccessor(new MapAccessor());
|
||||
Class<?> targetClass = AopUtils.getTargetClass(invocation.getThis());
|
||||
Method method = AopUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
|
||||
String[] argumentNames = this.expressionSource.getArgumentNames(method);
|
||||
String[] argumentNames = this.expressionSource.getArgumentVariableNames(method);
|
||||
context.setVariable(this.expressionSource.getMethodNameVariableName(method), method.getName());
|
||||
if (invocation.getArguments().length > 0 && argumentNames != null) {
|
||||
int index = 0;
|
||||
Map<String, Object> argumentMap = new HashMap<String, Object>();
|
||||
@@ -91,15 +92,15 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
|
||||
}
|
||||
argumentMap.put(argumentName, invocation.getArguments()[index++]);
|
||||
}
|
||||
context.setVariable(this.expressionSource.getArgumentMapName(method), argumentMap);
|
||||
context.setVariable(this.expressionSource.getArgumentMapVariableName(method), argumentMap);
|
||||
}
|
||||
try {
|
||||
Object returnValue = invocation.proceed();
|
||||
context.setVariable(this.expressionSource.getReturnValueName(method), returnValue);
|
||||
context.setVariable(this.expressionSource.getReturnValueVariableName(method), returnValue);
|
||||
return returnValue;
|
||||
}
|
||||
catch (Throwable t) {
|
||||
context.setVariable(this.expressionSource.getExceptionName(method), t);
|
||||
context.setVariable(this.expressionSource.getExceptionVariableName(method), t);
|
||||
throw t;
|
||||
}
|
||||
finally {
|
||||
@@ -110,7 +111,7 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
|
||||
private void publishMessage(Method method, StandardEvaluationContext context) throws Exception {
|
||||
String payloadExpressionString = this.expressionSource.getPayloadExpression(method);
|
||||
if (!StringUtils.hasText(payloadExpressionString)) {
|
||||
payloadExpressionString = "#" + this.expressionSource.getReturnValueName(method);
|
||||
payloadExpressionString = "#" + this.expressionSource.getReturnValueVariableName(method);
|
||||
}
|
||||
Expression expression = this.parser.parseExpression(payloadExpressionString);
|
||||
Object result = expression.getValue(context);
|
||||
|
||||
@@ -66,39 +66,47 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
|
||||
return this.getAnnotationValue(method, "headers", String[].class);
|
||||
}
|
||||
|
||||
public String[] getArgumentNames(Method method) {
|
||||
public String getMethodNameVariableName(Method method) {
|
||||
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
|
||||
if (annotation != null) {
|
||||
String name = annotation.argNames();
|
||||
if (StringUtils.hasText(name)) {
|
||||
return StringUtils.tokenizeToStringArray(name, ",");
|
||||
return annotation.methodNameVariableName();
|
||||
}
|
||||
return ExpressionSource.DEFAULT_METHOD_NAME_VARIABLE_NAME;
|
||||
}
|
||||
|
||||
public String[] getArgumentVariableNames(Method method) {
|
||||
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
|
||||
if (annotation != null) {
|
||||
String argNameList = annotation.argumentVariableNames();
|
||||
if (StringUtils.hasText(argNameList)) {
|
||||
return StringUtils.tokenizeToStringArray(argNameList, ",");
|
||||
}
|
||||
}
|
||||
return this.parameterNameDiscoverer.getParameterNames(method);
|
||||
}
|
||||
|
||||
public String getArgumentMapName(Method method) {
|
||||
public String getArgumentMapVariableName(Method method) {
|
||||
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
|
||||
if (annotation != null) {
|
||||
return annotation.argumentMapName();
|
||||
return annotation.argumentMapVariableName();
|
||||
}
|
||||
return ExpressionSource.DEFAULT_ARGUMENT_MAP_NAME;
|
||||
return ExpressionSource.DEFAULT_ARGUMENT_MAP_VARIABLE_NAME;
|
||||
}
|
||||
|
||||
public String getReturnValueName(Method method) {
|
||||
public String getReturnValueVariableName(Method method) {
|
||||
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
|
||||
if (annotation != null) {
|
||||
return annotation.returnValueName();
|
||||
return annotation.returnValueVariableName();
|
||||
}
|
||||
return ExpressionSource.DEFAULT_RETURN_VALUE_NAME;
|
||||
return ExpressionSource.DEFAULT_RETURN_VALUE_VARIABLE_NAME;
|
||||
}
|
||||
|
||||
public String getExceptionName(Method method) {
|
||||
public String getExceptionVariableName(Method method) {
|
||||
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
|
||||
if (annotation != null) {
|
||||
return annotation.exceptionName();
|
||||
return annotation.exceptionVariableName();
|
||||
}
|
||||
return ExpressionSource.DEFAULT_EXCEPTION_NAME;
|
||||
return ExpressionSource.DEFAULT_EXCEPTION_VARIABLE_NAME;
|
||||
}
|
||||
|
||||
public String getChannelName(Method method) {
|
||||
|
||||
@@ -37,13 +37,15 @@ public class MethodNameMappingExpressionSource implements ExpressionSource {
|
||||
|
||||
private volatile Map<String, String> channelMap = Collections.emptyMap();
|
||||
|
||||
private volatile Map<String, String[]> argNameMap;
|
||||
private volatile Map<String, String[]> argumentVariableNameMap;
|
||||
|
||||
private volatile String argumentMapName = ExpressionSource.DEFAULT_ARGUMENT_MAP_NAME;
|
||||
private volatile String methodNameVariableName = ExpressionSource.DEFAULT_METHOD_NAME_VARIABLE_NAME;
|
||||
|
||||
private volatile String returnValueName = ExpressionSource.DEFAULT_RETURN_VALUE_NAME;
|
||||
private volatile String argumentMapVariableName = ExpressionSource.DEFAULT_ARGUMENT_MAP_VARIABLE_NAME;
|
||||
|
||||
private volatile String exceptionName = ExpressionSource.DEFAULT_EXCEPTION_NAME;
|
||||
private volatile String returnValueVariableName = ExpressionSource.DEFAULT_RETURN_VALUE_VARIABLE_NAME;
|
||||
|
||||
private volatile String exceptionVariableName = ExpressionSource.DEFAULT_EXCEPTION_VARIABLE_NAME;
|
||||
|
||||
private final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
|
||||
|
||||
@@ -53,32 +55,41 @@ public class MethodNameMappingExpressionSource implements ExpressionSource {
|
||||
this.payloadExpressionMap = payloadExpressionMap;
|
||||
}
|
||||
|
||||
public void setArgumentMapName(String argumentMapName) {
|
||||
this.argumentMapName = argumentMapName;
|
||||
|
||||
public void setMethodNameVariableName(String methodNameVariableName) {
|
||||
this.methodNameVariableName = methodNameVariableName;
|
||||
}
|
||||
|
||||
public String getArgumentMapName(Method method) {
|
||||
return this.argumentMapName;
|
||||
public String getMethodNameVariableName(Method method) {
|
||||
return this.methodNameVariableName;
|
||||
}
|
||||
|
||||
public void setExceptionName(String exceptionName) {
|
||||
this.exceptionName = exceptionName;
|
||||
public void setArgumentMapVariableName(String argumentMapVariableName) {
|
||||
this.argumentMapVariableName = argumentMapVariableName;
|
||||
}
|
||||
|
||||
public String getExceptionName(Method method) {
|
||||
return this.exceptionName;
|
||||
public String getArgumentMapVariableName(Method method) {
|
||||
return this.argumentMapVariableName;
|
||||
}
|
||||
|
||||
public void setReturnValueName(String returnValueName) {
|
||||
this.returnValueName = returnValueName;
|
||||
public void setExceptionVariableName(String exceptionVariableName) {
|
||||
this.exceptionVariableName = exceptionVariableName;
|
||||
}
|
||||
|
||||
public String getReturnValueName(Method method) {
|
||||
return this.returnValueName;
|
||||
public String getExceptionVariableName(Method method) {
|
||||
return this.exceptionVariableName;
|
||||
}
|
||||
|
||||
public void setArgNameMap(Map<String, String[]> argNameMap) {
|
||||
this.argNameMap = argNameMap;
|
||||
public void setReturnValueVariableName(String returnValueVariableName) {
|
||||
this.returnValueVariableName = returnValueVariableName;
|
||||
}
|
||||
|
||||
public String getReturnValueVariableName(Method method) {
|
||||
return this.returnValueVariableName;
|
||||
}
|
||||
|
||||
public void setArgumentVariableNameMap(Map<String, String[]> argumentVariableNameMap) {
|
||||
this.argumentVariableNameMap = argumentVariableNameMap;
|
||||
}
|
||||
|
||||
public void setHeaderExpressionMap(Map<String, String[]> headerExpressionMap) {
|
||||
@@ -89,9 +100,9 @@ public class MethodNameMappingExpressionSource implements ExpressionSource {
|
||||
this.channelMap = channelMap;
|
||||
}
|
||||
|
||||
public String[] getArgumentNames(Method method) {
|
||||
if (this.argNameMap != null) {
|
||||
for (Map.Entry<String, String[]> entry : this.argNameMap.entrySet()) {
|
||||
public String[] getArgumentVariableNames(Method method) {
|
||||
if (this.argumentVariableNameMap != null) {
|
||||
for (Map.Entry<String, String[]> entry : this.argumentVariableNameMap.entrySet()) {
|
||||
if (PatternMatchUtils.simpleMatch(entry.getKey(), method.getName())) {
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
@@ -108,19 +108,23 @@ public class MessagePublishingInterceptorTests {
|
||||
|
||||
private static class TestExpressionSource implements ExpressionSource {
|
||||
|
||||
public String getArgumentMapName(Method method) {
|
||||
public String getMethodNameVariableName(Method method) {
|
||||
return "m";
|
||||
}
|
||||
|
||||
public String[] getArgumentNames(Method method) {
|
||||
public String getArgumentMapVariableName(Method method) {
|
||||
return "map";
|
||||
}
|
||||
|
||||
public String[] getArgumentVariableNames(Method method) {
|
||||
return new String[] { "a1", "a2"};
|
||||
}
|
||||
|
||||
public String getReturnValueName(Method method) {
|
||||
public String getReturnValueVariableName(Method method) {
|
||||
return "r";
|
||||
}
|
||||
|
||||
public String getExceptionName(Method method) {
|
||||
public String getExceptionVariableName(Method method) {
|
||||
return "x";
|
||||
}
|
||||
|
||||
|
||||
@@ -36,16 +36,16 @@ public class MethodAnnotationExpressionSourceTests {
|
||||
Method method = getMethod("methodWithExpressionAnnotationOnly", String.class, int.class);
|
||||
String expressionString = source.getPayloadExpression(method);
|
||||
assertEquals("testExpression1", expressionString);
|
||||
assertEquals(2, source.getArgumentNames(method).length);
|
||||
assertEquals("arg1", source.getArgumentNames(method)[0]);
|
||||
assertEquals("arg2", source.getArgumentNames(method)[1]);
|
||||
assertEquals(2, source.getArgumentVariableNames(method).length);
|
||||
assertEquals("arg1", source.getArgumentVariableNames(method)[0]);
|
||||
assertEquals("arg2", source.getArgumentVariableNames(method)[1]);
|
||||
String[] headerStrings = source.getHeaderExpressions(method);
|
||||
assertNotNull(headerStrings);
|
||||
assertEquals(1, headerStrings.length);
|
||||
assertEquals("", headerStrings[0]);
|
||||
assertEquals(ExpressionSource.DEFAULT_ARGUMENT_MAP_NAME, source.getArgumentMapName(method));
|
||||
assertEquals(ExpressionSource.DEFAULT_EXCEPTION_NAME, source.getExceptionName(method));
|
||||
assertEquals(ExpressionSource.DEFAULT_RETURN_VALUE_NAME, source.getReturnValueName(method));
|
||||
assertEquals(ExpressionSource.DEFAULT_ARGUMENT_MAP_VARIABLE_NAME, source.getArgumentMapVariableName(method));
|
||||
assertEquals(ExpressionSource.DEFAULT_EXCEPTION_VARIABLE_NAME, source.getExceptionVariableName(method));
|
||||
assertEquals(ExpressionSource.DEFAULT_RETURN_VALUE_VARIABLE_NAME, source.getReturnValueVariableName(method));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -53,12 +53,12 @@ public class MethodAnnotationExpressionSourceTests {
|
||||
Method method = getMethod("methodWithExpressionBinding", String.class, int.class);
|
||||
String expressionString = source.getPayloadExpression(method);
|
||||
assertEquals("testExpression2", expressionString);
|
||||
assertEquals(2, source.getArgumentNames(method).length);
|
||||
assertEquals("s", source.getArgumentNames(method)[0]);
|
||||
assertEquals("i", source.getArgumentNames(method)[1]);
|
||||
assertEquals("argz", source.getArgumentMapName(method));
|
||||
assertEquals("x", source.getExceptionName(method));
|
||||
assertEquals("result", source.getReturnValueName(method));
|
||||
assertEquals(2, source.getArgumentVariableNames(method).length);
|
||||
assertEquals("s", source.getArgumentVariableNames(method)[0]);
|
||||
assertEquals("i", source.getArgumentVariableNames(method)[1]);
|
||||
assertEquals("argz", source.getArgumentMapVariableName(method));
|
||||
assertEquals("x", source.getExceptionVariableName(method));
|
||||
assertEquals("result", source.getReturnValueVariableName(method));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,7 +88,8 @@ public class MethodAnnotationExpressionSourceTests {
|
||||
}
|
||||
|
||||
@Publisher("testExpression2")
|
||||
@ExpressionBinding(argNames="s, i", argumentMapName="argz", exceptionName="x", returnValueName="result")
|
||||
@ExpressionBinding(argumentVariableNames="s, i", argumentMapVariableName="argz",
|
||||
exceptionVariableName="x", returnValueVariableName="result")
|
||||
public void methodWithExpressionBinding(String arg1, int arg2) {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user