Java Code Examples for org.springframework.aop.support.AopUtils#getMostSpecificMethod()

The following examples show how to use org.springframework.aop.support.AopUtils#getMostSpecificMethod() . 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: SpanTagAnnotationHandler.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
void addAnnotatedParameters(MethodInvocation pjp) {
	try {
		Method method = pjp.getMethod();
		Method mostSpecificMethod = AopUtils.getMostSpecificMethod(method,
				pjp.getThis().getClass());
		List<SleuthAnnotatedParameter> annotatedParameters = SleuthAnnotationUtils
				.findAnnotatedParameters(mostSpecificMethod, pjp.getArguments());
		getAnnotationsFromInterfaces(pjp, mostSpecificMethod, annotatedParameters);
		mergeAnnotatedMethodsIfNecessary(pjp, method, mostSpecificMethod,
				annotatedParameters);
		addAnnotatedArguments(annotatedParameters);
	}
	catch (SecurityException ex) {
		log.error("Exception occurred while trying to add annotated parameters", ex);
	}
}
 
Example 2
Source File: AbstractFallbackJCacheOperationSource.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private JCacheOperation<?> computeCacheOperation(Method method, @Nullable Class<?> targetClass) {
	// Don't allow no-public methods as required.
	if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
		return null;
	}

	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);

	// First try is the method in the target class.
	JCacheOperation<?> operation = findCacheOperation(specificMethod, targetClass);
	if (operation != null) {
		return operation;
	}
	if (specificMethod != method) {
		// Fallback is to look at the original method.
		operation = findCacheOperation(method, targetClass);
		if (operation != null) {
			return operation;
		}
	}
	return null;
}
 
Example 3
Source File: AspectJExpressionPointcut.java    From java-technology-stack with MIT License 6 votes vote down vote up
private ShadowMatch getTargetShadowMatch(Method method, Class<?> targetClass) {
	Method targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
	if (targetMethod.getDeclaringClass().isInterface()) {
		// Try to build the most specific interface possible for inherited methods to be
		// considered for sub-interface matches as well, in particular for proxy classes.
		// Note: AspectJ is only going to take Method.getDeclaringClass() into account.
		Set<Class<?>> ifcs = ClassUtils.getAllInterfacesForClassAsSet(targetClass);
		if (ifcs.size() > 1) {
			try {
				Class<?> compositeInterface = ClassUtils.createCompositeInterface(
						ClassUtils.toClassArray(ifcs), targetClass.getClassLoader());
				targetMethod = ClassUtils.getMostSpecificMethod(targetMethod, compositeInterface);
			}
			catch (IllegalArgumentException ex) {
				// Implemented interfaces probably expose conflicting method signatures...
				// Proceed with original target method.
			}
		}
	}
	return getShadowMatch(targetMethod, method);
}
 
Example 4
Source File: AnnotationMethodMatcher.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean matches(Method method, Class<?> targetClass) {
	if (matchesMethod(method)) {
		return true;
	}
	// Proxy classes never have annotations on their redeclared methods.
	if (Proxy.isProxyClass(targetClass)) {
		return false;
	}
	// The method may be on an interface, so let's check on the target class as well.
	Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
	return (specificMethod != method && matchesMethod(specificMethod));
}
 
Example 5
Source File: MetadataCacheAttributeSource.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private CacheAttribute retrieve(Method m, Class t) {
	Method specificMethod = AopUtils.getMostSpecificMethod(m, t);
	CacheAttribute attribute = finder.find(specificMethod);
	if (attribute != null) return attribute;
	if (specificMethod != m) return finder.find(m);
	return null;
}
 
Example 6
Source File: DefaultLimitedResourceSource.java    From Limiter with Apache License 2.0 5 votes vote down vote up
private Collection<LimitedResource> computeLimitedResource(Method method, Class<?> targetClass) {
    // 从代理前的方法上获取
    Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
    Collection<LimitedResource> reDef = findLimitedResource(specificMethod);
    if (!CollectionUtils.isEmpty(reDef)) {
        return reDef;
    }
    // 代理前class对象
    reDef = findLimitedResource(specificMethod.getDeclaringClass());
    if (!CollectionUtils.isEmpty(reDef) && ClassUtils.isUserLevelMethod(specificMethod)) {
        return reDef;
    }
    if (specificMethod != method) {
        // 代理后的方法
        reDef = findLimitedResource(method);
        if (!CollectionUtils.isEmpty(reDef)) {
            return reDef;
        }
        // 代理后的class对象
        reDef = findLimitedResource(method.getDeclaringClass());
        if (!CollectionUtils.isEmpty(reDef) && ClassUtils.isUserLevelMethod(method)) {
            return reDef;
        }
    }

    return null;
}
 
Example 7
Source File: SofaTracerIntroductionInterceptor.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    if (method == null) {
        return invocation.proceed();
    }
    Method mostSpecificMethod = AopUtils.getMostSpecificMethod(method, invocation.getThis()
        .getClass());

    Tracer tracerSpan = findAnnotation(mostSpecificMethod, Tracer.class);
    if (tracerSpan == null) {
        return invocation.proceed();
    }
    return sofaMethodInvocationProcessor.process(invocation, tracerSpan);
}
 
Example 8
Source File: MethodJmsListenerEndpoint.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public Method getMostSpecificMethod() {
	if (this.mostSpecificMethod != null) {
		return this.mostSpecificMethod;
	}
	else if (AopUtils.isAopProxy(this.bean)) {
		Class<?> target = AopProxyUtils.ultimateTargetClass(this.bean);
		return AopUtils.getMostSpecificMethod(getMethod(), target);
	}
	else {
		return getMethod();
	}
}
 
Example 9
Source File: AbstractFallbackTransactionAttributeSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Same signature as {@link #getTransactionAttribute}, but doesn't cache the result.
 * {@link #getTransactionAttribute} is effectively a caching decorator for this method.
 * <p>As of 4.1.8, this method can be overridden.
 * @since 4.1.8
 * @see #getTransactionAttribute
 */
@Nullable
protected TransactionAttribute computeTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
	// Don't allow no-public methods as required.
	if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
		return null;
	}

	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);

	// First try is the method in the target class.
	TransactionAttribute txAttr = findTransactionAttribute(specificMethod);
	if (txAttr != null) {
		return txAttr;
	}

	// Second try is the transaction attribute on the target class.
	txAttr = findTransactionAttribute(specificMethod.getDeclaringClass());
	if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
		return txAttr;
	}

	if (specificMethod != method) {
		// Fallback is to look at the original method.
		txAttr = findTransactionAttribute(method);
		if (txAttr != null) {
			return txAttr;
		}
		// Last fallback is the class of the original method.
		txAttr = findTransactionAttribute(method.getDeclaringClass());
		if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
			return txAttr;
		}
	}

	return null;
}
 
Example 10
Source File: AspectJExpressionPointcut.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(Method method, Class<?> targetClass, boolean beanHasIntroductions) {
	checkReadyToMatch();
	Method targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
	ShadowMatch shadowMatch = getShadowMatch(targetMethod, method);

	// Special handling for this, target, @this, @target, @annotation
	// in Spring - we can optimize since we know we have exactly this class,
	// and there will never be matching subclass at runtime.
	if (shadowMatch.alwaysMatches()) {
		return true;
	}
	else if (shadowMatch.neverMatches()) {
		return false;
	}
	else {
		// the maybe case
		if (beanHasIntroductions) {
			return true;
		}
		// A match test returned maybe - if there are any subtype sensitive variables
		// involved in the test (this, target, at_this, at_target, at_annotation) then
		// we say this is not a match as in Spring there will never be a different
		// runtime subtype.
		RuntimeTestWalker walker = getRuntimeTestWalker(shadowMatch);
		return (!walker.testsSubtypeSensitiveVars() || walker.testTargetInstanceOfResidue(targetClass));
	}
}
 
Example 11
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 12
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 13
Source File: AnnotationMethodMatcher.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(Method method, Class<?> targetClass) {
	if (method.isAnnotationPresent(this.annotationType)) {
		return true;
	}
	// The method may be on an interface, so let's check on the target class as well.
	Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
	return (specificMethod != method && specificMethod.isAnnotationPresent(this.annotationType));
}
 
Example 14
Source File: MethodJmsListenerEndpoint.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
public Method getMostSpecificMethod() {
	if (this.mostSpecificMethod != null) {
		return this.mostSpecificMethod;
	}
	Method method = getMethod();
	if (method != null) {
		Object bean = getBean();
		if (AopUtils.isAopProxy(bean)) {
			Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
			method = AopUtils.getMostSpecificMethod(method, targetClass);
		}
	}
	return method;
}
 
Example 15
Source File: MetaAnnotationMethodMatcher.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public boolean matches(Method method, Class targetClass) {
	if (AnnotationUtils.getAnnotation(method, this.annotationType) != null) {
		return true;
	}
	// The method may be on an interface, so let's check on the target class as well.
	Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
	return (specificMethod != method &&
			(AnnotationUtils.getAnnotation(specificMethod, this.annotationType) != null));
}
 
Example 16
Source File: AbstractFallbackCacheOperationSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private Collection<CacheOperation> computeCacheOperations(Method method, @Nullable Class<?> targetClass) {
	// Don't allow no-public methods as required.
	if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
		return null;
	}

	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);

	// First try is the method in the target class.
	Collection<CacheOperation> opDef = findCacheOperations(specificMethod);
	if (opDef != null) {
		return opDef;
	}

	// Second try is the caching operation on the target class.
	opDef = findCacheOperations(specificMethod.getDeclaringClass());
	if (opDef != null && ClassUtils.isUserLevelMethod(method)) {
		return opDef;
	}

	if (specificMethod != method) {
		// Fallback is to look at the original method.
		opDef = findCacheOperations(method);
		if (opDef != null) {
			return opDef;
		}
		// Last fallback is the class of the original method.
		opDef = findCacheOperations(method.getDeclaringClass());
		if (opDef != null && ClassUtils.isUserLevelMethod(method)) {
			return opDef;
		}
	}

	return null;
}
 
Example 17
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 18
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 19
Source File: AnnotationMethodMatcher.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean matches(Method method, Class<?> targetClass) {
	if (matchesMethod(method)) {
		return true;
	}
	// Proxy classes never have annotations on their redeclared methods.
	if (Proxy.isProxyClass(targetClass)) {
		return false;
	}
	// The method may be on an interface, so let's check on the target class as well.
	Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
	return (specificMethod != method && matchesMethod(specificMethod));
}
 
Example 20
Source File: SpringStopwatchSource.java    From javasimon with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Method getTargetMethod(MethodInvocation methodInvocation, Class<?> targetClass) {
	return AopUtils.getMostSpecificMethod(methodInvocation.getMethod(), targetClass);
}