Search in sources :

Example 1 with MethodInterceptor

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

the class DefaultAdvisorAdapterRegistry method getInterceptors.

@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
    List<MethodInterceptor> interceptors = new ArrayList<>(3);
    Advice advice = advisor.getAdvice();
    if (advice instanceof MethodInterceptor) {
        interceptors.add((MethodInterceptor) advice);
    }
    for (AdvisorAdapter adapter : this.adapters) {
        if (adapter.supportsAdvice(advice)) {
            interceptors.add(adapter.getInterceptor(advisor));
        }
    }
    if (interceptors.isEmpty()) {
        throw new UnknownAdviceTypeException(advisor.getAdvice());
    }
    return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ArrayList(java.util.ArrayList) Advice(org.aopalliance.aop.Advice)

Example 2 with MethodInterceptor

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

the class AbstractAopProxyTests method testUndeclaredUnheckedException.

@Test
public void testUndeclaredUnheckedException() throws Throwable {
    final RuntimeException unexpectedException = new RuntimeException();
    // Test return value
    MethodInterceptor mi = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            throw unexpectedException;
        }
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    pc.addAdvice(mi);
    // We don't care about the object
    pc.setTarget(new TestBean());
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    try {
        // Note: exception param below isn't used
        tb.getAge();
        fail("Should have wrapped exception raised by interceptor");
    } catch (RuntimeException thrown) {
        assertEquals("exception matches", unexpectedException, thrown);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 3 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project ratpack by ratpack.

the class RatpackBaseRegistryModule method configure.

@Override
protected void configure() {
    ExecutionScope executionScope = new ExecutionScope();
    bindScope(ExecutionScoped.class, executionScope);
    RequestScope requestScope = new RequestScope();
    bindScope(RequestScoped.class, requestScope);
    bind(ExecutionScope.class).toInstance(executionScope);
    bind(RequestScope.class).toInstance(requestScope);
    bind(ExecutionPrimingInitializer.class);
    SIMPLE_TYPES.forEach(this::simpleBind);
    PARAMETERISED_TYPES.forEach(this::parameterisedBind);
    OPTIONAL_TYPES.forEach(this::optionalBind);
    MethodInterceptor interceptor = new BlockingInterceptor();
    bindInterceptor(Matchers.annotatedWith(Blocks.class), new NotGroovyMethodMatcher(), interceptor);
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(Blocks.class), interceptor);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Blocks(ratpack.api.Blocks)

Example 4 with MethodInterceptor

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

the class MethodInvokingMessageProcessorTests method testProxyInvocation.

@Test
public void testProxyInvocation() {
    final AtomicReference<Object> result = new AtomicReference<>();
    class MyHandler implements MessageHandler {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            result.set(message.getPayload());
        }
    }
    MessageHandler service = new MyHandler();
    final AtomicBoolean adviceCalled = new AtomicBoolean();
    ProxyFactory proxyFactory = new ProxyFactory(service);
    proxyFactory.addAdvice((MethodInterceptor) i -> {
        adviceCalled.set(true);
        return i.proceed();
    });
    service = (MessageHandler) proxyFactory.getProxy(getClass().getClassLoader());
    MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, "handleMessage");
    processor.processMessage(new GenericMessage<>("foo"));
    assertEquals("foo", result.get());
    assertTrue(adviceCalled.get());
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Assert.assertThat(org.junit.Assert.assertThat) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Method(java.lang.reflect.Method) Header(org.springframework.messaging.handler.annotation.Header) StopWatch(org.springframework.util.StopWatch) UUID(java.util.UUID) EnableIntegration(org.springframework.integration.config.EnableIntegration) Collectors(java.util.stream.Collectors) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) AdditionalAnswers.returnsFirstArg(org.mockito.AdditionalAnswers.returnsFirstArg) Configuration(org.springframework.context.annotation.Configuration) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Payload(org.springframework.messaging.handler.annotation.Payload) LogFactory(org.apache.commons.logging.LogFactory) Mockito.mock(org.mockito.Mockito.mock) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) MessagingException(org.springframework.messaging.MessagingException) UseSpelInvoker(org.springframework.integration.annotation.UseSpelInvoker) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Assert.assertSame(org.junit.Assert.assertSame) LinkedHashMap(java.util.LinkedHashMap) MessageBuilder(org.springframework.integration.support.MessageBuilder) Message(org.springframework.messaging.Message) ExpectedException(org.junit.rules.ExpectedException) Description(org.hamcrest.Description) Properties(java.util.Properties) Assert.assertNotNull(org.junit.Assert.assertNotNull) MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SpelParserConfiguration(org.springframework.expression.spel.SpelParserConfiguration) ApplicationContext(org.springframework.context.ApplicationContext) MessageHeaders(org.springframework.messaging.MessageHeaders) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) GatewayProxyFactoryBean(org.springframework.integration.gateway.GatewayProxyFactoryBean) BeanFactory(org.springframework.beans.factory.BeanFactory) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Expression(org.springframework.expression.Expression) Log(org.apache.commons.logging.Log) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelCompilerMode(org.springframework.expression.spel.SpelCompilerMode) Collections(java.util.Collections) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 5 with MethodInterceptor

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

the class MethodInvokingMessageProcessorTests method testProxyAndHeaderAnnotation.

@Test
public void testProxyAndHeaderAnnotation() {
    final AtomicReference<Object> payloadReference = new AtomicReference<>();
    final AtomicReference<UUID> idReference = new AtomicReference<>();
    class MyHandler {

        public void handle(@Header(MessageHeaders.ID) UUID id, @Payload Object payload) {
            idReference.set(id);
            payloadReference.set(payload);
        }
    }
    MyHandler service = new MyHandler();
    final AtomicBoolean adviceCalled = new AtomicBoolean();
    ProxyFactory proxyFactory = new ProxyFactory(service);
    proxyFactory.addAdvice((MethodInterceptor) i -> {
        adviceCalled.set(true);
        return i.proceed();
    });
    service = (MyHandler) proxyFactory.getProxy(getClass().getClassLoader());
    GenericMessage<String> testMessage = new GenericMessage<>("foo");
    MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, "handle");
    processor.processMessage(testMessage);
    assertEquals(testMessage.getPayload(), payloadReference.get());
    assertEquals(testMessage.getHeaders().getId(), idReference.get());
    assertTrue(adviceCalled.get());
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Assert.assertThat(org.junit.Assert.assertThat) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Method(java.lang.reflect.Method) Header(org.springframework.messaging.handler.annotation.Header) StopWatch(org.springframework.util.StopWatch) UUID(java.util.UUID) EnableIntegration(org.springframework.integration.config.EnableIntegration) Collectors(java.util.stream.Collectors) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) AdditionalAnswers.returnsFirstArg(org.mockito.AdditionalAnswers.returnsFirstArg) Configuration(org.springframework.context.annotation.Configuration) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Payload(org.springframework.messaging.handler.annotation.Payload) LogFactory(org.apache.commons.logging.LogFactory) Mockito.mock(org.mockito.Mockito.mock) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) MessagingException(org.springframework.messaging.MessagingException) UseSpelInvoker(org.springframework.integration.annotation.UseSpelInvoker) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Assert.assertSame(org.junit.Assert.assertSame) LinkedHashMap(java.util.LinkedHashMap) MessageBuilder(org.springframework.integration.support.MessageBuilder) Message(org.springframework.messaging.Message) ExpectedException(org.junit.rules.ExpectedException) Description(org.hamcrest.Description) Properties(java.util.Properties) Assert.assertNotNull(org.junit.Assert.assertNotNull) MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SpelParserConfiguration(org.springframework.expression.spel.SpelParserConfiguration) ApplicationContext(org.springframework.context.ApplicationContext) MessageHeaders(org.springframework.messaging.MessageHeaders) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) GatewayProxyFactoryBean(org.springframework.integration.gateway.GatewayProxyFactoryBean) BeanFactory(org.springframework.beans.factory.BeanFactory) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Expression(org.springframework.expression.Expression) Log(org.apache.commons.logging.Log) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelCompilerMode(org.springframework.expression.spel.SpelCompilerMode) Collections(java.util.Collections) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GenericMessage(org.springframework.messaging.support.GenericMessage) Header(org.springframework.messaging.handler.annotation.Header) Payload(org.springframework.messaging.handler.annotation.Payload) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)57 Test (org.junit.Test)30 MethodInvocation (org.aopalliance.intercept.MethodInvocation)28 List (java.util.List)20 Method (java.lang.reflect.Method)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 ProxyFactory (org.springframework.aop.framework.ProxyFactory)11 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