org.springframework.context.expression.AnnotatedElementKey Java Examples

The following examples show how to use org.springframework.context.expression.AnnotatedElementKey. 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: ExpressionEvaluatorTests.java    From java-technology-stack with 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 File: AbstractFallbackJCacheOperationSource.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: ExpressionEvaluatorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: ELContext.java    From Milkomeda with 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 #5
Source File: ExpressionEvaluatorTests.java    From spring-analysis-note with 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 #6
Source File: CacheOperationExpressionEvaluator.java    From lams with GNU General Public License v2.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);
		this.targetMethodCache.put(methodKey, targetMethod);
	}
	return targetMethod;
}
 
Example #7
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 #8
Source File: LimiterExecutionContext.java    From Limiter with 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 #9
Source File: EventExpressionEvaluator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #10
Source File: LimiterOperationExpressionEvaluator.java    From Limiter with Apache License 2.0 5 votes vote down vote up
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 #11
Source File: EventExpressionEvaluator.java    From lams with GNU General Public License v2.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);
		this.targetMethodCache.put(methodKey, targetMethod);
	}
	return targetMethod;
}
 
Example #12
Source File: ApplicationListenerMethodAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 #13
Source File: CacheAspectSupport.java    From lams with 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 #14
Source File: RedisRateLimiterAspect.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 计算参数表达式
 *
 * @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 #15
Source File: CacheAspectSupport.java    From java-technology-stack with 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 #16
Source File: ApplicationListenerMethodAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 #17
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 #18
Source File: EventExpressionEvaluator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #19
Source File: EventExpressionEvaluator.java    From spring4-understanding with 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 #20
Source File: CacheAspectSupport.java    From spring4-understanding with 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 #21
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 #22
Source File: ApplicationListenerMethodAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 #23
Source File: CacheAspectSupport.java    From spring-analysis-note with 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 #24
Source File: ExpressionEvaluator.java    From spring4-understanding with 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 #25
Source File: ExpressionEvaluator.java    From Milkomeda with 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 #26
Source File: ApplicationListenerMethodAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 #27
Source File: ExpressionEvaluator.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public Object key(String keyExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return getExpression(this.keyCache, methodKey, keyExpression).getValue(evalContext);
}
 
Example #28
Source File: CacheOperationExpressionEvaluator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public boolean unless(String unlessExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return getExpression(this.unlessCache, methodKey, unlessExpression).getValue(evalContext, boolean.class);
}
 
Example #29
Source File: CacheOperationExpressionEvaluator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public boolean condition(String conditionExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return getExpression(this.conditionCache, methodKey, conditionExpression).getValue(evalContext, boolean.class);
}
 
Example #30
Source File: CacheOperationExpressionEvaluator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Object key(String keyExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return getExpression(this.keyCache, methodKey, keyExpression).getValue(evalContext);
}