use of com.google.inject.spi.Element in project guice by google.
the class SpiUtils method setModuleTest.
@SuppressWarnings("unchecked")
private static <T> void setModuleTest(Key<Set<T>> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
Key<?> collectionOfProvidersKey = setKey.ofType(collectionOfProvidersOf(elementType));
Key<?> collectionOfJavaxProvidersKey = setKey.ofType(collectionOfJavaxProvidersOf(elementType));
List<BindResult> bindResults = Lists.newArrayList(results);
List<Element> elements = Elements.getElements(modules);
Visitor<T> visitor = new Visitor<T>();
MultibinderBinding<Set<T>> multibinder = null;
for (Element element : elements) {
if (element instanceof Binding && ((Binding) element).getKey().equals(setKey)) {
multibinder = (MultibinderBinding<Set<T>>) ((Binding) element).acceptTargetVisitor(visitor);
break;
}
}
assertNotNull(multibinder);
assertEquals(elementType, multibinder.getElementTypeLiteral());
List<Object> otherMultibinders = Lists.newArrayList();
Set<Element> otherContains = new HashSet<Element>();
List<Element> otherElements = Lists.newArrayList();
int duplicates = 0;
Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
Indexer indexer = new Indexer(null);
boolean collectionOfProvidersMatch = false;
boolean collectionOfJavaxProvidersMatch = false;
for (Element element : elements) {
boolean contains = multibinder.containsElement(element);
if (!contains) {
otherElements.add(element);
}
boolean matched = false;
Key key = null;
if (element instanceof Binding) {
Binding binding = (Binding) element;
if (indexer.isIndexable(binding) && !setOfIndexed.add((IndexedBinding) binding.acceptTargetVisitor(indexer))) {
duplicates++;
}
key = binding.getKey();
Object visited = binding.acceptTargetVisitor(visitor);
if (visited != null) {
matched = true;
if (visited.equals(multibinder)) {
assertTrue(contains);
} else {
otherMultibinders.add(visited);
}
}
}
if (collectionOfProvidersKey.equals(key)) {
assertTrue(contains);
assertFalse(matched);
collectionOfProvidersMatch = true;
} else if (collectionOfJavaxProvidersKey.equals(key)) {
assertTrue(contains);
assertFalse(matched);
collectionOfJavaxProvidersMatch = true;
} else if (!matched && contains) {
otherContains.add(element);
}
}
if (allowDuplicates) {
assertEquals("wrong contained elements: " + otherContains, bindResults.size() + 1 + duplicates, otherContains.size());
} else {
assertEquals("wrong contained elements: " + otherContains, bindResults.size() + duplicates, otherContains.size());
}
assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
assertTrue(collectionOfProvidersMatch);
assertTrue(collectionOfJavaxProvidersMatch);
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(otherElements));
}
use of com.google.inject.spi.Element in project shiro by apache.
the class ShiroAopModuleTest method testGetAnnotationResolver.
@Test
public void testGetAnnotationResolver() {
final AnnotationResolver annotationResolver = new DefaultAnnotationResolver();
ShiroAopModule underTest = new ShiroAopModule() {
@Override
protected AnnotationResolver createAnnotationResolver() {
return annotationResolver;
}
@Override
protected void configureDefaultInterceptors(AnnotationResolver resolver) {
assertSame(annotationResolver, resolver);
bind(Object.class).annotatedWith(Names.named("configureDefaultInterceptors"));
}
@Override
protected void configureInterceptors(AnnotationResolver resolver) {
assertSame(annotationResolver, resolver);
bind(Object.class).annotatedWith(Names.named("configureInterceptors"));
}
};
boolean calledDefault = false;
boolean calledCustom = false;
for (Element e : Elements.getElements(underTest)) {
if (e instanceof Binding) {
Key key = ((Binding) e).getKey();
if (Named.class.isAssignableFrom(key.getAnnotation().annotationType()) && "configureInterceptors".equals(((Named) key.getAnnotation()).value()) && key.getTypeLiteral().getRawType().equals(Object.class)) {
calledCustom = true;
}
if (Named.class.isAssignableFrom(key.getAnnotation().annotationType()) && "configureDefaultInterceptors".equals(((Named) key.getAnnotation()).value()) && key.getTypeLiteral().getRawType().equals(Object.class)) {
calledDefault = true;
}
}
}
}
use of com.google.inject.spi.Element in project guice by google.
the class PrivateElementsImpl method applyTo.
@Override
public void applyTo(Binder binder) {
PrivateBinder privateBinder = binder.withSource(source).newPrivateBinder();
for (Element element : getElements()) {
element.applyTo(privateBinder);
}
// ensure exposedKeysToSources is populated
getExposedKeys();
for (Map.Entry<Key<?>, Object> entry : exposedKeysToSources.entrySet()) {
privateBinder.withSource(entry.getValue()).expose(entry.getKey());
}
}
use of com.google.inject.spi.Element in project guice by google.
the class MethodInterceptionTest method testGetElements_interceptorBindings.
@Test
public void testGetElements_interceptorBindings() throws Exception {
@SuppressWarnings("rawtypes") Matcher<Class> classMatcher = Matchers.subclassesOf(List.class);
Matcher<Method> methodMatcher = Matchers.returns(Matchers.identicalTo(int.class));
MethodInterceptor interceptor = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
return null;
}
};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(classMatcher, methodMatcher, interceptor);
}
});
final List<InterceptorBinding> interceptorBindings = new ArrayList<>();
for (Element element : injector.getElements()) {
element.acceptVisitor(new DefaultElementVisitor<Void>() {
@Override
public Void visit(InterceptorBinding interceptorBinding) {
interceptorBindings.add(interceptorBinding);
return null;
}
});
}
assertThat(interceptorBindings).hasSize(1);
InterceptorBinding extractedBinding = interceptorBindings.get(0);
assertSame(classMatcher, extractedBinding.getClassMatcher());
assertSame(methodMatcher, extractedBinding.getMethodMatcher());
assertSame(interceptor, extractedBinding.getInterceptors().get(0));
}
use of com.google.inject.spi.Element in project guice by google.
the class ServletScopesTest method testIsRequestScopedNegative.
public void testIsRequestScopedNegative() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<String> e = Key.get(String.class, named("E"));
final Key<String> f = Key.get(String.class, named("F"));
final Key<String> g = Key.get(String.class, named("G"));
final Key<String> h = Key.get(String.class, named("H"));
final Key<String> i = Key.get(String.class, named("I"));
final Key<String> j = Key.get(String.class, named("J"));
Module requestScopedBindings = new AbstractModule() {
@Override
protected void configure() {
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(Scopes.NO_SCOPE);
bind(d).toInstance("d");
bind(e).toProvider(Providers.of("e")).asEagerSingleton();
bind(f).toProvider(Providers.of("f")).in(Scopes.SINGLETON);
bind(g).toProvider(Providers.of("g")).in(Singleton.class);
bind(h).toProvider(Providers.of("h")).in(CustomScoped.class);
bindScope(CustomScoped.class, Scopes.NO_SCOPE);
install(new PrivateModule() {
@Override
protected void configure() {
bind(i).toProvider(Providers.of("i")).in(CustomScoped.class);
expose(i);
}
});
}
@Provides
@Named("J")
@CustomScoped
String provideJ() {
return "j";
}
};
// we know the module contains only bindings
@SuppressWarnings("unchecked") List<Element> moduleBindings = Elements.getElements(requestScopedBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
assertFalse(ServletScopes.isRequestScoped(map.get(a)));
assertFalse(ServletScopes.isRequestScoped(map.get(b)));
assertFalse(ServletScopes.isRequestScoped(map.get(c)));
assertFalse(ServletScopes.isRequestScoped(map.get(d)));
assertFalse(ServletScopes.isRequestScoped(map.get(e)));
assertFalse(ServletScopes.isRequestScoped(map.get(f)));
assertFalse(ServletScopes.isRequestScoped(map.get(g)));
assertFalse(ServletScopes.isRequestScoped(map.get(h)));
assertFalse(ServletScopes.isRequestScoped(map.get(i)));
assertFalse(ServletScopes.isRequestScoped(map.get(j)));
Injector injector = Guice.createInjector(requestScopedBindings);
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(a)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(b)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(c)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(d)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(e)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(f)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(g)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(h)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(i)));
assertFalse(ServletScopes.isRequestScoped(injector.getBinding(j)));
}
Aggregations