类org.springframework.context.expression.MethodBasedEvaluationContext源码实例Demo

下面列出了怎么用org.springframework.context.expression.MethodBasedEvaluationContext的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: Milkomeda   文件: ExpressionEvaluator.java
/**
 * 根据方法创建一个 {@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;
}
 
源代码2 项目: mall4j   文件: SpelUtil.java
/**
 * 支持 #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);
}
 
源代码3 项目: resilience4j   文件: SpelResolver.java
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;
}
 
源代码4 项目: mica   文件: MicaExpressionEvaluator.java
/**
 * 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;
}
 
/**
 * 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)));
}
 
源代码6 项目: magic-starter   文件: ExpressionEvaluator.java
/**
 * 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;
}
 
/**
 * 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)));
}
 
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;
}
 
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;
}
 
源代码10 项目: retro-game   文件: ActivityAdvice.java
@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);
}
 
源代码11 项目: distributed-lock   文件: SpelKeyGenerator.java
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;
}
 
源代码12 项目: lams   文件: EventExpressionEvaluator.java
/**
 * 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;
}
 
/**
 * 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);
}
 
源代码14 项目: mica   文件: MicaExpressionEvaluator.java
/**
 * 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;
}
 
源代码15 项目: magic-starter   文件: ExpressionEvaluator.java
/**
 * 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;
}
 
 类方法
 同包方法