Search in sources :

Example 1 with AfterReturningAdvice

use of cn.taketoday.aop.AfterReturningAdvice 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 AfterReturningAdvice

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

the class AspectJPrecedenceComparatorTests method createSpringAOPAfterAdvice.

private Advisor createSpringAOPAfterAdvice(int order) {
    AfterReturningAdvice advice = (returnValue, method) -> {
    };
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(this.anyOldPointcut, advice);
    advisor.setOrder(order);
    return advisor;
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) AspectJAfterAdvice(cn.taketoday.aop.aspectj.AspectJAfterAdvice) Advisor(cn.taketoday.aop.Advisor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AspectJAroundAdvice(cn.taketoday.aop.aspectj.AspectJAroundAdvice) Test(org.junit.jupiter.api.Test) AspectJAfterThrowingAdvice(cn.taketoday.aop.aspectj.AspectJAfterThrowingAdvice) AspectJMethodBeforeAdvice(cn.taketoday.aop.aspectj.AspectJMethodBeforeAdvice) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) BeforeAdvice(cn.taketoday.aop.BeforeAdvice) AbstractAspectJAdvice(cn.taketoday.aop.aspectj.AbstractAspectJAdvice) AspectJPointcutAdvisor(cn.taketoday.aop.aspectj.AspectJPointcutAdvisor) AspectJExpressionPointcut(cn.taketoday.aop.aspectj.AspectJExpressionPointcut) Method(java.lang.reflect.Method) AspectJAfterReturningAdvice(cn.taketoday.aop.aspectj.AspectJAfterReturningAdvice) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) AspectJAfterReturningAdvice(cn.taketoday.aop.aspectj.AspectJAfterReturningAdvice) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor)

Example 3 with AfterReturningAdvice

use of cn.taketoday.aop.AfterReturningAdvice in project today-framework 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 4 with AfterReturningAdvice

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

the class AspectJPrecedenceComparatorTests method createSpringAOPAfterAdvice.

private Advisor createSpringAOPAfterAdvice(int order) {
    AfterReturningAdvice advice = (returnValue, method) -> {
    };
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(this.anyOldPointcut, advice);
    advisor.setOrder(order);
    return advisor;
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) AspectJAfterAdvice(cn.taketoday.aop.aspectj.AspectJAfterAdvice) Advisor(cn.taketoday.aop.Advisor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AspectJAroundAdvice(cn.taketoday.aop.aspectj.AspectJAroundAdvice) Test(org.junit.jupiter.api.Test) AspectJAfterThrowingAdvice(cn.taketoday.aop.aspectj.AspectJAfterThrowingAdvice) AspectJMethodBeforeAdvice(cn.taketoday.aop.aspectj.AspectJMethodBeforeAdvice) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) BeforeAdvice(cn.taketoday.aop.BeforeAdvice) AbstractAspectJAdvice(cn.taketoday.aop.aspectj.AbstractAspectJAdvice) AspectJPointcutAdvisor(cn.taketoday.aop.aspectj.AspectJPointcutAdvisor) AspectJExpressionPointcut(cn.taketoday.aop.aspectj.AspectJExpressionPointcut) Method(java.lang.reflect.Method) AspectJAfterReturningAdvice(cn.taketoday.aop.aspectj.AspectJAfterReturningAdvice) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) AspectJAfterReturningAdvice(cn.taketoday.aop.aspectj.AspectJAfterReturningAdvice) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor)

Example 5 with AfterReturningAdvice

use of cn.taketoday.aop.AfterReturningAdvice in project today-framework 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, 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.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) 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) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) CountingAfterReturningAdvice(cn.taketoday.aop.CountingAfterReturningAdvice) 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) Test(org.junit.jupiter.api.Test)

Aggregations

Advisor (cn.taketoday.aop.Advisor)5 AfterReturningAdvice (cn.taketoday.aop.AfterReturningAdvice)5 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)5 Method (java.lang.reflect.Method)5 Test (org.junit.jupiter.api.Test)5 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)3 StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)3 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)3 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)3 MethodInvocation (org.aopalliance.intercept.MethodInvocation)3 BeforeAdvice (cn.taketoday.aop.BeforeAdvice)2 AbstractAspectJAdvice (cn.taketoday.aop.aspectj.AbstractAspectJAdvice)2 AspectJAfterAdvice (cn.taketoday.aop.aspectj.AspectJAfterAdvice)2 AspectJAfterReturningAdvice (cn.taketoday.aop.aspectj.AspectJAfterReturningAdvice)2 AspectJAfterThrowingAdvice (cn.taketoday.aop.aspectj.AspectJAfterThrowingAdvice)2 AspectJAroundAdvice (cn.taketoday.aop.aspectj.AspectJAroundAdvice)2 AspectJExpressionPointcut (cn.taketoday.aop.aspectj.AspectJExpressionPointcut)2 AspectJMethodBeforeAdvice (cn.taketoday.aop.aspectj.AspectJMethodBeforeAdvice)2 AspectJPointcutAdvisor (cn.taketoday.aop.aspectj.AspectJPointcutAdvisor)2 CountingAfterReturningAdvice (cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice)2