Search in sources :

Example 1 with MethodMatcher

use of org.springframework.aop.MethodMatcher 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)

Example 2 with MethodMatcher

use of org.springframework.aop.MethodMatcher in project spring-security by spring-projects.

the class PostFilterAuthorizationMethodInterceptorTests method methodMatcherWhenMethodHasPostFilterAnnotationThenMatches.

@Test
public void methodMatcherWhenMethodHasPostFilterAnnotationThenMatches() throws Exception {
    PostFilterAuthorizationMethodInterceptor advice = new PostFilterAuthorizationMethodInterceptor();
    MethodMatcher methodMatcher = advice.getPointcut().getMethodMatcher();
    assertThat(methodMatcher.matches(TestClass.class.getMethod("doSomethingArray", String[].class), TestClass.class)).isTrue();
}
Also used : MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 3 with MethodMatcher

use of org.springframework.aop.MethodMatcher in project spring-security by spring-projects.

the class PreFilterAuthorizationMethodInterceptorTests method methodMatcherWhenMethodHasPreFilterAnnotationThenMatches.

@Test
public void methodMatcherWhenMethodHasPreFilterAnnotationThenMatches() throws Exception {
    PreFilterAuthorizationMethodInterceptor advice = new PreFilterAuthorizationMethodInterceptor();
    MethodMatcher methodMatcher = advice.getPointcut().getMethodMatcher();
    assertThat(methodMatcher.matches(TestClass.class.getMethod("doSomethingListFilterTargetMatch", List.class), TestClass.class)).isTrue();
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 4 with MethodMatcher

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

the class MethodMatchersTests method testDefaultMatchesAll.

@Test
public void testDefaultMatchesAll() throws Exception {
    MethodMatcher defaultMm = MethodMatcher.TRUE;
    assertThat(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)).isTrue();
    assertThat(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)).isTrue();
}
Also used : MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 5 with MethodMatcher

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

the class MethodMatchersTests method testStaticMethodMatcherUnion.

@Test
public void testStaticMethodMatcherUnion() throws Exception {
    MethodMatcher getterMatcher = new StartsWithMatcher("get");
    MethodMatcher setterMatcher = new StartsWithMatcher("set");
    MethodMatcher union = MethodMatchers.union(getterMatcher, setterMatcher);
    assertThat(union.isRuntime()).as("Union is a static matcher").isFalse();
    assertThat(union.matches(ITESTBEAN_SETAGE, TestBean.class)).as("Matched setAge method").isTrue();
    assertThat(union.matches(ITESTBEAN_GETAGE, TestBean.class)).as("Matched getAge method").isTrue();
    assertThat(union.matches(IOTHER_ABSQUATULATE, TestBean.class)).as("Didn't matched absquatulate method").isFalse();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Aggregations

MethodMatcher (org.springframework.aop.MethodMatcher)16 Test (org.junit.jupiter.api.Test)12 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 TestBean (org.springframework.beans.testfixture.beans.TestBean)5 ClassFilter (org.springframework.aop.ClassFilter)4 Pointcut (org.springframework.aop.Pointcut)4 ArrayList (java.util.ArrayList)2 ComposablePointcut (org.springframework.aop.support.ComposablePointcut)2 Method (java.lang.reflect.Method)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 Advisor (org.springframework.aop.Advisor)1 IntroductionAdvisor (org.springframework.aop.IntroductionAdvisor)1 IntroductionAwareMethodMatcher (org.springframework.aop.IntroductionAwareMethodMatcher)1 PointcutAdvisor (org.springframework.aop.PointcutAdvisor)1 AdvisorAdapterRegistry (org.springframework.aop.framework.adapter.AdvisorAdapterRegistry)1 GlobalAdvisorAdapterRegistry (org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry)1 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)1 StaticMethodMatcher (org.springframework.aop.support.StaticMethodMatcher)1