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