use of com.google.inject.ConfigurationException in project guice by google.
the class MembersInjectorStore method createWithListeners.
/**
* Creates a new members injector and attaches both injection listeners and method aspects.
*/
private <T> MembersInjectorImpl<T> createWithListeners(TypeLiteral<T> type, Errors errors) throws ErrorsException {
int numErrorsBefore = errors.size();
Set<InjectionPoint> injectionPoints;
try {
injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
} catch (ConfigurationException e) {
errors.merge(e.getErrorMessages());
injectionPoints = e.getPartialValue();
}
ImmutableList<SingleMemberInjector> injectors = getInjectors(injectionPoints, errors);
errors.throwIfNewErrors(numErrorsBefore);
EncounterImpl<T> encounter = new EncounterImpl<>(errors, injector.lookups);
Set<TypeListener> alreadySeenListeners = Sets.newHashSet();
for (TypeListenerBinding binding : typeListenerBindings) {
TypeListener typeListener = binding.getListener();
if (!alreadySeenListeners.contains(typeListener) && binding.getTypeMatcher().matches(type)) {
alreadySeenListeners.add(typeListener);
try {
typeListener.hear(type, encounter);
} catch (RuntimeException e) {
errors.errorNotifyingTypeListener(binding, type, e);
}
}
}
encounter.invalidate();
errors.throwIfNewErrors(numErrorsBefore);
return new MembersInjectorImpl<T>(injector, type, encounter, injectors);
}
use of com.google.inject.ConfigurationException in project guice by google.
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(getSharedTypeArguments(parameterizedType)[0]));
return guiceProviderType;
}
@SuppressWarnings("unchecked") TypeLiteral<T> wrappedPrimitives = (TypeLiteral<T>) PRIMITIVE_TO_WRAPPER.get(typeLiteral);
if (wrappedPrimitives != null) {
return wrappedPrimitives;
}
// If we know this isn't a subclass, return as-is.
if (typeLiteral.getClass() == TypeLiteral.class) {
return typeLiteral;
}
// recreate the TypeLiteral to avoid anonymous TypeLiterals from holding refs to their
// surrounding classes.
@SuppressWarnings("unchecked") TypeLiteral<T> recreated = (TypeLiteral<T>) TypeLiteral.get(typeLiteral.getType());
return recreated;
}
use of com.google.inject.ConfigurationException in project guice by google.
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 guice by google.
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(), "No injectable constructor for type Integer.");
}
}
use of com.google.inject.ConfigurationException in project guice by google.
the class BoundFieldModuleTest method testFieldBoundAsNonTransparentProvider_lazy.
public void testFieldBoundAsNonTransparentProvider_lazy() {
LazyNonTransparentProvider instance = new LazyNonTransparentProvider();
BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);
assertNull(injector.getInstance(IntegerProvider.class));
instance.anIntProvider = new IntegerProvider(3);
assertEquals(3, injector.getInstance(IntegerProvider.class).get().intValue());
try {
injector.getInstance(Integer.class);
fail();
} catch (ConfigurationException expected) {
// expected because we don't interpret IntegerProvider as a Provider<Integer>
}
}
Aggregations