Search in sources :

Example 16 with MethodMatcher

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

the class CallCountingInterceptor method testMatchExplicit.

@Test
public void testMatchExplicit() {
    String expression = "execution(int cn.taketoday.beans.testfixture.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);
    assertThat(methodMatcher.isRuntime()).as("Should not be a runtime match").isFalse();
    assertMatchesGetAge(methodMatcher);
    assertThat(methodMatcher.matches(setAge, TestBean.class)).as("Expression should match setAge() method").isFalse();
}
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 17 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-infrastructure 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)

Example 18 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-infrastructure 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)

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