Search in sources :

Example 86 with MethodInvocation

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

the class FactoryProvider2Test method testMethodInterceptorsOnAssistedTypes.

@Test
public void testMethodInterceptorsOnAssistedTypes() {
    assumeTrue(InternalFlags.isBytecodeGenEnabled());
    final AtomicInteger invocationCount = new AtomicInteger();
    final MethodInterceptor interceptor = new MethodInterceptor() {

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

        @Override
        protected void configure() {
            bindInterceptor(Matchers.any(), Matchers.any(), interceptor);
            bind(Double.class).toInstance(5.0d);
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
        }
    });
    ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class);
    Mustang mustang = (Mustang) factory.create(Color.GREEN);
    assertEquals(0, invocationCount.get());
    mustang.drive();
    assertEquals(1, invocationCount.get());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Injector(com.google.inject.Injector) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test)

Example 87 with MethodInvocation

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

the class BindingTest method testToConstructorAndMethodInterceptors.

@Test
public void testToConstructorAndMethodInterceptors() throws NoSuchMethodException {
    assumeTrue(InternalFlags.isBytecodeGenEnabled());
    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);
    int unused = d.hashCode();
    int unused2 = d.hashCode();
    assertEquals(2, count.get());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodInvocation(org.aopalliance.intercept.MethodInvocation) InjectionPoint(com.google.inject.spi.InjectionPoint) Test(org.junit.Test)

Example 88 with MethodInvocation

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

the class LineNumbersTest method testCanHandleLineNumbersForGuiceGeneratedClasses.

@Test
public void testCanHandleLineNumbersForGuiceGeneratedClasses() {
    assumeTrue(InternalFlags.isBytecodeGenEnabled());
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bindInterceptor(Matchers.only(A.class), Matchers.any(), new MethodInterceptor() {

                    @Override
                    public Object invoke(MethodInvocation methodInvocation) {
                        return null;
                    }
                });
                bind(A.class);
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "No implementation for LineNumbersTest$B was bound.", "for 1st parameter b", "at LineNumbersTest$2.configure");
    }
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test)

Example 89 with MethodInvocation

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

the class ElementsTest method testBindIntercepor.

public void testBindIntercepor() {
    // Unavoidable since subclassesOf returns raw type
    @SuppressWarnings("rawtypes") final Matcher<Class> classMatcher = Matchers.subclassesOf(List.class);
    final Matcher<Object> methodMatcher = Matchers.any();
    final MethodInterceptor methodInterceptor = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation methodInvocation) {
            return null;
        }
    };
    checkModule(new AbstractModule() {

        @Override
        protected void configure() {
            bindInterceptor(classMatcher, methodMatcher, methodInterceptor);
        }
    }, new FailingElementVisitor() {

        @Override
        public Void visit(InterceptorBinding command) {
            assertSame(classMatcher, command.getClassMatcher());
            assertSame(methodMatcher, command.getMethodMatcher());
            assertEquals(Arrays.asList(methodInterceptor), command.getInterceptors());
            return null;
        }
    });
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AbstractModule(com.google.inject.AbstractModule)

Example 90 with MethodInvocation

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

the class JetCacheInterceptorTest method test3.

@Test
public void test3() throws Throwable {
    final Method m = I3.class.getMethod("foo");
    final C3 c = new C3();
    pc.matches(m, C3.class);
    final MethodInvocation mi = mock(MethodInvocation.class);
    when(mi.getMethod()).thenReturn(m);
    when(mi.getThis()).thenReturn(c);
    when(mi.getArguments()).thenReturn(null);
    when(mi.proceed()).thenThrow(new SQLException(""));
    try {
        interceptor.invoke(mi);
        fail("");
    } catch (SQLException e) {
    }
}
Also used : SQLException(java.sql.SQLException) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)124 Test (org.junit.jupiter.api.Test)50 Test (org.junit.Test)36 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)27 Method (java.lang.reflect.Method)24 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)22 ArrayList (java.util.ArrayList)11 Log (org.apache.commons.logging.Log)11 Authentication (org.springframework.security.core.Authentication)10 EvaluationContext (org.springframework.expression.EvaluationContext)9 Expression (org.springframework.expression.Expression)9 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 List (java.util.List)7 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 MyThrowsHandler (org.springframework.aop.testfixture.advice.MyThrowsHandler)7 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)7 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5