Java Code Examples for org.springframework.context.expression.AnnotatedElementKey

The following examples show how to use org.springframework.context.expression.AnnotatedElementKey. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: ExpressionEvaluatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testMultipleCachingEval() {
	AnnotatedClass target = new AnnotatedClass();
	Method method = ReflectionUtils.findMethod(
			AnnotatedClass.class, "multipleCaching", Object.class, Object.class);
	Object[] args = new Object[] {new Object(), new Object()};
	Collection<ConcurrentMapCache> caches = Collections.singleton(new ConcurrentMapCache("test"));

	EvaluationContext evalCtx = this.eval.createEvaluationContext(caches, method, args,
			target, target.getClass(), method, CacheOperationExpressionEvaluator.NO_RESULT, null);
	Collection<CacheOperation> ops = getOps("multipleCaching");

	Iterator<CacheOperation> it = ops.iterator();
	AnnotatedElementKey key = new AnnotatedElementKey(method, AnnotatedClass.class);

	Object keyA = this.eval.key(it.next().getKey(), key, evalCtx);
	Object keyB = this.eval.key(it.next().getKey(), key, evalCtx);

	assertEquals(args[0], keyA);
	assertEquals(args[1], keyB);
}
 
Example 2
Source Project: Milkomeda   Source File: ELContext.java    License: MIT License 6 votes vote down vote up
/**
 * 根据类的反射信息,获取EL表达式的值
 * @param object            目标对象
 * @param args              参数
 * @param clazz             目标类型
 * @param method            方法
 * @param condition         el条件表达式
 * @param desiredResultType 返回类型
 * @param <T>   返回类型
 * @return  解析的值
 */
public static <T> T getValue(Object object, Object[] args, Class<?> clazz, Method method,
                              String condition, Class<T> desiredResultType) {
    if (args == null) {
        return null;
    }
    // EL表达式执行器
    ExpressionEvaluator<T> evaluator = new ExpressionEvaluator<>();
    // 创建AOP方法的执行上下文
    StandardEvaluationContext evaluationContext =
            evaluator.createEvaluationContext(object, clazz, method, args);
    // 设置Bean工厂解析器
    evaluationContext.setBeanResolver(beanFactoryResolver);
    AnnotatedElementKey methodKey = new AnnotatedElementKey(method, clazz);
    return evaluator.condition(condition, methodKey, evaluationContext, desiredResultType);
}
 
Example 3
Source Project: java-technology-stack   Source File: ExpressionEvaluatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testMultipleCachingEval() {
	AnnotatedClass target = new AnnotatedClass();
	Method method = ReflectionUtils.findMethod(
			AnnotatedClass.class, "multipleCaching", Object.class, Object.class);
	Object[] args = new Object[] {new Object(), new Object()};
	Collection<ConcurrentMapCache> caches = Collections.singleton(new ConcurrentMapCache("test"));

	EvaluationContext evalCtx = this.eval.createEvaluationContext(caches, method, args,
			target, target.getClass(), method, CacheOperationExpressionEvaluator.NO_RESULT, null);
	Collection<CacheOperation> ops = getOps("multipleCaching");

	Iterator<CacheOperation> it = ops.iterator();
	AnnotatedElementKey key = new AnnotatedElementKey(method, AnnotatedClass.class);

	Object keyA = this.eval.key(it.next().getKey(), key, evalCtx);
	Object keyB = this.eval.key(it.next().getKey(), key, evalCtx);

	assertEquals(args[0], keyA);
	assertEquals(args[1], keyB);
}
 
Example 4
@Test
public void testMultipleCachingEval() throws Exception {
	AnnotatedClass target = new AnnotatedClass();
	Method method = ReflectionUtils.findMethod(AnnotatedClass.class, "multipleCaching", Object.class,
			Object.class);
	Object[] args = new Object[] { new Object(), new Object() };
	Collection<ConcurrentMapCache> caches = Collections.singleton(new ConcurrentMapCache("test"));

	EvaluationContext evalCtx = eval.createEvaluationContext(caches, method, args, target, target.getClass());
	Collection<CacheOperation> ops = getOps("multipleCaching");

	Iterator<CacheOperation> it = ops.iterator();

	AnnotatedElementKey key = new AnnotatedElementKey(method, AnnotatedClass.class);

	Object keyA = eval.key(it.next().getKey(), key, evalCtx);
	Object keyB = eval.key(it.next().getKey(), key, evalCtx);

	assertEquals(args[0], keyA);
	assertEquals(args[1], keyB);
}
 
Example 5
@Override
public JCacheOperation<?> getCacheOperation(Method method, Class<?> targetClass) {
	Object cacheKey = new AnnotatedElementKey(method, targetClass);
	Object cached = this.cache.get(cacheKey);

	if (cached != null) {
		return (cached != NULL_CACHING_ATTRIBUTE ? (JCacheOperation<?>) cached : null);
	}
	else {
		JCacheOperation<?> operation = computeCacheOperation(method, targetClass);
		if (operation != null) {
			if (logger.isDebugEnabled()) {
				logger.debug("Adding cacheable method '" + method.getName() + "' with operation: " + operation);
			}
			this.cache.put(cacheKey, operation);
		}
		else {
			this.cache.put(cacheKey, NULL_CACHING_ATTRIBUTE);
		}
		return operation;
	}
}
 
Example 6
/**
 * 计算参数表达式
 *
 * @param point      ProceedingJoinPoint
 * @param limitParam limitParam
 * @return 结果
 */
private String evalLimitParam(ProceedingJoinPoint point, String limitParam) {
	MethodSignature ms = (MethodSignature) point.getSignature();
	Method method = ms.getMethod();
	Object[] args = point.getArgs();
	Object target = point.getTarget();
	Class<?> targetClass = target.getClass();
	EvaluationContext context = evaluator.createContext(method, args, target, targetClass, applicationContext);
	AnnotatedElementKey elementKey = new AnnotatedElementKey(method, targetClass);
	return evaluator.evalAsText(limitParam, elementKey, context);
}
 
Example 7
Source Project: spring-analysis-note   Source File: EventExpressionEvaluator.java    License: 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 8
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
	this.beanName = beanName;
	this.method = BridgeMethodResolver.findBridgedMethod(method);
	this.targetMethod = (!Proxy.isProxyClass(targetClass) ?
			AopUtils.getMostSpecificMethod(method, targetClass) : this.method);
	this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass);

	EventListener ann = AnnotatedElementUtils.findMergedAnnotation(this.targetMethod, EventListener.class);
	this.declaredEventTypes = resolveDeclaredEventTypes(method, ann);
	this.condition = (ann != null ? ann.condition() : null);
	this.order = resolveOrder(this.targetMethod);
}
 
Example 9
Source Project: spring-analysis-note   Source File: CacheAspectSupport.java    License: MIT License 5 votes vote down vote up
public CacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass,
		KeyGenerator keyGenerator, CacheResolver cacheResolver) {

	this.operation = operation;
	this.method = BridgeMethodResolver.findBridgedMethod(method);
	this.targetClass = targetClass;
	this.targetMethod = (!Proxy.isProxyClass(targetClass) ?
			AopUtils.getMostSpecificMethod(method, targetClass) : this.method);
	this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass);
	this.keyGenerator = keyGenerator;
	this.cacheResolver = cacheResolver;
}
 
Example 10
Source Project: Milkomeda   Source File: ExpressionEvaluator.java    License: MIT License 5 votes vote down vote up
/**
 * 获取并缓存Method
 * @param targetClass   目标类
 * @param method        目标方法
 * @return  Method
 */
private Method getTargetMethod(Class<?> targetClass, Method method) {
    AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass);
    Method targetMethod = this.targetMethodCache.get(methodKey);
    if (targetMethod == null) {
        targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
        this.targetMethodCache.put(methodKey, targetMethod);
    }
    return targetMethod;
}
 
Example 11
Source Project: java-technology-stack   Source File: EventExpressionEvaluator.java    License: 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 12
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
	this.beanName = beanName;
	this.method = BridgeMethodResolver.findBridgedMethod(method);
	this.targetMethod = (!Proxy.isProxyClass(targetClass) ?
			AopUtils.getMostSpecificMethod(method, targetClass) : this.method);
	this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass);

	EventListener ann = AnnotatedElementUtils.findMergedAnnotation(this.targetMethod, EventListener.class);
	this.declaredEventTypes = resolveDeclaredEventTypes(method, ann);
	this.condition = (ann != null ? ann.condition() : null);
	this.order = resolveOrder(this.targetMethod);
}
 
Example 13
Source Project: java-technology-stack   Source File: CacheAspectSupport.java    License: MIT License 5 votes vote down vote up
public CacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass,
		KeyGenerator keyGenerator, CacheResolver cacheResolver) {

	this.operation = operation;
	this.method = BridgeMethodResolver.findBridgedMethod(method);
	this.targetClass = targetClass;
	this.targetMethod = (!Proxy.isProxyClass(targetClass) ?
			AopUtils.getMostSpecificMethod(method, targetClass) : this.method);
	this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass);
	this.keyGenerator = keyGenerator;
	this.cacheResolver = cacheResolver;
}
 
Example 14
protected Expression getExpression(AnnotatedElementKey elementKey, String expression) {

        ExpressionKey expressionKey = new ExpressionKey(elementKey, expression);
        Expression expr = keyCache.get(expressionKey);
        if (expr == null) {
            expr = this.parser.parseExpression(expression);
            keyCache.put(expressionKey, expr);
        }
        return expr;
    }
 
Example 15
Source Project: Limiter   Source File: LimiterExecutionContext.java    License: Apache License 2.0 5 votes vote down vote up
private Object generateKey() {
    if (StringUtils.hasText(this.metadata.getLimitedResource().getKey())) {
        EvaluationContext evaluationContext = evaluator.createEvaluationContext(this.metadata.getLimiter(), this.metadata.getTargetMethod(), this.args,
                this.target, this.metadata.getTargetClass(), this.metadata.getTargetMethod(), injectArgs, beanFactory);
        Object evalKey = evaluator.key(this.metadata.getLimitedResource().getKey(), new AnnotatedElementKey(this.metadata.getTargetMethod(), this.metadata.getTargetClass()), evaluationContext);
        Assert.notNull(evalKey, "key值计算为null!");
        return evalKey;
    }
    return this.metadata.getTargetClass().getName() + "#" +
            this.metadata.getTargetMethod().getName();

}
 
Example 16
Source Project: distributed-lock   Source File: SpelKeyGenerator.java    License: 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 17
/**
 * Specify if the condition defined by the specified expression matches.
 */
public boolean condition(String conditionExpression,
		AnnotatedElementKey elementKey, EvaluationContext evalContext) {

	return getExpression(this.conditionCache, elementKey, conditionExpression).getValue(
			evalContext, boolean.class);
}
 
Example 18
private Method getTargetMethod(Class<?> targetClass, Method method) {
	AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass);
	Method targetMethod = this.targetMethodCache.get(methodKey);
	if (targetMethod == null) {
		targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
		this.targetMethodCache.put(methodKey, targetMethod);
	}
	return targetMethod;
}
 
Example 19
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
	this.beanName = beanName;
	this.method = method;
	this.targetClass = targetClass;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);

	Method targetMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
	EventListener ann = AnnotatedElementUtils.findMergedAnnotation(targetMethod, EventListener.class);

	this.declaredEventTypes = resolveDeclaredEventTypes(method, ann);
	this.condition = (ann != null ? ann.condition() : null);
	this.order = resolveOrder(method);

	this.methodKey = new AnnotatedElementKey(method, targetClass);
}
 
Example 20
Source Project: lams   Source File: CacheAspectSupport.java    License: GNU General Public License v2.0 5 votes vote down vote up
public CacheOperationContext(CacheOperationMetadata metadata, Object[] args, Object target) {
	this.metadata = metadata;
	this.args = extractArgs(metadata.method, args);
	this.target = target;
	this.caches = CacheAspectSupport.this.getCaches(this, metadata.cacheResolver);
	this.cacheNames = createCacheNames(this.caches);
	this.methodCacheKey = new AnnotatedElementKey(metadata.method, metadata.targetClass);
}
 
Example 21
private Method getTargetMethod(Class<?> targetClass, Method method) {
	AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass);
	Method targetMethod = this.targetMethodCache.get(methodKey);
	if (targetMethod == null) {
		targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
		this.targetMethodCache.put(methodKey, targetMethod);
	}
	return targetMethod;
}
 
Example 22
/**
 * Specify if the condition defined by the specified expression matches.
 */
public boolean condition(String conditionExpression,
		AnnotatedElementKey elementKey, EvaluationContext evalContext) {

	return getExpression(this.conditionCache, elementKey, conditionExpression)
			.getValue(evalContext, boolean.class);
}
 
Example 23
private Method getTargetMethod(Class<?> targetClass, Method method) {
	AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass);
	Method targetMethod = this.targetMethodCache.get(methodKey);
	if (targetMethod == null) {
		targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
		if (targetMethod == null) {
			targetMethod = method;
		}
		this.targetMethodCache.put(methodKey, targetMethod);
	}
	return targetMethod;
}
 
Example 24
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
	this.beanName = beanName;
	this.method = method;
	this.targetClass = targetClass;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
	this.declaredEventTypes = resolveDeclaredEventTypes();
	this.methodKey = new AnnotatedElementKey(this.method, this.targetClass);
}
 
Example 25
Source Project: spring4-understanding   Source File: CacheAspectSupport.java    License: Apache License 2.0 5 votes vote down vote up
public CacheOperationContext(CacheOperationMetadata metadata, Object[] args, Object target) {
	this.metadata = metadata;
	this.args = extractArgs(metadata.method, args);
	this.target = target;
	this.caches = CacheAspectSupport.this.getCaches(this, metadata.cacheResolver);
	this.cacheNames = createCacheNames(this.caches);
	this.methodCacheKey = new AnnotatedElementKey(metadata.method, metadata.targetClass);
}
 
Example 26
Source Project: spring4-understanding   Source File: ExpressionEvaluator.java    License: Apache License 2.0 5 votes vote down vote up
private Method getTargetMethod(Class<?> targetClass, Method method) {
	AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass);
	Method targetMethod = this.targetMethodCache.get(methodKey);
	if (targetMethod == null) {
		targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
		if (targetMethod == null) {
			targetMethod = method;
		}
		this.targetMethodCache.put(methodKey, targetMethod);
	}
	return targetMethod;
}
 
Example 27
@Nullable
public Object eval(String expression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return eval(expression, methodKey, evalContext, null);
}
 
Example 28
@Nullable
public <T> T eval(String expression, AnnotatedElementKey methodKey, EvaluationContext evalContext, @Nullable Class<T> valueType) {
	return getExpression(this.expressionCache, methodKey, expression).getValue(evalContext, valueType);
}
 
Example 29
@Nullable
public String evalAsText(String expression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return eval(expression, methodKey, evalContext, String.class);
}
 
Example 30
public boolean evalAsBool(String expression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return Boolean.TRUE.equals(eval(expression, methodKey, evalContext, Boolean.class));
}