Search in sources :

Example 6 with MethodMatcher

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

the class MethodMatchersTests method testDynamicAndStaticMethodMatcherIntersection.

@Test
public void testDynamicAndStaticMethodMatcherIntersection() throws Exception {
    MethodMatcher mm1 = MethodMatcher.TRUE;
    MethodMatcher mm2 = new TestDynamicMethodMatcherWhichMatches();
    MethodMatcher intersection = MethodMatchers.intersection(mm1, mm2);
    assertTrue("Intersection is a dynamic matcher", intersection.isRuntime());
    assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class));
    assertTrue("3Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Integer(5)));
    // Knock out dynamic part
    intersection = MethodMatchers.intersection(intersection, new TestDynamicMethodMatcherWhichDoesNotMatch());
    assertTrue("Intersection is a dynamic matcher", intersection.isRuntime());
    assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class));
    assertFalse("3 - not Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Integer(5)));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.Test)

Example 7 with MethodMatcher

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

the class AbstractAspectJAdvice method buildSafePointcut.

/**
	 * Build a 'safe' pointcut that excludes the AspectJ advice method itself.
	 * @return a composable pointcut that builds on the original AspectJ expression pointcut
	 * @see #getPointcut()
	 */
public final Pointcut buildSafePointcut() {
    Pointcut pc = getPointcut();
    MethodMatcher safeMethodMatcher = MethodMatchers.intersection(new AdviceExcludingMethodMatcher(this.aspectJAdviceMethod), pc.getMethodMatcher());
    return new ComposablePointcut(pc.getClassFilter(), safeMethodMatcher);
}
Also used : ComposablePointcut(org.springframework.aop.support.ComposablePointcut) Pointcut(org.springframework.aop.Pointcut) MethodMatcher(org.springframework.aop.MethodMatcher) StaticMethodMatcher(org.springframework.aop.support.StaticMethodMatcher) ComposablePointcut(org.springframework.aop.support.ComposablePointcut)

Example 8 with MethodMatcher

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

the class DefaultAdvisorChainFactory method getInterceptorsAndDynamicInterceptionAdvice.

@Override
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Advised config, Method method, Class<?> targetClass) {
    // This is somewhat tricky... We have to process introductions first,
    // but we need to preserve order in the ultimate list.
    List<Object> interceptorList = new ArrayList<>(config.getAdvisors().length);
    Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
    boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
    AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
    for (Advisor advisor : config.getAdvisors()) {
        if (advisor instanceof PointcutAdvisor) {
            // Add it conditionally.
            PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
            if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
                MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
                MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
                if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
                    if (mm.isRuntime()) {
                        // isn't a problem as we normally cache created chains.
                        for (MethodInterceptor interceptor : interceptors) {
                            interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
                        }
                    } else {
                        interceptorList.addAll(Arrays.asList(interceptors));
                    }
                }
            }
        } else if (advisor instanceof IntroductionAdvisor) {
            IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
            if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
                Interceptor[] interceptors = registry.getInterceptors(advisor);
                interceptorList.addAll(Arrays.asList(interceptors));
            }
        } else {
            Interceptor[] interceptors = registry.getInterceptors(advisor);
            interceptorList.addAll(Arrays.asList(interceptors));
        }
    }
    return interceptorList;
}
Also used : PointcutAdvisor(org.springframework.aop.PointcutAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) ArrayList(java.util.ArrayList) PointcutAdvisor(org.springframework.aop.PointcutAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) Advisor(org.springframework.aop.Advisor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) GlobalAdvisorAdapterRegistry(org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry) AdvisorAdapterRegistry(org.springframework.aop.framework.adapter.AdvisorAdapterRegistry) MethodMatcher(org.springframework.aop.MethodMatcher)

Example 9 with MethodMatcher

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

the class CallCountingInterceptor method testMatchWithTypePattern.

@Test
public void testMatchWithTypePattern() throws Exception {
    String expression = "execution(* *..TestBean.*Age(..))";
    Pointcut pointcut = getPointcut(expression);
    ClassFilter classFilter = pointcut.getClassFilter();
    MethodMatcher methodMatcher = pointcut.getMethodMatcher();
    assertMatchesTestBeanClass(classFilter);
    // not currently testable in a reliable fashion
    //assertDoesNotMatchStringClass(classFilter);
    assertFalse("Should not be a runtime match", methodMatcher.isRuntime());
    assertMatchesGetAge(methodMatcher);
    assertTrue("Expression should match setAge(int) method", methodMatcher.matches(setAge, TestBean.class));
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ClassFilter(org.springframework.aop.ClassFilter) MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.Test)

Example 10 with MethodMatcher

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

the class CallCountingInterceptor method testMatchExplicit.

@Test
public void testMatchExplicit() {
    String expression = "execution(int org.springframework.tests.sample.beans.TestBean.getAge())";
    Pointcut pointcut = getPointcut(expression);
    ClassFilter classFilter = pointcut.getClassFilter();
    MethodMatcher methodMatcher = pointcut.getMethodMatcher();
    assertMatchesTestBeanClass(classFilter);
    // not currently testable in a reliable fashion
    //assertDoesNotMatchStringClass(classFilter);
    assertFalse("Should not be a runtime match", methodMatcher.isRuntime());
    assertMatchesGetAge(methodMatcher);
    assertFalse("Expression should match setAge() method", methodMatcher.matches(setAge, TestBean.class));
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ClassFilter(org.springframework.aop.ClassFilter) MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.Test)

Aggregations

MethodMatcher (org.springframework.aop.MethodMatcher)11 Test (org.junit.Test)8 ITestBean (org.springframework.tests.sample.beans.ITestBean)7 TestBean (org.springframework.tests.sample.beans.TestBean)7 Pointcut (org.springframework.aop.Pointcut)4 ClassFilter (org.springframework.aop.ClassFilter)3 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)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 ComposablePointcut (org.springframework.aop.support.ComposablePointcut)1 StaticMethodMatcher (org.springframework.aop.support.StaticMethodMatcher)1