org.springframework.context.expression.MethodBasedEvaluationContext Java Examples

The following examples show how to use org.springframework.context.expression.MethodBasedEvaluationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExpressionEvaluator.java    From Milkomeda with MIT License 6 votes vote down vote up
/**
 * 根据方法创建一个 {@link EvaluationContext}
 * @param object        目标对象
 * @param targetClass   目标类型
 * @param method        方法
 * @param args          参数
 * @return  EvaluationContext
 */
public StandardEvaluationContext createEvaluationContext(Object object, Class<?> targetClass,
                                                         Method method, Object[] args) {
    Method targetMethod = getTargetMethod(targetClass, method);
    // 创建自定义EL Root
    ExpressionRootObject root = new ExpressionRootObject(object, args);
    // 创建基于方法的执行上下文
    MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(root, targetMethod, args, this.paramNameDiscoverer);
    // 添加变量引用
    Environment env = ApplicationContextHolder.getEnvironment();
    if (env != null) {
        evaluationContext.setVariable("env", env.getProperties());
    }
    evaluationContext.setVariable("target", object);
    ServletRequestAttributes requestAttributes = WebContext.getRequestAttributes();
    if (requestAttributes != null) {
        evaluationContext.setVariable("request", requestAttributes.getRequest());
        evaluationContext.setVariable("reqParams", requestAttributes.getRequest().getParameterMap());
    }
    return evaluationContext;
}
 
Example #2
Source File: SpelUtil.java    From mall4j with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 支持 #p0 参数索引的表达式解析
 * @param rootObject 根对象,method 所在的对象
 * @param spel 表达式
 * @param method ,目标方法
 * @param args 方法入参
 * @return 解析后的字符串
 */
public static String parse(Object rootObject,String spel, Method method, Object[] args) {
    if (StrUtil.isBlank(spel)) {
        return StrUtil.EMPTY;
    }
    //获取被拦截方法参数名列表(使用Spring支持类库)
    LocalVariableTableParameterNameDiscoverer u =
            new LocalVariableTableParameterNameDiscoverer();
    String[] paraNameArr = u.getParameterNames(method);
    if (ArrayUtil.isEmpty(paraNameArr)) {
        return spel;
    }
    //使用SPEL进行key的解析
    ExpressionParser parser = new SpelExpressionParser();
    //SPEL上下文
    StandardEvaluationContext context = new MethodBasedEvaluationContext(rootObject,method,args,u);
    //把方法参数放入SPEL上下文中
    for (int i = 0; i < paraNameArr.length; i++) {
        context.setVariable(paraNameArr[i], args[i]);
    }
    return parser.parseExpression(spel).getValue(context, String.class);
}
 
Example #3
Source File: SpelResolver.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
public String resolve(Method method, Object[] arguments, String spelExpression) {
    if (StringUtils.isEmpty(spelExpression)) {
        return spelExpression;
    }

    if (spelExpression.matches(BEAN_SPEL_REGEX) && stringValueResolver != null) {
        return stringValueResolver.resolveStringValue(spelExpression);
    }

    if (spelExpression.matches(METHOD_SPEL_REGEX)) {
        SpelRootObject rootObject = new SpelRootObject(method, arguments);
        MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, method, arguments, parameterNameDiscoverer);
        Object evaluated = expressionParser.parseExpression(spelExpression).getValue(evaluationContext);

        return (String) evaluated;
    }

    return spelExpression;
}
 
Example #4
Source File: MicaExpressionEvaluator.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create an {@link EvaluationContext}.
 *
 * @param method      the method
 * @param args        the method arguments
 * @param target      the target object
 * @param targetClass the target class
 * @return the evaluation context
 */
public EvaluationContext createContext(Method method, Object[] args, Object target, Class<?> targetClass, @Nullable BeanFactory beanFactory) {
	Method targetMethod = getTargetMethod(targetClass, method);
	MicaExpressionRootObject rootObject = new MicaExpressionRootObject(method, args, target, targetClass, targetMethod);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #5
Source File: EventExpressionEvaluator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine if the condition defined by the specified expression evaluates
 * to {@code true}.
 */
public boolean condition(String conditionExpression, ApplicationEvent event, Method targetMethod,
		AnnotatedElementKey methodKey, Object[] args, @Nullable BeanFactory beanFactory) {

	EventExpressionRootObject root = new EventExpressionRootObject(event, args);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(
			root, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}

	return (Boolean.TRUE.equals(getExpression(this.conditionCache, methodKey, conditionExpression).getValue(
			evaluationContext, Boolean.class)));
}
 
Example #6
Source File: ExpressionEvaluator.java    From magic-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create an {@link EvaluationContext}.
 *
 * @param method      the method
 * @param args        the method arguments
 * @param target      the target object
 * @param targetClass the target class
 * @return the evaluation context
 */
public EvaluationContext createContext(Method method, Object[] args, Object target, Class<?> targetClass, @Nullable BeanFactory beanFactory) {
	Method targetMethod = getTargetMethod(targetClass, method);
	ExpressionRootObject rootObject = new ExpressionRootObject(method, args, target, targetClass, targetMethod);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #7
Source File: EventExpressionEvaluator.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Specify if the condition defined by the specified expression matches.
 */
public boolean condition(String conditionExpression, ApplicationEvent event, Method targetMethod,
		AnnotatedElementKey methodKey, Object[] args, @Nullable BeanFactory beanFactory) {

	EventExpressionRootObject root = new EventExpressionRootObject(event, args);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(
			root, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}

	return (Boolean.TRUE.equals(getExpression(this.conditionCache, methodKey, conditionExpression).getValue(
			evaluationContext, Boolean.class)));
}
 
Example #8
Source File: BusinessKeyProvider.java    From spring-boot-klock-starter with Apache License 2.0 5 votes vote down vote up
private List<String> getSpelDefinitionKey(String[] definitionKeys, Method method, Object[] parameterValues) {
    List<String> definitionKeyList = new ArrayList<>();
    for (String definitionKey : definitionKeys) {
        if (!ObjectUtils.isEmpty(definitionKey)) {
            EvaluationContext context = new MethodBasedEvaluationContext(null, method, parameterValues, nameDiscoverer);
            Object objKey = parser.parseExpression(definitionKey).getValue(context);
            definitionKeyList.add(ObjectUtils.nullSafeToString(objKey));
        }
    }
    return definitionKeyList;
}
 
Example #9
Source File: LimiterOperationExpressionEvaluator.java    From Limiter with Apache License 2.0 5 votes vote down vote up
public EvaluationContext createEvaluationContext(Limiter limiter, Method method, Object[] args, Object target, Class<?> targetClass, Method targetMethod,
                                                 Map<String, Object> injectArgs, BeanFactory beanFactory) {

    LimiterExpressionRootObject rootObject = new LimiterExpressionRootObject(limiter, method, args, target, targetClass);
    MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, targetMethod, args, this.parameterNameDiscoverer);
    for (String key : injectArgs.keySet()) {
        evaluationContext.setVariable(key, injectArgs.get(key));
    }

    if (beanFactory != null) {
        evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
    }
    return evaluationContext;
}
 
Example #10
Source File: ActivityAdvice.java    From retro-game with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) {
  var parser = new SpelExpressionParser();
  var context = new MethodBasedEvaluationContext(target, method, args, parameterNameDiscoverer);
  var bodies = AnnotationUtils.findAnnotation(method, Activity.class).bodies();
  for (var body : bodies) {
    var bodyId = parser.parseExpression(body).getValue(context, Long.class);
    activityService.handleBodyActivity(bodyId, null);
  }

  // The annotation is meant to update the current user activity.
  var userId = CustomUser.getCurrentUserId();
  activityService.handleUserActivity(userId);
}
 
Example #11
Source File: SpelKeyGenerator.java    From distributed-lock with MIT License 5 votes vote down vote up
private Object evaluateExpression(final String expression, final Object object, final Method method, final Object[] args) {
  final EvaluationContext context = new MethodBasedEvaluationContext(object, method, args, super.getParameterNameDiscoverer());
  context.setVariable("executionPath", object.getClass().getCanonicalName() + "." + method.getName());

  final Expression evaluatedExpression = getExpression(this.conditionCache, new AnnotatedElementKey(method, object.getClass()), expression);
  final Object expressionValue = evaluatedExpression.getValue(context);
  if (expressionValue == null) {
    throw new EvaluationConvertException("Expression evaluated in a null");
  }

  return expressionValue;
}
 
Example #12
Source File: EventExpressionEvaluator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create the suitable {@link EvaluationContext} for the specified event handling
 * on the specified method.
 */
public EvaluationContext createEvaluationContext(ApplicationEvent event, Class<?> targetClass,
		Method method, Object[] args, BeanFactory beanFactory) {

	Method targetMethod = getTargetMethod(targetClass, method);
	EventExpressionRootObject root = new EventExpressionRootObject(event, args);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(
			root, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #13
Source File: EventExpressionEvaluator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create the suitable {@link EvaluationContext} for the specified event handling
 * on the specified method.
 */
public EvaluationContext createEvaluationContext(ApplicationEvent event, Class<?> targetClass,
		Method method, Object[] args) {

	Method targetMethod = getTargetMethod(targetClass, method);
	EventExpressionRootObject root = new EventExpressionRootObject(event, args);
	return new MethodBasedEvaluationContext(root, targetMethod, args, this.paramNameDiscoverer);
}
 
Example #14
Source File: MicaExpressionEvaluator.java    From mica with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Create an {@link EvaluationContext}.
 *
 * @param method      the method
 * @param args        the method arguments
 * @param rootObject  rootObject
 * @param targetClass the target class
 * @return the evaluation context
 */
public EvaluationContext createContext(Method method, Object[] args, Class<?> targetClass, Object rootObject, @Nullable BeanFactory beanFactory) {
	Method targetMethod = getTargetMethod(targetClass, method);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #15
Source File: ExpressionEvaluator.java    From magic-starter with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Create an {@link EvaluationContext}.
 *
 * @param method      the method
 * @param args        the method arguments
 * @param rootObject  rootObject
 * @param targetClass the target class
 * @return the evaluation context
 */
public EvaluationContext createContext(Method method, Object[] args, Class<?> targetClass, Object rootObject, @Nullable BeanFactory beanFactory) {
	Method targetMethod = getTargetMethod(targetClass, method);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}