Search in sources :

Example 51 with MethodInvocation

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;
}
Also used : Inject(javax.inject.Inject) TimeUnit(java.util.concurrent.TimeUnit) Timed(com.codahale.metrics.annotation.Timed) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MetricRegistry(com.codahale.metrics.MetricRegistry) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Timer(com.codahale.metrics.Timer) Promise(ratpack.exec.Promise) Method(java.lang.reflect.Method) Promise(ratpack.exec.Promise) Timer(com.codahale.metrics.Timer) Timed(com.codahale.metrics.annotation.Timed)

Example 52 with MethodInvocation

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);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Test(org.junit.Test)

Example 53 with MethodInvocation

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));
}
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) List(java.util.List) LinkedList(java.util.LinkedList) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.Test)

Example 54 with MethodInvocation

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);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LockedException(test.mixin.LockedException) Test(org.junit.Test)

Example 55 with MethodInvocation

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

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)84 Test (org.junit.Test)59 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)22 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)15 ITestBean (org.springframework.tests.sample.beans.ITestBean)13 Method (java.lang.reflect.Method)11 Log (org.apache.commons.logging.Log)9 EvaluationContext (org.springframework.expression.EvaluationContext)9 Expression (org.springframework.expression.Expression)9 PreInvocationExpressionAttribute (org.springframework.security.access.expression.method.PreInvocationExpressionAttribute)9 Authentication (org.springframework.security.core.Authentication)9 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 TestBean (org.springframework.tests.sample.beans.TestBean)9 IOException (java.io.IOException)8 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)7 FileNotFoundException (java.io.FileNotFoundException)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)5 AccessibleObject (java.lang.reflect.AccessibleObject)4 ConnectException (java.rmi.ConnectException)4 RemoteException (java.rmi.RemoteException)4