Search in sources :

Example 1 with IntroductionAwareMethodMatcher

use of org.springframework.aop.IntroductionAwareMethodMatcher in project spring-framework by spring-projects.

the class AopUtils method canApply.

/**
	 * Can the given pointcut apply at all on the given class?
	 * <p>This is an important test as it can be used to optimize
	 * out a pointcut for a class.
	 * @param pc the static or dynamic pointcut to check
	 * @param targetClass the class to test
	 * @param hasIntroductions whether or not the advisor chain
	 * for this bean includes any introductions
	 * @return whether the pointcut can apply on any method
	 */
public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
    Assert.notNull(pc, "Pointcut must not be null");
    if (!pc.getClassFilter().matches(targetClass)) {
        return false;
    }
    MethodMatcher methodMatcher = pc.getMethodMatcher();
    if (methodMatcher == MethodMatcher.TRUE) {
        // No need to iterate the methods if we're matching any method anyway...
        return true;
    }
    IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
    if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
        introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
    }
    Set<Class<?>> classes = new LinkedHashSet<>(ClassUtils.getAllInterfacesForClassAsSet(targetClass));
    classes.add(targetClass);
    for (Class<?> clazz : classes) {
        Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz);
        for (Method method : methods) {
            if ((introductionAwareMethodMatcher != null && introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions)) || methodMatcher.matches(method, targetClass)) {
                return true;
            }
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Method(java.lang.reflect.Method) IntroductionAwareMethodMatcher(org.springframework.aop.IntroductionAwareMethodMatcher) IntroductionAwareMethodMatcher(org.springframework.aop.IntroductionAwareMethodMatcher) MethodMatcher(org.springframework.aop.MethodMatcher)

Aggregations

Method (java.lang.reflect.Method)1 LinkedHashSet (java.util.LinkedHashSet)1 IntroductionAwareMethodMatcher (org.springframework.aop.IntroductionAwareMethodMatcher)1 MethodMatcher (org.springframework.aop.MethodMatcher)1