Search in sources :

Example 11 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

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);
    assertThat(methodMatcher.isRuntime()).as("Should not be a runtime match").isFalse();
    assertMatchesGetAge(methodMatcher);
    assertThat(methodMatcher.matches(setAge, TestBean.class)).as("Expression should match setAge(int) method").isTrue();
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ClassFilter(cn.taketoday.aop.ClassFilter) MethodMatcher(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 12 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

the class MethodMatchersTests method testUnionEquals.

@Test
public void testUnionEquals() {
    MethodMatcher first = MethodMatchers.union(MethodMatcher.TRUE, MethodMatcher.TRUE);
    MethodMatcher second = new ComposablePointcut(MethodMatcher.TRUE).union(new ComposablePointcut(MethodMatcher.TRUE)).getMethodMatcher();
    assertThat(first.equals(second)).isTrue();
    assertThat(second.equals(first)).isTrue();
}
Also used : MethodMatcher(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 13 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

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(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 14 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

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);
    assertThat(intersection.isRuntime()).as("Intersection is a dynamic matcher").isTrue();
    assertThat(intersection.matches(ITESTBEAN_SETAGE, TestBean.class)).as("2Matched setAge method").isTrue();
    DefaultMethodInvocation defaultMethodInvocation = new DefaultMethodInvocation(null, new TestBean(), ITESTBEAN_SETAGE, TestBean.class, new Object[] { 5 }, null);
    assertThat(intersection.matches(defaultMethodInvocation)).as("3Matched setAge method").isTrue();
    // Knock out dynamic part
    intersection = MethodMatchers.intersection(intersection, new TestDynamicMethodMatcherWhichDoesNotMatch());
    assertThat(intersection.isRuntime()).as("Intersection is a dynamic matcher").isTrue();
    assertThat(intersection.matches(ITESTBEAN_SETAGE, TestBean.class)).as("2Matched setAge method").isTrue();
    assertThat(intersection.matches(defaultMethodInvocation)).as("3 - not Matched setAge method").isFalse();
}
Also used : DefaultMethodInvocation(cn.taketoday.aop.framework.DefaultMethodInvocation) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodMatcher(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 15 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

the class Pointcuts method matches.

/**
 * Perform the least expensive check for a pointcut match.
 *
 * @param pointcut the pointcut to match
 * @param invocation runtime invocation contains the candidate method
 * and target class, arguments to the method
 * @return whether there's a runtime match
 */
public static boolean matches(Pointcut pointcut, MethodInvocation invocation) {
    Assert.notNull(pointcut, "Pointcut must not be null");
    if (pointcut == Pointcut.TRUE) {
        return true;
    }
    final Class<?> targetClass = AopUtils.getTargetClass(invocation);
    if (pointcut.getClassFilter().matches(targetClass)) {
        // Only check if it gets past first hurdle.
        MethodMatcher mm = pointcut.getMethodMatcher();
        if (mm.matches(invocation.getMethod(), targetClass)) {
            // We may need additional runtime (argument) check.
            return (!mm.isRuntime() || mm.matches(invocation));
        }
    }
    return false;
}
Also used : MethodMatcher(cn.taketoday.aop.MethodMatcher)

Aggregations

MethodMatcher (cn.taketoday.aop.MethodMatcher)18 Test (org.junit.jupiter.api.Test)16 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)10 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)10 ClassFilter (cn.taketoday.aop.ClassFilter)6 Pointcut (cn.taketoday.aop.Pointcut)6 DefaultMethodInvocation (cn.taketoday.aop.framework.DefaultMethodInvocation)4