use of com.google.inject.ConfigurationException in project guice by google.
the class FactoryProvider method newFactory.
public static <F> Provider<F> newFactory(TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
Map<Method, AssistedConstructor<?>> factoryMethodToConstructor = createMethodMapping(factoryType, implementationType);
if (!factoryMethodToConstructor.isEmpty()) {
return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
} else {
BindingCollector collector = new BindingCollector();
// Preserving backwards-compatibility: Map all return types in a factory
// interface to the passed implementation type.
Errors errors = new Errors();
Key<?> implementationKey = Key.get(implementationType);
try {
for (Method method : factoryType.getRawType().getMethods()) {
Key<?> returnType = getKey(factoryType.getReturnType(method), method, method.getAnnotations(), errors);
if (!implementationKey.equals(returnType)) {
collector.addBinding(returnType, implementationType);
}
}
} catch (ErrorsException e) {
throw new ConfigurationException(e.getErrors().getMessages());
}
return new FactoryProvider2<F>(Key.get(factoryType), collector, /* userLookups= */
null);
}
}
use of com.google.inject.ConfigurationException in project guice by google.
the class InjectorImpl method getProvider.
@Override
public <T> Provider<T> getProvider(final Key<T> key) {
checkNotNull(key, "key");
Errors errors = new Errors(key);
try {
Provider<T> result = getProviderOrThrow(Dependency.get(key), errors);
errors.throwIfNewErrors(0);
return result;
} catch (ErrorsException e) {
ConfigurationException exception = new ConfigurationException(errors.merge(e.getErrors()).getMessages());
throw exception;
}
}
use of com.google.inject.ConfigurationException in project guice by google.
the class InjectorImpl method getBinding.
/**
* Returns the binding for {@code key}
*/
@Override
public <T> BindingImpl<T> getBinding(Key<T> key) {
Errors errors = new Errors(checkNotNull(key, "key"));
try {
BindingImpl<T> result = getBindingOrThrow(key, errors, JitLimitation.EXISTING_JIT);
errors.throwConfigurationExceptionIfErrorsExist();
return result;
} catch (ErrorsException e) {
ConfigurationException exception = new ConfigurationException(errors.merge(e.getErrors()).getMessages());
throw exception;
}
}
use of com.google.inject.ConfigurationException in project guice by google.
the class InjectorImpl method getMembersInjector.
@Override
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
checkNotNull(typeLiteral, "typeLiteral");
userRequestedMembersInjectorTypes.add(typeLiteral);
Errors errors = new Errors(typeLiteral);
try {
return membersInjectorStore.get(typeLiteral, errors);
} catch (ErrorsException e) {
ConfigurationException exception = new ConfigurationException(errors.merge(e.getErrors()).getMessages());
throw exception;
}
}
use of com.google.inject.ConfigurationException in project guice by google.
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 atInjectRequired) throws ErrorsException {
int numErrors = errors.size();
Class<?> rawType = constructorInjector == null ? key.getTypeLiteral().getRawType() : constructorInjector.getDeclaringType().getRawType();
// We can't inject abstract classes.
if (Modifier.isAbstract(rawType.getModifiers())) {
errors.missingImplementationWithHint(key, injector);
}
// Error: Inner class.
if (Classes.isInnerClass(rawType)) {
errors.cannotInjectInnerClass(rawType);
}
if (KotlinSupport.getInstance().isLocalClass(rawType)) {
errors.cannotInjectLocalClass(rawType);
}
errors.throwIfNewErrors(numErrors);
// Find a constructor annotated @Inject
if (constructorInjector == null) {
try {
constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral(), atInjectRequired);
} 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<>(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