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

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

@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);
}
 
源代码2 项目: Milkomeda   文件: ELContext.java
/**
 * 根据类的反射信息,获取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);
}
 
@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);
}
 
@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);
}
 
@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;
	}
}
 
源代码6 项目: mica   文件: RedisRateLimiterAspect.java
/**
 * 计算参数表达式
 *
 * @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);
}
 
/**
 * 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)));
}
 
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);
}
 
源代码9 项目: spring-analysis-note   文件: CacheAspectSupport.java
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;
}
 
源代码10 项目: Milkomeda   文件: ExpressionEvaluator.java
/**
 * 获取并缓存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;
}
 
/**
 * 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)));
}
 
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);
}
 
源代码13 项目: java-technology-stack   文件: CacheAspectSupport.java
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;
}
 
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;
    }
 
源代码15 项目: Limiter   文件: LimiterExecutionContext.java
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();

}
 
源代码16 项目: 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;
}
 
源代码17 项目: lams   文件: EventExpressionEvaluator.java
/**
 * 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);
}
 
源代码18 项目: lams   文件: EventExpressionEvaluator.java
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;
}
 
源代码19 项目: lams   文件: ApplicationListenerMethodAdapter.java
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);
}
 
源代码20 项目: lams   文件: CacheAspectSupport.java
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);
}
 
源代码21 项目: lams   文件: CacheOperationExpressionEvaluator.java
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;
}
 
/**
 * 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);
}
 
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;
}
 
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);
}
 
源代码25 项目: spring4-understanding   文件: CacheAspectSupport.java
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);
}
 
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;
}
 
源代码27 项目: mica   文件: MicaExpressionEvaluator.java
@Nullable
public Object eval(String expression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return eval(expression, methodKey, evalContext, null);
}
 
源代码28 项目: mica   文件: MicaExpressionEvaluator.java
@Nullable
public <T> T eval(String expression, AnnotatedElementKey methodKey, EvaluationContext evalContext, @Nullable Class<T> valueType) {
	return getExpression(this.expressionCache, methodKey, expression).getValue(evalContext, valueType);
}
 
源代码29 项目: mica   文件: MicaExpressionEvaluator.java
@Nullable
public String evalAsText(String expression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return eval(expression, methodKey, evalContext, String.class);
}
 
源代码30 项目: mica   文件: MicaExpressionEvaluator.java
public boolean evalAsBool(String expression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
	return Boolean.TRUE.equals(eval(expression, methodKey, evalContext, Boolean.class));
}
 
 同包方法