use of org.aopalliance.intercept.MethodInvocation in project ratpack by ratpack.
the class TimedMethodInterceptor method invoke.
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
String timerTag = buildTimerTag(invocation.getMethod().getAnnotation(Timed.class), invocation.getMethod());
final Timer timer = metricRegistry.timer(timerTag);
final Timer.Context timerContext = timer.time();
Object result;
try {
result = invocation.proceed();
if (result instanceof Promise<?>) {
result = ((Promise<?>) result).time(duration -> timer.update(duration.getNano(), TimeUnit.NANOSECONDS));
} else {
timerContext.stop();
}
} catch (Exception e) {
timerContext.stop();
throw e;
}
return result;
}
use of org.aopalliance.intercept.MethodInvocation in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testDeclaredException.
@Test
public void testDeclaredException() throws Throwable {
final Exception expectedException = new Exception();
// Test return value
MethodInterceptor mi = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
throw expectedException;
}
};
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
pc.addAdvice(mi);
// We don't care about the object
mockTargetSource.setTarget(new Object());
pc.setTargetSource(mockTargetSource);
AopProxy aop = createAopProxy(pc);
try {
ITestBean tb = (ITestBean) aop.getProxy();
// Note: exception param below isn't used
tb.exceptional(expectedException);
fail("Should have thrown exception raised by interceptor");
} catch (Exception thrown) {
assertEquals("exception matches", expectedException, thrown);
}
}
use of org.aopalliance.intercept.MethodInvocation 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 = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
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 LinkedList<>();
@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);
assertEquals(name1, tb.getName());
it.setName(name2);
// NameReverter saved it back
assertEquals(name1, it.getName());
assertEquals(2, saver.names.size());
assertEquals(name2, saver.names.get(0));
assertEquals(name1, saver.names.get(1));
}
use of org.aopalliance.intercept.MethodInvocation in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testCanGetFactoryReferenceAndManipulate.
@Test
public void testCanGetFactoryReferenceAndManipulate() {
ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test1");
assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(config.getObjectType()));
assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("test1")));
// Trigger lazy initialization.
config.getObject();
assertEquals("Have one advisors", 1, config.getAdvisors().length);
assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(config.getObjectType()));
assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("test1")));
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, new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
throw ex;
}
});
assertEquals("Have correct advisor count", 2, config.getAdvisors().length);
tb = (ITestBean) factory.getBean("test1");
try {
// Will fail now
tb.toString();
fail("Evil interceptor added programmatically should fail all method calls");
} catch (Exception thrown) {
assertTrue(thrown == ex);
}
}
use of org.aopalliance.intercept.MethodInvocation 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 = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if (!context) {
assertNoInvocationContext();
} else {
assertNotNull("have context", ExposeInvocationInterceptor.currentInvocation());
}
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();
assertSame("correct return value", s, tb.getName());
}
Aggregations