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) {
}
}
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);
}
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);
}
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);
}
}
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();
}
Aggregations