Search in sources :

Example 91 with TypeLiteral

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

the class BinderTest method testNothingIsSerializableInBinderApi.

public void testNothingIsSerializableInBinderApi() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            public void configure() {
                try {
                    assertNotSerializable(binder());
                    assertNotSerializable(getProvider(Integer.class));
                    assertNotSerializable(getProvider(Key.get(new TypeLiteral<List<String>>() {
                    })));
                    assertNotSerializable(bind(Integer.class));
                    assertNotSerializable(bind(Integer.class).annotatedWith(Names.named("a")));
                    assertNotSerializable(bindConstant());
                    assertNotSerializable(bindConstant().annotatedWith(Names.named("b")));
                } catch (IOException e) {
                    fail(e.getMessage());
                }
            }
        });
        fail();
    } catch (CreationException ignored) {
    }
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) CreationException(com.google.inject.CreationException) IOException(java.io.IOException) AbstractModule(com.google.inject.AbstractModule)

Example 92 with TypeLiteral

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

the class InjectorImpl method createTypeLiteralBinding.

/**
   * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's
   * a bit awkward because we have to pull out the inner type in the type literal.
   */
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException {
    Type typeLiteralType = key.getTypeLiteral().getType();
    if (!(typeLiteralType instanceof ParameterizedType)) {
        throw errors.cannotInjectRawTypeLiteral().toException();
    }
    ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType;
    Type innerType = parameterizedType.getActualTypeArguments()[0];
    // this proves problematic, we can probably fix TypeLiteral to support type variables
    if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) {
        throw errors.cannotInjectTypeLiteralOf(innerType).toException();
    }
    // by definition, innerType == T, so this is safe
    @SuppressWarnings("unchecked") TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType);
    InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<TypeLiteral<T>>(Initializables.of(value));
    return new InstanceBindingImpl<TypeLiteral<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), value);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(com.google.inject.TypeLiteral) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 93 with TypeLiteral

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

the class InjectorImpl method createMembersInjectorBinding.

private <T> BindingImpl<MembersInjector<T>> createMembersInjectorBinding(Key<MembersInjector<T>> key, Errors errors) throws ErrorsException {
    Type membersInjectorType = key.getTypeLiteral().getType();
    if (!(membersInjectorType instanceof ParameterizedType)) {
        throw errors.cannotInjectRawMembersInjector().toException();
    }
    // safe because T came from Key<MembersInjector<T>>
    @SuppressWarnings("unchecked") TypeLiteral<T> instanceType = (TypeLiteral<T>) TypeLiteral.get(((ParameterizedType) membersInjectorType).getActualTypeArguments()[0]);
    MembersInjector<T> membersInjector = membersInjectorStore.get(instanceType, errors);
    InternalFactory<MembersInjector<T>> factory = new ConstantFactory<MembersInjector<T>>(Initializables.of(membersInjector));
    return new InstanceBindingImpl<MembersInjector<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), membersInjector);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(com.google.inject.TypeLiteral) MembersInjector(com.google.inject.MembersInjector)

Example 94 with TypeLiteral

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

the class Errors method formatSource.

public static void formatSource(Formatter formatter, Object source, ElementSource elementSource) {
    String modules = moduleSourceString(elementSource);
    if (source instanceof Dependency) {
        Dependency<?> dependency = (Dependency<?>) source;
        InjectionPoint injectionPoint = dependency.getInjectionPoint();
        if (injectionPoint != null) {
            formatInjectionPoint(formatter, dependency, injectionPoint, elementSource);
        } else {
            formatSource(formatter, dependency.getKey(), elementSource);
        }
    } else if (source instanceof InjectionPoint) {
        formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource);
    } else if (source instanceof Class) {
        formatter.format("  at %s%s%n", StackTraceElements.forType((Class<?>) source), modules);
    } else if (source instanceof Member) {
        formatter.format("  at %s%s%n", StackTraceElements.forMember((Member) source), modules);
    } else if (source instanceof TypeLiteral) {
        formatter.format("  while locating %s%s%n", source, modules);
    } else if (source instanceof Key) {
        Key<?> key = (Key<?>) source;
        formatter.format("  while locating %s%n", convert(key, elementSource));
    } else {
        formatter.format("  at %s%s%n", source, modules);
    }
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency) Member(java.lang.reflect.Member) Key(com.google.inject.Key)

Example 95 with TypeLiteral

use of com.google.inject.TypeLiteral in project graylog2-server by Graylog2.

the class GenericBindings method configure.

@Override
protected void configure() {
    // This is holding all our metrics.
    bind(MetricRegistry.class).toProvider(MetricRegistryProvider.class).asEagerSingleton();
    // must not be a singleton!
    bind(LocalMetricRegistry.class).in(Scopes.NO_SCOPE);
    install(new FactoryModuleBuilder().build(DecodingProcessor.Factory.class));
    bind(ProcessBuffer.class).asEagerSingleton();
    bind(InputBuffer.class).to(InputBufferImpl.class);
    bind(NodeId.class).toProvider(NodeIdProvider.class);
    bind(ServiceManager.class).toProvider(ServiceManagerProvider.class).asEagerSingleton();
    bind(HashedWheelTimer.class).toInstance(new HashedWheelTimer());
    bind(ThroughputCounter.class);
    bind(EventBus.class).toProvider(EventBusProvider.class).in(Scopes.SINGLETON);
    bind(Semaphore.class).annotatedWith(Names.named("JournalSignal")).toInstance(new Semaphore(0));
    install(new FactoryModuleBuilder().build(new TypeLiteral<IOState.Factory<MessageInput>>() {
    }));
    bind(InputRegistry.class).asEagerSingleton();
    bind(OkHttpClient.class).toProvider(OkHttpClientProvider.class).asEagerSingleton();
    bind(OkHttpClient.class).annotatedWith(Names.named("systemHttpClient")).toProvider(SystemOkHttpClientProvider.class).asEagerSingleton();
    bind(MimetypesFileTypeMap.class).toInstance(new MimetypesFileTypeMap());
    bind(ExecutorService.class).annotatedWith(Names.named("proxiedRequestsExecutorService")).toProvider(ProxiedRequestsExecutorService.class).asEagerSingleton();
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) OkHttpClient(okhttp3.OkHttpClient) FactoryModuleBuilder(com.google.inject.assistedinject.FactoryModuleBuilder) MessageInput(org.graylog2.plugin.inputs.MessageInput) HashedWheelTimer(org.jboss.netty.util.HashedWheelTimer) Semaphore(java.util.concurrent.Semaphore) InputRegistry(org.graylog2.shared.inputs.InputRegistry) MetricRegistryProvider(org.graylog2.shared.bindings.providers.MetricRegistryProvider) EventBusProvider(org.graylog2.shared.bindings.providers.EventBusProvider) TypeLiteral(com.google.inject.TypeLiteral) ServiceManagerProvider(org.graylog2.shared.bindings.providers.ServiceManagerProvider) ProxiedRequestsExecutorService(org.graylog2.shared.bindings.providers.ProxiedRequestsExecutorService) IOState(org.graylog2.plugin.IOState) NodeId(org.graylog2.plugin.system.NodeId) SystemOkHttpClientProvider(org.graylog2.shared.bindings.providers.SystemOkHttpClientProvider) OkHttpClientProvider(org.graylog2.shared.bindings.providers.OkHttpClientProvider) ProxiedRequestsExecutorService(org.graylog2.shared.bindings.providers.ProxiedRequestsExecutorService) ExecutorService(java.util.concurrent.ExecutorService) InputBuffer(org.graylog2.plugin.buffers.InputBuffer) SystemOkHttpClientProvider(org.graylog2.shared.bindings.providers.SystemOkHttpClientProvider) ProcessBuffer(org.graylog2.shared.buffers.ProcessBuffer) LocalMetricRegistry(org.graylog2.plugin.LocalMetricRegistry)

Aggregations

TypeLiteral (com.google.inject.TypeLiteral)118 AbstractModule (com.google.inject.AbstractModule)43 Injector (com.google.inject.Injector)38 Module (com.google.inject.Module)15 Map (java.util.Map)14 Key (com.google.inject.Key)12 Provider (com.google.inject.Provider)12 ParameterizedType (java.lang.reflect.ParameterizedType)12 ImmutableSet (com.google.common.collect.ImmutableSet)10 Binding (com.google.inject.Binding)10 Annotation (java.lang.annotation.Annotation)10 HashMap (java.util.HashMap)10 Set (java.util.Set)10 Binder (com.google.inject.Binder)9 InjectionPoint (com.google.inject.spi.InjectionPoint)9 Method (java.lang.reflect.Method)9 Type (java.lang.reflect.Type)9 HashSet (java.util.HashSet)9 ProvisionException (com.google.inject.ProvisionException)7 FactoryModuleBuilder (com.google.inject.assistedinject.FactoryModuleBuilder)7