Search in sources :

Example 26 with ConfigurationException

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

Example 27 with ConfigurationException

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

Example 28 with ConfigurationException

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

Example 29 with ConfigurationException

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;
}
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 30 with ConfigurationException

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

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