Search in sources :

Example 51 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testCanChangeArgumentsIndependentlyOnClonedInvocation.

/**
 * We want to change the arguments on a clone: it shouldn't affect the original.
 */
@Test
public void testCanChangeArgumentsIndependentlyOnClonedInvocation() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    pc.addInterface(ITestBean.class);
    /**
     * Changes the name, then changes it back.
     */
    MethodInterceptor nameReverter = mi -> {
        MethodInvocation clone = ((ReflectiveMethodInvocation) mi).invocableClone();
        String oldName = ((ITestBean) mi.getThis()).getName();
        clone.getArguments()[0] = oldName;
        // Original method invocation should be unaffected by changes to argument list of clone
        mi.proceed();
        return clone.proceed();
    };
    class NameSaver implements MethodInterceptor {

        private List<Object> names = new ArrayList<>();

        @Override
        public Object invoke(MethodInvocation mi) throws Throwable {
            names.add(mi.getArguments()[0]);
            return mi.proceed();
        }
    }
    NameSaver saver = new NameSaver();
    pc.addAdvisor(new DefaultPointcutAdvisor(Pointcuts.SETTERS, nameReverter));
    pc.addAdvisor(new DefaultPointcutAdvisor(Pointcuts.SETTERS, saver));
    ITestBean it = (ITestBean) createProxy(pc);
    String name1 = "tony";
    String name2 = "gordon";
    tb.setName(name1);
    assertThat(tb.getName()).isEqualTo(name1);
    it.setName(name2);
    // NameReverter saved it back
    assertThat(it.getName()).isEqualTo(name1);
    assertThat(saver.names.size()).isEqualTo(2);
    assertThat(saver.names.get(0)).isEqualTo(name2);
    assertThat(saver.names.get(1)).isEqualTo(name1);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixin(test.mixin.LockMixin) AopUtils(org.springframework.aop.support.AopUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) TargetSource(org.springframework.aop.TargetSource) Pointcuts(org.springframework.aop.support.Pointcuts) Lockable(test.mixin.Lockable) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) Map(java.util.Map) TestBean(org.springframework.beans.testfixture.beans.TestBean) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) ThrowsAdvice(org.springframework.aop.ThrowsAdvice) ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) MethodCounter(org.springframework.aop.testfixture.advice.MethodCounter) FileNotFoundException(java.io.FileNotFoundException) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) List(java.util.List) LockMixinAdvisor(test.mixin.LockMixinAdvisor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) HashMap(java.util.HashMap) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) ArrayList(java.util.ArrayList) TimeStamped(org.springframework.core.testfixture.TimeStamped) SQLException(java.sql.SQLException) DynamicIntroductionAdvice(org.springframework.aop.DynamicIntroductionAdvice) IOther(org.springframework.beans.testfixture.beans.IOther) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Advice(org.aopalliance.aop.Advice) MarshalException(java.rmi.MarshalException) Nullable(org.springframework.lang.Nullable) Advisor(org.springframework.aop.Advisor) SerializationTestUtils(org.springframework.core.testfixture.io.SerializationTestUtils) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Iterator(java.util.Iterator) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) DynamicMethodMatcherPointcut(org.springframework.aop.support.DynamicMethodMatcherPointcut) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AfterEach(org.junit.jupiter.api.AfterEach) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Person(org.springframework.beans.testfixture.beans.Person) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInvocation(org.aopalliance.intercept.MethodInvocation) List(java.util.List) ArrayList(java.util.ArrayList) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 52 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testContext.

/**
 * @param context if true, want context
 */
private void testContext(final boolean context) throws Throwable {
    final String s = "foo";
    // Test return value
    MethodInterceptor mi = invocation -> {
        if (!context) {
            assertNoInvocationContext();
        } else {
            assertThat(ExposeInvocationInterceptor.currentInvocation()).as("have context").isNotNull();
        }
        return s;
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    if (context) {
        pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    }
    pc.addAdvice(mi);
    // Keep CGLIB happy
    if (requiresTarget()) {
        pc.setTarget(new TestBean());
    }
    AopProxy aop = createAopProxy(pc);
    assertNoInvocationContext();
    ITestBean tb = (ITestBean) aop.getProxy();
    assertNoInvocationContext();
    assertThat(tb.getName()).as("correct return value").isSameAs(s);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixin(test.mixin.LockMixin) AopUtils(org.springframework.aop.support.AopUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) TargetSource(org.springframework.aop.TargetSource) Pointcuts(org.springframework.aop.support.Pointcuts) Lockable(test.mixin.Lockable) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) Map(java.util.Map) TestBean(org.springframework.beans.testfixture.beans.TestBean) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) ThrowsAdvice(org.springframework.aop.ThrowsAdvice) ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) MethodCounter(org.springframework.aop.testfixture.advice.MethodCounter) FileNotFoundException(java.io.FileNotFoundException) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) List(java.util.List) LockMixinAdvisor(test.mixin.LockMixinAdvisor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) HashMap(java.util.HashMap) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) ArrayList(java.util.ArrayList) TimeStamped(org.springframework.core.testfixture.TimeStamped) SQLException(java.sql.SQLException) DynamicIntroductionAdvice(org.springframework.aop.DynamicIntroductionAdvice) IOther(org.springframework.beans.testfixture.beans.IOther) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Advice(org.aopalliance.aop.Advice) MarshalException(java.rmi.MarshalException) Nullable(org.springframework.lang.Nullable) Advisor(org.springframework.aop.Advisor) SerializationTestUtils(org.springframework.core.testfixture.io.SerializationTestUtils) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Iterator(java.util.Iterator) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) DynamicMethodMatcherPointcut(org.springframework.aop.support.DynamicMethodMatcherPointcut) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AfterEach(org.junit.jupiter.api.AfterEach) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Person(org.springframework.beans.testfixture.beans.Person) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean)

Example 53 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testCanGetFactoryReferenceAndManipulate.

@Test
public void testCanGetFactoryReferenceAndManipulate() {
    ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test1");
    assertThat(ITestBean.class.isAssignableFrom(config.getObjectType())).as("Has correct object type").isTrue();
    assertThat(ITestBean.class.isAssignableFrom(factory.getType("test1"))).as("Has correct object type").isTrue();
    // Trigger lazy initialization.
    config.getObject();
    assertThat(config.getAdvisors().length).as("Have one advisors").isEqualTo(1);
    assertThat(ITestBean.class.isAssignableFrom(config.getObjectType())).as("Has correct object type").isTrue();
    assertThat(ITestBean.class.isAssignableFrom(factory.getType("test1"))).as("Has correct object type").isTrue();
    ITestBean tb = (ITestBean) factory.getBean("test1");
    // no exception
    tb.hashCode();
    final Exception ex = new UnsupportedOperationException("invoke");
    // Add evil interceptor to head of list
    config.addAdvice(0, (MethodInterceptor) invocation -> {
        throw ex;
    });
    assertThat(config.getAdvisors().length).as("Have correct advisor count").isEqualTo(2);
    ITestBean tb1 = (ITestBean) factory.getBean("test1");
    assertThatExceptionOfType(Exception.class).isThrownBy(tb1::toString).isSameAs(ex);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeforeEach(org.junit.jupiter.api.BeforeEach) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) Proxy(java.lang.reflect.Proxy) AopUtils(org.springframework.aop.support.AopUtils) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ArrayList(java.util.ArrayList) Lockable(test.mixin.Lockable) SideEffectBean(org.springframework.beans.testfixture.beans.SideEffectBean) TimeStamped(org.springframework.core.testfixture.TimeStamped) IntroductionInterceptor(org.springframework.aop.IntroductionInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) TestApplicationListener(org.springframework.context.testfixture.beans.TestApplicationListener) BeanCreationException(org.springframework.beans.factory.BeanCreationException) FactoryBean(org.springframework.beans.factory.FactoryBean) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Advice(org.aopalliance.aop.Advice) TestBean(org.springframework.beans.testfixture.beans.TestBean) Nullable(org.springframework.lang.Nullable) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) SerializationTestUtils(org.springframework.core.testfixture.io.SerializationTestUtils) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) DynamicMethodMatcherPointcut(org.springframework.aop.support.DynamicMethodMatcherPointcut) ApplicationListener(org.springframework.context.ApplicationListener) FileNotFoundException(java.io.FileNotFoundException) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) ClassFilter(org.springframework.aop.ClassFilter) LockedException(test.mixin.LockedException) BeanFactory(org.springframework.beans.factory.BeanFactory) Person(org.springframework.beans.testfixture.beans.Person) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) FileNotFoundException(java.io.FileNotFoundException) LockedException(test.mixin.LockedException) Test(org.junit.jupiter.api.Test)

Example 54 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class AsyncAnnotationBeanPostProcessorTests method invokedAsynchronouslyOnProxyTarget.

@Test
public void invokedAsynchronouslyOnProxyTarget() {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBeanDefinition("postProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class));
    TestBean tb = new TestBean();
    ProxyFactory pf = new ProxyFactory(ITestBean.class, (MethodInterceptor) invocation -> invocation.getMethod().invoke(tb, invocation.getArguments()));
    context.registerBean("target", ITestBean.class, () -> (ITestBean) pf.getProxy());
    context.refresh();
    ITestBean testBean = context.getBean("target", ITestBean.class);
    testBean.test();
    Thread mainThread = Thread.currentThread();
    testBean.await(3000);
    Thread asyncThread = testBean.getThread();
    assertThat(asyncThread).isNotSameAs(mainThread);
    context.close();
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) AopUtils(org.springframework.aop.support.AopUtils) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) Future(java.util.concurrent.Future) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Method(java.lang.reflect.Method) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Executor(java.util.concurrent.Executor) AsyncUncaughtExceptionHandler(org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) CountDownLatch(java.util.concurrent.CountDownLatch) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ReflectionUtils(org.springframework.util.ReflectionUtils) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Bean(org.springframework.context.annotation.Bean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ProxyFactory(org.springframework.aop.framework.ProxyFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 55 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class UnsupportedInterceptor method getAdvisedProxy.

private ITestBean getAdvisedProxy(TestBean target) {
    ProxyFactory pf = new ProxyFactory(new Class<?>[] { ITestBean.class });
    pf.setProxyTargetClass(true);
    MethodInterceptor advice = new NopInterceptor();
    Pointcut pointcut = new Pointcut() {

        @Override
        public ClassFilter getClassFilter() {
            return ClassFilter.TRUE;
        }

        @Override
        public MethodMatcher getMethodMatcher() {
            return MethodMatcher.TRUE;
        }

        @Override
        public boolean equals(Object obj) {
            return true;
        }

        @Override
        public int hashCode() {
            return 0;
        }
    };
    pf.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice));
    pf.setTarget(target);
    pf.setFrozen(true);
    pf.setExposeProxy(false);
    return (ITestBean) pf.getProxy();
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor)

Aggregations

MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)56 Test (org.junit.Test)30 MethodInvocation (org.aopalliance.intercept.MethodInvocation)28 Method (java.lang.reflect.Method)19 List (java.util.List)19 ArrayList (java.util.ArrayList)18 Test (org.junit.jupiter.api.Test)15 Advice (org.aopalliance.aop.Advice)14 Map (java.util.Map)13 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)13 AopUtils (org.springframework.aop.support.AopUtils)12 TestBean (org.springframework.beans.testfixture.beans.TestBean)12 HashMap (java.util.HashMap)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)9 Advisor (org.springframework.aop.Advisor)9 Message (org.springframework.messaging.Message)8 Assert.assertEquals (org.junit.Assert.assertEquals)7