Search in sources :

Example 1 with StaticMethodMatcherPointcutAdvisor

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

the class AbstractAopProxyTests method testAfterReturningAdvisorIsInvoked.

@Test
public void testAfterReturningAdvisorIsInvoked() {
    class SummingAfterAdvice implements AfterReturningAdvice {

        public int sum;

        @Override
        public void afterReturning(Object returnValue, MethodInvocation invocation) throws Throwable {
            sum += ((Integer) returnValue).intValue();
        }
    }
    SummingAfterAdvice aa = new SummingAfterAdvice();
    @SuppressWarnings("serial") Advisor matchesInt = new StaticMethodMatcherPointcutAdvisor(aa) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getReturnType() == int.class;
        }
    };
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesInt);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesInt);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(aa.sum).isEqualTo(0);
    int i1 = 12;
    int i2 = 13;
    // Won't be advised
    proxied.setAge(i1);
    assertThat(proxied.getAge()).isEqualTo(i1);
    assertThat(aa.sum).isEqualTo(i1);
    proxied.setAge(i2);
    assertThat(proxied.getAge()).isEqualTo(i2);
    assertThat(aa.sum).isEqualTo((i1 + i2));
    assertThat(proxied.getAge()).isEqualTo(i2);
}
Also used : NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) Advisor(cn.taketoday.aop.Advisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingAfterReturningAdvice(cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) Method(java.lang.reflect.Method) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Nullable(cn.taketoday.lang.Nullable) Test(org.junit.jupiter.api.Test)

Example 2 with StaticMethodMatcherPointcutAdvisor

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

the class AbstractAopProxyTests method testThrowsAdvisorIsInvoked.

@Test
public void testThrowsAdvisorIsInvoked() throws Throwable {
    // Reacts to ServletException and RemoteException
    MyThrowsHandler th = new MyThrowsHandler();
    @SuppressWarnings("serial") Advisor matchesEchoInvocations = new StaticMethodMatcherPointcutAdvisor(th) {

        @Override
        public boolean matches(Method m, Class<?> targetClass) {
            return m.getName().startsWith("echo");
        }
    };
    Echo target = new Echo();
    target.setA(16);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesEchoInvocations);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesEchoInvocations);
    IEcho proxied = (IEcho) createProxy(pf);
    assertThat(th.getCalls()).isEqualTo(0);
    assertThat(proxied.getA()).isEqualTo(target.getA());
    assertThat(th.getCalls()).isEqualTo(0);
    Exception ex = new Exception();
    // Will be advised but doesn't match
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> proxied.echoException(1, ex)).matches(ex::equals);
    FileNotFoundException fex = new FileNotFoundException();
    assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> proxied.echoException(1, fex)).matches(fex::equals);
    assertThat(th.getCalls("ioException")).isEqualTo(1);
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) FileNotFoundException(java.io.FileNotFoundException) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Advisor(cn.taketoday.aop.Advisor) LockMixinAdvisor(cn.taketoday.aop.mixin.LockMixinAdvisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Method(java.lang.reflect.Method) AopConfigException(cn.taketoday.aop.framework.AopConfigException) LockedException(cn.taketoday.aop.mixin.LockedException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) MyThrowsHandler(cn.taketoday.aop.MyThrowsHandler) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 3 with StaticMethodMatcherPointcutAdvisor

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

the class AbstractAopProxyTests method testBeforeAdvisorIsInvoked.

@Test
public void testBeforeAdvisorIsInvoked() {
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getParameterCount() == 0;
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesNoArgs);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(cba.getCalls()).isEqualTo(0);
    assertThat(cba.getCalls("getAge")).isEqualTo(0);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(cba.getCalls("getAge")).isEqualTo(1);
    assertThat(cba.getCalls("setAge")).isEqualTo(0);
    // Won't be advised
    proxied.setAge(26);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(proxied.getAge()).isEqualTo(26);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) Advisor(cn.taketoday.aop.Advisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Method(java.lang.reflect.Method) Nullable(cn.taketoday.lang.Nullable) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 4 with StaticMethodMatcherPointcutAdvisor

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

the class AbstractAopProxyTests method testOverloadedMethodsWithDifferentAdvice.

@SuppressWarnings("serial")
@Test
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
    Overloads target = new Overloads();
    ProxyFactory pc = new ProxyFactory(target);
    NopInterceptor overLoadVoids = new NopInterceptor();
    pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getName().equals("overload") && m.getParameterCount() == 0;
        }
    });
    NopInterceptor overLoadInts = new NopInterceptor();
    pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getName().equals("overload") && m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(int.class);
        }
    });
    IOverloads proxy = (IOverloads) createProxy(pc);
    assertThat(overLoadInts.getCount()).isEqualTo(0);
    assertThat(overLoadVoids.getCount()).isEqualTo(0);
    proxy.overload();
    assertThat(overLoadInts.getCount()).isEqualTo(0);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
    assertThat(proxy.overload(25)).isEqualTo(25);
    assertThat(overLoadInts.getCount()).isEqualTo(1);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
    proxy.noAdvice();
    assertThat(overLoadInts.getCount()).isEqualTo(1);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
}
Also used : NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 5 with StaticMethodMatcherPointcutAdvisor

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

the class AbstractAopProxyTests method testCloneInvocationToProceedThreeTimes.

/**
 * There are times when we want to call proceed() twice.
 * We can do this if we clone the invocation.
 */
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    pc.addInterface(ITestBean.class);
    MethodInterceptor twoBirthdayInterceptor = mi -> {
        // Clone the invocation to proceed three times
        // "The Moor's Last Sigh": this technology can cause premature aging
        MethodInvocation clone1 = ((AbstractMethodInvocation) mi).invocableClone();
        MethodInvocation clone2 = ((AbstractMethodInvocation) mi).invocableClone();
        clone1.proceed();
        clone2.proceed();
        return mi.proceed();
    };
    @SuppressWarnings("serial") StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return "haveBirthday".equals(m.getName());
        }
    };
    pc.addAdvisor(advisor);
    ITestBean it = (ITestBean) createProxy(pc);
    final int age = 20;
    it.setAge(age);
    assertThat(it.getAge()).isEqualTo(age);
    // Should return the age before the third, AOP-induced birthday
    assertThat(it.haveBirthday()).isEqualTo((age + 2));
    // Return the final age produced by 3 birthdays
    assertThat(it.getAge()).isEqualTo((age + 3));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TimeStamped(cn.taketoday.core.testfixture.TimeStamped) LockMixin(test.mixin.LockMixin) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Pointcuts(cn.taketoday.aop.support.Pointcuts) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) Map(java.util.Map) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) Method(java.lang.reflect.Method) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) AdvisedSupportListener(cn.taketoday.aop.AdvisedSupportListener) ExposeInvocationInterceptor(cn.taketoday.aop.interceptor.ExposeInvocationInterceptor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) TimestampIntroductionInterceptor(cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor) Person(cn.taketoday.beans.testfixture.beans.Person) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.jupiter.api.Test) List(java.util.List) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) Nullable(cn.taketoday.lang.Nullable) LockMixinAdvisor(test.mixin.LockMixinAdvisor) NameMatchMethodPointcut(cn.taketoday.aop.support.NameMatchMethodPointcut) CountingAfterReturningAdvice(cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice) Advisor(cn.taketoday.aop.Advisor) HashMap(java.util.HashMap) MethodCounter(cn.taketoday.aop.testfixture.advice.MethodCounter) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ArrayList(java.util.ArrayList) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) SQLException(java.sql.SQLException) DelegatingIntroductionInterceptor(cn.taketoday.aop.support.DelegatingIntroductionInterceptor) ThrowsAdvice(cn.taketoday.aop.ThrowsAdvice) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) AopUtils(cn.taketoday.aop.support.AopUtils) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) Lockable(test.aop.Lockable) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Advice(org.aopalliance.aop.Advice) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) DynamicIntroductionAdvice(cn.taketoday.aop.DynamicIntroductionAdvice) IOther(cn.taketoday.beans.testfixture.beans.IOther) Iterator(java.util.Iterator) IOException(java.io.IOException) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AfterEach(org.junit.jupiter.api.AfterEach) DynamicMethodMatcherPointcut(cn.taketoday.aop.support.DynamicMethodMatcherPointcut) SerializationTestUtils(cn.taketoday.core.testfixture.io.SerializationTestUtils) TargetSource(cn.taketoday.aop.TargetSource) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) LockedException(test.mixin.LockedException) MyThrowsHandler(cn.taketoday.aop.testfixture.advice.MyThrowsHandler) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Nullable(cn.taketoday.lang.Nullable) Test(org.junit.jupiter.api.Test)

Aggregations

StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)18 Method (java.lang.reflect.Method)18 Test (org.junit.jupiter.api.Test)18 Advisor (cn.taketoday.aop.Advisor)14 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)14 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)14 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)12 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)12 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)12 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)12 Nullable (cn.taketoday.lang.Nullable)10 LockMixinAdvisor (test.mixin.LockMixinAdvisor)10 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)6 MethodInvocation (org.aopalliance.intercept.MethodInvocation)6 AfterReturningAdvice (cn.taketoday.aop.AfterReturningAdvice)5 NopInterceptor (cn.taketoday.aop.NopInterceptor)5 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)5