com.xxelin.whale.utils.SpelUtils Maven / Gradle / Ivy
package com.xxelin.whale.utils;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import java.lang.reflect.Method;
/**
* @author ElinZhou [email protected]
* @version $Id: SpelUtils.java , v 0.1 2019-08-07 15:39 ElinZhou Exp $
*/
public class SpelUtils {
private SpelUtils() {
throw new IllegalStateException("cant instance");
}
public static T parse(String spel, Class returnType, Object object, Method method, Object[] args) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(spel);
LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer =
new LocalVariableTableParameterNameDiscoverer();
String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);
MethodBasedEvaluationContext context = new MethodBasedEvaluationContext(object,
method, args, parameterNameDiscoverer);
if (parameterNames != null) {
//在一些情况下,会无法获取的实际的参数名称
for (int i = 0; i < args.length; i++) {
context.setVariable(parameterNames[i], args[i]);
}
}
for (int i = 0; i < args.length; i++) {
context.setVariable("arg" + i, args[i]);
}
return expression.getValue(context, returnType);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy