Search in sources :

Example 36 with ConfigurationException

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

the class FactoryProvider method newFactory.

public static <F> Provider<F> newFactory(TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
    Map<Method, AssistedConstructor<?>> factoryMethodToConstructor = createMethodMapping(factoryType, implementationType);
    if (!factoryMethodToConstructor.isEmpty()) {
        return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
    } else {
        BindingCollector collector = new BindingCollector();
        // Preserving backwards-compatibility:  Map all return types in a factory
        // interface to the passed implementation type.
        Errors errors = new Errors();
        Key<?> implementationKey = Key.get(implementationType);
        try {
            for (Method method : factoryType.getRawType().getMethods()) {
                Key<?> returnType = getKey(factoryType.getReturnType(method), method, method.getAnnotations(), errors);
                if (!implementationKey.equals(returnType)) {
                    collector.addBinding(returnType, implementationType);
                }
            }
        } catch (ErrorsException e) {
            throw new ConfigurationException(e.getErrors().getMessages());
        }
        return new FactoryProvider2<F>(Key.get(factoryType), collector);
    }
}
Also used : Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException) ErrorsException(com.google.inject.internal.ErrorsException) Method(java.lang.reflect.Method)

Example 37 with ConfigurationException

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

the class InjectionPoint method forMember.

private ImmutableList<Dependency<?>> forMember(Member member, TypeLiteral<?> type, Annotation[][] paramterAnnotations) {
    Errors errors = new Errors(member);
    List<Dependency<?>> dependencies = Lists.newArrayList();
    int index = 0;
    for (TypeLiteral<?> parameterType : type.getParameterTypes(member)) {
        try {
            Annotation[] parameterAnnotations = paramterAnnotations[index];
            Key<?> key = Annotations.getKey(parameterType, member, parameterAnnotations, errors);
            dependencies.add(newDependency(key, Nullability.allowsNull(parameterAnnotations), index));
            index++;
        } catch (ConfigurationException e) {
            errors.merge(e.getErrorMessages());
        } catch (ErrorsException e) {
            errors.merge(e.getErrors());
        }
    }
    errors.throwConfigurationExceptionIfErrorsExist();
    return ImmutableList.copyOf(dependencies);
}
Also used : Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException) ErrorsException(com.google.inject.internal.ErrorsException) Annotation(java.lang.annotation.Annotation)

Example 38 with ConfigurationException

use of com.google.inject.ConfigurationException in project OpenAM by OpenRock.

the class ConfigLoader method validateClass.

/**
     * Validates that the specified class can be loaded and implements the proper interface so that we don't have to do
     * that for every request.
     *
     * @param cfg
     * @return the class of the RequestHandler to be used for this client config, or null if the class is invalid.
     */
@SuppressWarnings("unchecked")
private Class validateClass(ClientConfig cfg) {
    Class clazz = null;
    try {
        clazz = Class.forName(cfg.getAccessRequestHandlerClassname());
    } catch (final ClassNotFoundException e) {
        LOG.error("Unable to load Handler Class '" + cfg.getAccessRequestHandlerClassname() + "' for RADIUS client '" + cfg.getName() + "'. Requests from this client will be ignored.", e);
        return null;
    }
    Object inst = null;
    try {
        inst = InjectorHolder.getInstance(clazz);
    } catch (ConfigurationException | ProvisionException e) {
        LOG.error("Unable to instantiate Handler Class '" + cfg.getAccessRequestHandlerClassname() + "' for RADIUS client '" + cfg.getName() + "'. Requests from this client will be ignored.", e);
        return null;
    }
    AccessRequestHandler handler = null;
    try {
        handler = (AccessRequestHandler) inst;
    } catch (final ClassCastException e) {
        LOG.error("Unable to use Handler Class '" + cfg.getAccessRequestHandlerClassname() + "' for RADIUS client '" + cfg.getName() + "'. Requests from this client will be ignored.", e);
        return null;
    }
    return clazz;
}
Also used : ProvisionException(com.google.inject.ProvisionException) ConfigurationException(com.google.inject.ConfigurationException) AccessRequestHandler(org.forgerock.openam.radius.server.spi.AccessRequestHandler)

Example 39 with ConfigurationException

use of com.google.inject.ConfigurationException in project dsl-devkit by dsldevkit.

the class RegistryBuilderParticipant method buildLanguageSpecificParticipants.

/**
 * For each {@link IResourceDescription.Delta} searches and calls the responsible {@link ILanguageSpecificBuilderParticipant}s.
 *
 * @param buildContext
 *          the {@link IBuildContext}, must not be {@code null}
 * @param monitor
 *          the {@link IProgressMonitor}, must not be {@code null}
 */
protected void buildLanguageSpecificParticipants(final IBuildContext buildContext, final IProgressMonitor monitor) {
    initParticipants();
    SubMonitor progress = SubMonitor.convert(monitor, buildContext.getDeltas().size() * MONITOR_PARTICIPANTS_PER_LANGUAGE);
    Map<String, BuildContext> languageIdToBuildContext = Maps.newHashMap();
    for (IResourceDescription.Delta delta : buildContext.getDeltas()) {
        IResourceServiceProvider resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(delta.getUri());
        if (resourceServiceProvider == null) {
            progress.worked(MONITOR_PARTICIPANTS_PER_LANGUAGE);
            continue;
        }
        IGrammarAccess grammarAccess = null;
        try {
            grammarAccess = resourceServiceProvider.get(IGrammarAccess.class);
        } catch (ConfigurationException e) {
            progress.worked(MONITOR_PARTICIPANTS_PER_LANGUAGE);
            continue;
        }
        if (grammarAccess == null) {
            progress.worked(MONITOR_PARTICIPANTS_PER_LANGUAGE);
            continue;
        }
        String languageId = grammarAccess.getGrammar().getName();
        BuildContext entryBuildContext = languageIdToBuildContext.get(languageId);
        if (entryBuildContext == null) {
            entryBuildContext = new BuildContext(buildContext.getBuiltProject(), buildContext.getResourceSet(), buildContext.getBuildType());
            languageIdToBuildContext.put(languageId, entryBuildContext);
        }
        entryBuildContext.addDelta(delta);
    }
    builLanguageSpecificContext(buildContext, progress, languageIdToBuildContext);
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IGrammarAccess(org.eclipse.xtext.IGrammarAccess) ConfigurationException(com.google.inject.ConfigurationException) SubMonitor(org.eclipse.core.runtime.SubMonitor)

Example 40 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)

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