Search in sources :

Example 46 with MethodInvocation

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

the class ApplicationContextEventTests method testEventPublicationInterceptor.

@Test
public void testEventPublicationInterceptor() throws Throwable {
    MethodInvocation invocation = mock(MethodInvocation.class);
    ApplicationContext ctx = mock(ApplicationContext.class);
    EventPublicationInterceptor interceptor = new EventPublicationInterceptor();
    interceptor.setApplicationEventClass(MyEvent.class);
    interceptor.setApplicationEventPublisher(ctx);
    interceptor.afterPropertiesSet();
    given(invocation.proceed()).willReturn(new Object());
    given(invocation.getThis()).willReturn(new Object());
    interceptor.invoke(invocation);
    verify(ctx).publishEvent(isA(MyEvent.class));
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 47 with MethodInvocation

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

the class RmiSupportTests method remoteInvocation.

@Test
public void remoteInvocation() throws NoSuchMethodException {
    // let's see if the remote invocation object works
    final RemoteBean rb = new RemoteBean();
    final Method setNameMethod = rb.getClass().getDeclaredMethod("setName", String.class);
    MethodInvocation mi = new MethodInvocation() {

        @Override
        public Method getMethod() {
            return setNameMethod;
        }

        @Override
        public Object[] getArguments() {
            return new Object[] { "bla" };
        }

        @Override
        public Object proceed() throws Throwable {
            throw new UnsupportedOperationException();
        }

        @Override
        public Object getThis() {
            return rb;
        }

        @Override
        public AccessibleObject getStaticPart() {
            return setNameMethod;
        }
    };
    RemoteInvocation inv = new RemoteInvocation(mi);
    assertEquals("setName", inv.getMethodName());
    assertEquals("bla", inv.getArguments()[0]);
    assertEquals(String.class, inv.getParameterTypes()[0]);
    // this is a bit BS, but we need to test it
    inv = new RemoteInvocation();
    inv.setArguments(new Object[] { "bla" });
    assertEquals("bla", inv.getArguments()[0]);
    inv.setMethodName("setName");
    assertEquals("setName", inv.getMethodName());
    inv.setParameterTypes(new Class<?>[] { String.class });
    assertEquals(String.class, inv.getParameterTypes()[0]);
    inv = new RemoteInvocation("setName", new Class<?>[] { String.class }, new Object[] { "bla" });
    assertEquals("bla", inv.getArguments()[0]);
    assertEquals("setName", inv.getMethodName());
    assertEquals(String.class, inv.getParameterTypes()[0]);
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 48 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project dubbo by alibaba.

the class RmiProtocol method doRefer.

@SuppressWarnings("unchecked")
protected <T> T doRefer(final Class<T> serviceType, final URL url) throws RpcException {
    final RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
    // RMI needs extra parameter since it uses customized remote invocation object
    if (url.getParameter(Constants.DUBBO_VERSION_KEY, Version.getVersion()).equals(Version.getVersion())) {
        // Check dubbo version on provider, this feature only support
        rmiProxyFactoryBean.setRemoteInvocationFactory(new RemoteInvocationFactory() {

            public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
                return new RmiRemoteInvocation(methodInvocation);
            }
        });
    }
    rmiProxyFactoryBean.setServiceUrl(url.toIdentityString());
    rmiProxyFactoryBean.setServiceInterface(serviceType);
    rmiProxyFactoryBean.setCacheStub(true);
    rmiProxyFactoryBean.setLookupStubOnStartup(true);
    rmiProxyFactoryBean.setRefreshStubOnConnectFailure(true);
    rmiProxyFactoryBean.afterPropertiesSet();
    return (T) rmiProxyFactoryBean.getObject();
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) RemoteInvocationFactory(org.springframework.remoting.support.RemoteInvocationFactory) MethodInvocation(org.aopalliance.intercept.MethodInvocation) RmiProxyFactoryBean(org.springframework.remoting.rmi.RmiProxyFactoryBean)

Example 49 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project leopard by tanhaichao.

the class TopnbInterceptorTest method invoke.

// @Test
// public void getContext() {
// Assert.assertNotNull(TopnbInterceptor.getContext("applicationContext.xml"));
// Assert.assertNotNull(TopnbInterceptor.getContext("applicationContext.xml", false));
// Assert.assertNotNull(TopnbInterceptor.getContext("applicationContext.xml", true));
// }
@Test
public void invoke() throws Throwable {
    MethodInvocation invocation = new MethodInvocation() {

        @Override
        public Object[] getArguments() {
            return null;
        }

        @Override
        public AccessibleObject getStaticPart() {
            return null;
        }

        @Override
        public Object getThis() {
            return TopnbInterceptorTest.this;
        }

        @Override
        public Object proceed() throws Throwable {
            return "ok";
        }

        @Override
        public Method getMethod() {
            try {
                return TopnbInterceptorTest.class.getMethod("invoke");
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    };
    Object result = new MethodTimeInterceptor().invoke(invocation);
    Assert.assertEquals("ok", result);
}
Also used : MethodTimeInterceptor(io.leopard.topnb.methodtime.MethodTimeInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AccessibleObject(java.lang.reflect.AccessibleObject) Test(org.junit.Test) PerformanceUtilTest(io.leopard.topnb.methodtime.PerformanceUtilTest)

Example 50 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project guice by google.

the class BindingTest method testToConstructorAndMethodInterceptors.

/*if[AOP]*/
public void testToConstructorAndMethodInterceptors() throws NoSuchMethodException {
    final Constructor<D> constructor = D.class.getConstructor(Stage.class);
    final AtomicInteger count = new AtomicInteger();
    final MethodInterceptor countingInterceptor = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            count.incrementAndGet();
            return methodInvocation.proceed();
        }
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Object.class).toConstructor(constructor);
            bindInterceptor(Matchers.any(), Matchers.any(), countingInterceptor);
        }
    });
    D d = (D) injector.getInstance(Object.class);
    d.hashCode();
    d.hashCode();
    assertEquals(2, count.get());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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