Search in sources :

Example 31 with ConfigurationException

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

Example 32 with ConfigurationException

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

Example 33 with ConfigurationException

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

Example 34 with ConfigurationException

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

Example 35 with ConfigurationException

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

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