Search in sources :

Example 46 with ConfigurationException

use of com.google.inject.ConfigurationException in project roboguice by roboguice.

the class InjectorImpl method getBinding.

/**
 * Returns the binding for {@code key}
 */
public <T> BindingImpl<T> getBinding(Key<T> key) {
    Errors errors = new Errors(key);
    try {
        BindingImpl<T> result = getBindingOrThrow(key, errors, JitLimitation.EXISTING_JIT);
        errors.throwConfigurationExceptionIfErrorsExist();
        return result;
    } catch (ErrorsException e) {
        throw new ConfigurationException(errors.merge(e.getErrors()).getMessages());
    }
}
Also used : ConfigurationException(com.google.inject.ConfigurationException)

Example 47 with ConfigurationException

use of com.google.inject.ConfigurationException in project roboguice by roboguice.

the class BindingBuilder method toConstructor.

public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor, TypeLiteral<? extends S> type) {
    checkNotNull(constructor, "constructor");
    checkNotNull(type, "type");
    checkNotTargetted();
    BindingImpl<T> base = getBinding();
    Set<InjectionPoint> injectionPoints;
    try {
        injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
    } catch (ConfigurationException e) {
        copyErrorsToBinder(e);
        injectionPoints = e.getPartialValue();
    }
    try {
        InjectionPoint constructorPoint = InjectionPoint.forConstructor(constructor, type);
        setBinding(new ConstructorBindingImpl<T>(base.getKey(), base.getSource(), base.getScoping(), constructorPoint, injectionPoints));
    } catch (ConfigurationException e) {
        copyErrorsToBinder(e);
    }
    return this;
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) ConfigurationException(com.google.inject.ConfigurationException)

Example 48 with ConfigurationException

use of com.google.inject.ConfigurationException in project roboguice by roboguice.

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<T>(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 49 with ConfigurationException

use of com.google.inject.ConfigurationException in project opt4j by felixreimann.

the class Opt4JModule method configure.

/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.inject.AbstractModule#configure()
	 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void configure() {
    /**
     * Configure injected constants.
     */
    PropertyModule module = new PropertyModule(this);
    for (Property property : module.getProperties()) {
        for (Annotation annotation : property.getAnnotations()) {
            if (annotation.annotationType().getAnnotation(BindingAnnotation.class) != null) {
                Class<?> type = property.getType();
                Object value = property.getValue();
                ConstantBindingBuilder builder = bindConstant(annotation);
                if (type.equals(Integer.TYPE)) {
                    builder.to((Integer) value);
                } else if (type.equals(Long.TYPE)) {
                    builder.to((Long) value);
                } else if (type.equals(Double.TYPE)) {
                    builder.to((Double) value);
                } else if (type.equals(Float.TYPE)) {
                    builder.to((Float) value);
                } else if (type.equals(Byte.TYPE)) {
                    builder.to((Byte) value);
                } else if (type.equals(Short.TYPE)) {
                    builder.to((Short) value);
                } else if (type.equals(Boolean.TYPE)) {
                    builder.to((Boolean) value);
                } else if (type.equals(Character.TYPE)) {
                    builder.to((Character) value);
                } else if (type.equals(String.class)) {
                    builder.to((String) value);
                } else if (type.equals(Class.class)) {
                    builder.to((Class<?>) value);
                } else if (value instanceof Enum<?>) {
                    builder.to((Enum) value);
                } else {
                    String message = "Constant type not bindable: " + type + " of field " + property.getName() + " in module " + this.getClass().getName();
                    throw new ConfigurationException(Arrays.asList(new Message(message)));
                }
            }
        }
    }
    multi(OptimizerStateListener.class);
    multi(OptimizerIterationListener.class);
    multi(IndividualStateListener.class);
    config();
}
Also used : Message(com.google.inject.spi.Message) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) ConstantBindingBuilder(com.google.inject.binder.ConstantBindingBuilder) ConfigurationException(com.google.inject.ConfigurationException) BindingAnnotation(com.google.inject.BindingAnnotation) PropertyModule(org.opt4j.core.config.PropertyModule) Property(org.opt4j.core.config.Property)

Example 50 with ConfigurationException

use of com.google.inject.ConfigurationException in project guice by google.

the class BindingBuilder method toConstructor.

@Override
public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor, TypeLiteral<? extends S> type) {
    checkNotNull(constructor, "constructor");
    checkNotNull(type, "type");
    checkNotTargetted();
    BindingImpl<T> base = getBinding();
    Set<InjectionPoint> injectionPoints;
    try {
        injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
    } catch (ConfigurationException e) {
        copyErrorsToBinder(e);
        injectionPoints = e.getPartialValue();
    }
    try {
        InjectionPoint constructorPoint = InjectionPoint.forConstructor(constructor, type);
        setBinding(new ConstructorBindingImpl<T>(base.getKey(), base.getSource(), base.getScoping(), constructorPoint, injectionPoints));
    } catch (ConfigurationException e) {
        copyErrorsToBinder(e);
    }
    return this;
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) ConfigurationException(com.google.inject.ConfigurationException)

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