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()]);
}
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);
}
}
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);
}
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());
}
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());
}
Aggregations