use of com.google.inject.TypeLiteral in project guice by google.
the class FactoryProviderTest method testGenericAssistedFactory.
public void testGenericAssistedFactory() {
final TypeLiteral<GenericColoredCarFactory<Mustang>> mustangTypeLiteral = new TypeLiteral<GenericColoredCarFactory<Mustang>>() {
};
final TypeLiteral<GenericColoredCarFactory<Camaro>> camaroTypeLiteral = new TypeLiteral<GenericColoredCarFactory<Camaro>>() {
};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Double.class).toInstance(5.0d);
bind(int.class).annotatedWith(Names.named("horsePower")).toInstance(250);
bind(int.class).annotatedWith(Names.named("modelYear")).toInstance(1984);
bind(mustangTypeLiteral).toProvider(FactoryProvider.newFactory(mustangTypeLiteral, TypeLiteral.get(Mustang.class)));
bind(camaroTypeLiteral).toProvider(FactoryProvider.newFactory(camaroTypeLiteral, TypeLiteral.get(Camaro.class)));
}
});
GenericColoredCarFactory<Mustang> mustangFactory = injector.getInstance(Key.get(mustangTypeLiteral));
GenericColoredCarFactory<Camaro> camaroFactory = injector.getInstance(Key.get(camaroTypeLiteral));
Mustang blueMustang = mustangFactory.create(Color.BLUE);
assertEquals(Color.BLUE, blueMustang.color);
assertEquals(5.0d, blueMustang.engineSize, 0.0);
Camaro redCamaro = camaroFactory.create(Color.RED);
assertEquals(Color.RED, redCamaro.color);
assertEquals(1984, redCamaro.modelYear);
assertEquals(250, redCamaro.horsePower);
}
use of com.google.inject.TypeLiteral in project guice by google.
the class Java8LanguageFeatureBindingTest method testBinding_lambdaToInterface.
// Some of these tests are kind of weird.
// See https://github.com/google/guice/issues/757 for more on why they exist.
public void testBinding_lambdaToInterface() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<Predicate<Object>>() {
}).toInstance(o -> o != null);
}
});
Predicate<Object> predicate = injector.getInstance(new Key<Predicate<Object>>() {
});
assertTrue(predicate.test(new Object()));
assertFalse(predicate.test(null));
}
use of com.google.inject.TypeLiteral in project guice by google.
the class RealMultibinder method collectionOfJavaxProvidersOf.
@SuppressWarnings("unchecked")
static <T> TypeLiteral<Collection<javax.inject.Provider<T>>> collectionOfJavaxProvidersOf(TypeLiteral<T> elementType) {
Type providerType = Types.newParameterizedType(javax.inject.Provider.class, elementType.getType());
Type type = Types.collectionOf(providerType);
return (TypeLiteral<Collection<javax.inject.Provider<T>>>) TypeLiteral.get(type);
}
use of com.google.inject.TypeLiteral in project guice by google.
the class TypeConverterBindingProcessor method convertToPrimitiveType.
private static <T> void convertToPrimitiveType(InjectorImpl injector, Class<T> primitiveType, final Class<T> wrapperType) {
try {
final Method parser = wrapperType.getMethod("parse" + capitalize(primitiveType.getName()), String.class);
TypeConverter typeConverter = new TypeConverter() {
@Override
@SuppressWarnings("unchecked")
public Object convert(String value, TypeLiteral<?> toType) {
try {
return parser.invoke(null, value);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getTargetException().getMessage());
}
}
@Override
public String toString() {
return "TypeConverter<" + wrapperType.getSimpleName() + ">";
}
};
convertToClass(injector, wrapperType, typeConverter);
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
use of com.google.inject.TypeLiteral in project guice by google.
the class ProviderMethodsModule method createProviderMethod.
private <T> ProviderMethod<T> createProviderMethod(Binder binder, Method method, Annotation annotation) {
binder = binder.withSource(method);
Errors errors = new Errors(method);
// prepare the parameter providers
InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral);
// Define T as the method's return type.
@SuppressWarnings("unchecked") TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
try {
key = scanner.prepareMethod(binder, annotation, key, point);
} catch (Throwable t) {
binder.addError(t);
}
Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations());
for (Message message : errors.getMessages()) {
binder.addError(message);
}
return ProviderMethod.create(key, method, delegate, ImmutableSet.copyOf(point.getDependencies()), scopeAnnotation, skipFastClassGeneration, annotation);
}
Aggregations