use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class InjectorImpl method getProvider.
public <T> Provider<T> getProvider(final Key<T> key) {
Errors errors = new Errors(key);
try {
Provider<T> result = getProviderOrThrow(key, errors);
errors.throwIfNewErrors(0);
return result;
} catch (ErrorsException e) {
throw new ConfigurationException(errors.merge(e.getErrors()).getMessages());
}
}
use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class BinderTest method testPartialInjectorGetInstance.
public void testPartialInjectorGetInstance() {
Injector injector = Guice.createInjector();
try {
injector.getInstance(MissingParameter.class);
fail();
} catch (ConfigurationException expected) {
assertContains(expected.getMessage(), "1) Could not find a suitable constructor in " + NoInjectConstructor.class.getName(), "at " + MissingParameter.class.getName() + ".<init>(BinderTest.java:");
}
}
use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class InjectionPoint method forStaticMethodsAndFields.
/**
* Returns all static method and field injection points on {@code type}.
*
* @return a possibly empty set of injection points. The set has a specified iteration order. All
* fields are returned and then all methods. Within the fields, supertype fields are returned
* before subtype fields. Similarly, supertype methods are returned before subtype methods.
* @throws ConfigurationException if there is a malformed injection point on {@code type}, such as
* a field with multiple binding annotations. The exception's {@link
* ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>}
* of the valid injection points.
*/
public static Set<InjectionPoint> forStaticMethodsAndFields(TypeLiteral<?> type) {
Errors errors = new Errors();
Set<InjectionPoint> result;
if (type.getRawType().isInterface()) {
errors.staticInjectionOnInterface(type.getRawType());
result = null;
} else {
result = getInjectionPoints(type, true, errors);
}
if (errors.hasErrors()) {
throw new ConfigurationException(errors.getMessages()).withPartialValue(result);
}
return result;
}
use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class FactoryProvider2 method findMatchingConstructorInjectionPoint.
/**
* Finds a constructor suitable for the method. If the implementation contained any constructors
* marked with {@link AssistedInject}, this requires all {@link Assisted} parameters to exactly
* match the parameters (in any order) listed in the method. Otherwise, if no
* {@link AssistedInject} constructors exist, this will default to looking for an
* {@literal @}{@link Inject} constructor.
*/
private <T> InjectionPoint findMatchingConstructorInjectionPoint(Method method, Key<?> returnType, TypeLiteral<T> implementation, List<Key<?>> paramList) throws ErrorsException {
Errors errors = new Errors(method);
if (returnType.getTypeLiteral().equals(implementation)) {
errors = errors.withSource(implementation);
} else {
errors = errors.withSource(returnType).withSource(implementation);
}
Class<?> rawType = implementation.getRawType();
if (Modifier.isInterface(rawType.getModifiers())) {
errors.addMessage("%s is an interface, not a concrete class. Unable to create AssistedInject factory.", implementation);
throw errors.toException();
} else if (Modifier.isAbstract(rawType.getModifiers())) {
errors.addMessage("%s is abstract, not a concrete class. Unable to create AssistedInject factory.", implementation);
throw errors.toException();
} else if (Classes.isInnerClass(rawType)) {
errors.cannotInjectInnerClass(rawType);
throw errors.toException();
}
Constructor<?> matchingConstructor = null;
boolean anyAssistedInjectConstructors = false;
// Look for AssistedInject constructors...
for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {
if (constructor.isAnnotationPresent(AssistedInject.class)) {
anyAssistedInjectConstructors = true;
if (constructorHasMatchingParams(implementation, constructor, paramList, errors)) {
if (matchingConstructor != null) {
errors.addMessage("%s has more than one constructor annotated with @AssistedInject" + " that matches the parameters in method %s. Unable to create " + "AssistedInject factory.", implementation, method);
throw errors.toException();
} else {
matchingConstructor = constructor;
}
}
}
}
if (!anyAssistedInjectConstructors) {
// If none existed, use @Inject.
try {
return InjectionPoint.forConstructorOf(implementation);
} catch (ConfigurationException e) {
errors.merge(e.getErrorMessages());
throw errors.toException();
}
} else {
// Otherwise, use it or fail with a good error message.
if (matchingConstructor != null) {
// safe because we got the constructor from this implementation.
@SuppressWarnings("unchecked") InjectionPoint ip = InjectionPoint.forConstructor((Constructor<? super T>) matchingConstructor, implementation);
return ip;
} else {
errors.addMessage("%s has @AssistedInject constructors, but none of them match the" + " parameters in method %s. Unable to create AssistedInject factory.", implementation, method);
throw errors.toException();
}
}
}
use of com.google.inject.ConfigurationException in project roboguice by roboguice.
the class ModuleRewriterTest method testRewriteBindings.
public void testRewriteBindings() {
// create a module the binds String.class and CharSequence.class
Module module = new AbstractModule() {
protected void configure() {
bind(String.class).toInstance("Pizza");
bind(CharSequence.class).toInstance("Wine");
}
};
// record the elements from that module
List<Element> elements = Elements.getElements(module);
// create a rewriter that rewrites the binding to 'Wine' with a binding to 'Beer'
List<Element> rewritten = Lists.newArrayList();
for (Element element : elements) {
element = element.acceptVisitor(new DefaultElementVisitor<Element>() {
@Override
public <T> Element visit(Binding<T> binding) {
T target = binding.acceptTargetVisitor(Elements.<T>getInstanceVisitor());
if ("Wine".equals(target)) {
return null;
} else {
return binding;
}
}
});
if (element != null) {
rewritten.add(element);
}
}
// create a module from the original list of elements and the rewriter
Module rewrittenModule = Elements.getModule(rewritten);
// the wine binding is dropped
Injector injector = Guice.createInjector(rewrittenModule);
try {
injector.getInstance(CharSequence.class);
fail();
} catch (ConfigurationException expected) {
}
}
Aggregations