use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class MapBinderTest method testMapBinderMultimapWithAnotation.
public void testMapBinderMultimapWithAnotation() {
AbstractModule ab1 = new AbstractModule() {
@Override
protected void configure() {
MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
multibinder.addBinding("a").toInstance("A");
multibinder.addBinding("b").toInstance("B1");
}
};
AbstractModule b2c = new AbstractModule() {
@Override
protected void configure() {
MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
multibinder.addBinding("b").toInstance("B2");
multibinder.addBinding("c").toInstance("C");
multibinder.permitDuplicates();
}
};
Injector injector = Guice.createInjector(ab1, b2c);
assertEquals(mapOf("a", setOf("A"), "b", setOf("B1", "B2"), "c", setOf("C")), injector.getInstance(Key.get(mapOfSetOfString, Abc.class)));
try {
injector.getInstance(Key.get(mapOfSetOfString));
fail();
} catch (ConfigurationException expected) {
}
assertMapVisitor(Key.get(mapOfString, Abc.class), stringType, stringType, setOf(ab1, b2c), BOTH, true, 0, instance("a", "A"), instance("b", "B1"), instance("b", "B2"), instance("c", "C"));
}
use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class BoundFieldModuleTest method testProviderSubclassesDoNotBindParameterizedType.
public void testProviderSubclassesDoNotBindParameterizedType() {
final Integer testValue = 1024;
Object instance = new Object() {
@Bind
private IntegerProvider anIntProvider = new IntegerProvider(testValue);
};
BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);
try {
injector.getInstance(Integer.class);
fail();
} catch (ConfigurationException e) {
assertContains(e.getMessage(), "Could not find a suitable constructor in java.lang.Integer.");
}
}
use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class BoundFieldModuleTest method testBindingSuperTypeAccessSubType.
public void testBindingSuperTypeAccessSubType() {
final Integer testValue = 1024;
Object instance = new Object() {
@Bind(to = Number.class)
private Integer anInt = testValue;
};
BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);
try {
injector.getInstance(Integer.class);
} catch (ConfigurationException e) {
assertContains(e.getMessage(), "Could not find a suitable constructor in java.lang.Integer");
}
}
use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class MoreTypes method canonicalizeForKey.
/**
* Returns an type that's appropriate for use in a key.
*
* <p>If the raw type of {@code typeLiteral} is a {@code javax.inject.Provider}, this returns a
* {@code com.google.inject.Provider} with the same type parameters.
*
* <p>If the type is a primitive, the corresponding wrapper type will be returned.
*
* @throws ConfigurationException if {@code type} contains a type variable
*/
public static <T> TypeLiteral<T> canonicalizeForKey(TypeLiteral<T> typeLiteral) {
Type type = typeLiteral.getType();
if (!isFullySpecified(type)) {
Errors errors = new Errors().keyNotFullySpecified(typeLiteral);
throw new ConfigurationException(errors.getMessages());
}
if (typeLiteral.getRawType() == javax.inject.Provider.class) {
ParameterizedType parameterizedType = (ParameterizedType) type;
// the following casts are generally unsafe, but com.google.inject.Provider extends
// javax.inject.Provider and is covariant
@SuppressWarnings("unchecked") TypeLiteral<T> guiceProviderType = (TypeLiteral<T>) TypeLiteral.get(Types.providerOf(parameterizedType.getActualTypeArguments()[0]));
return guiceProviderType;
}
@SuppressWarnings("unchecked") TypeLiteral<T> wrappedPrimitives = (TypeLiteral<T>) PRIMITIVE_TO_WRAPPER.get(typeLiteral);
return wrappedPrimitives != null ? wrappedPrimitives : typeLiteral;
}
use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class ConstructorBindingImpl method create.
/**
* @param constructorInjector the constructor to use, or {@code null} to use the default.
* @param failIfNotLinked true if this ConstructorBindingImpl's InternalFactory should
* only succeed if retrieved from a linked binding
*/
static <T> ConstructorBindingImpl<T> create(InjectorImpl injector, Key<T> key, InjectionPoint constructorInjector, Object source, Scoping scoping, Errors errors, boolean failIfNotLinked, boolean failIfNotExplicit) throws ErrorsException {
int numErrors = errors.size();
// constructorBinding guarantees type is consistent
@SuppressWarnings("unchecked") Class<? super T> rawType = constructorInjector == null ? key.getTypeLiteral().getRawType() : (Class) constructorInjector.getDeclaringType().getRawType();
// We can't inject abstract classes.
if (Modifier.isAbstract(rawType.getModifiers())) {
errors.missingImplementation(key);
}
// Error: Inner class.
if (Classes.isInnerClass(rawType)) {
errors.cannotInjectInnerClass(rawType);
}
errors.throwIfNewErrors(numErrors);
// Find a constructor annotated @Inject
if (constructorInjector == null) {
try {
constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral());
if (failIfNotExplicit && !hasAtInject((Constructor) constructorInjector.getMember())) {
errors.atInjectRequired(rawType);
}
} catch (ConfigurationException e) {
throw errors.merge(e.getErrorMessages()).toException();
}
}
// if no scope is specified, look for a scoping annotation on the concrete class
if (!scoping.isExplicitlyScoped()) {
Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass();
Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType);
if (scopeAnnotation != null) {
scoping = Scoping.makeInjectable(Scoping.forAnnotation(scopeAnnotation), injector, errors.withSource(rawType));
}
}
errors.throwIfNewErrors(numErrors);
Factory<T> factoryFactory = new Factory<T>(failIfNotLinked, key);
InternalFactory<? extends T> scopedFactory = Scoping.scope(key, injector, factoryFactory, source, scoping);
return new ConstructorBindingImpl<T>(injector, key, source, scopedFactory, scoping, factoryFactory, constructorInjector);
}
Aggregations