Search in sources :

Example 6 with ConfigurationException

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);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) InjectionPoint(com.google.inject.spi.InjectionPoint) TypeListenerBinding(com.google.inject.spi.TypeListenerBinding) ConfigurationException(com.google.inject.ConfigurationException) TypeListener(com.google.inject.spi.TypeListener)

Example 7 with ConfigurationException

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(com.google.inject.TypeLiteral) ConfigurationException(com.google.inject.ConfigurationException)

Example 8 with ConfigurationException

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;
}
Also used : Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException)

Example 9 with ConfigurationException

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.");
    }
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector)

Example 10 with ConfigurationException

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>
    }
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector)

Aggregations

ConfigurationException (com.google.inject.ConfigurationException)62 Injector (com.google.inject.Injector)16 Errors (com.google.inject.internal.Errors)13 InjectionPoint (com.google.inject.spi.InjectionPoint)8 Method (java.lang.reflect.Method)7 AbstractModule (com.google.inject.AbstractModule)5 Inject (com.google.inject.Inject)5 ErrorsException (com.google.inject.internal.ErrorsException)5 Annotation (java.lang.annotation.Annotation)5 TypeLiteral (com.google.inject.TypeLiteral)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 GuiceUtil (com.google.gwt.inject.rebind.util.GuiceUtil)3 Module (com.google.inject.Module)3 Message (com.google.inject.spi.Message)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Binding (com.google.inject.Binding)2