use of com.google.inject.TypeLiteral in project guice by google.
the class FactoryProvider2 method constructorHasMatchingParams.
/**
* Matching logic for constructors annotated with AssistedInject. This returns true if and only if
* all @Assisted parameters in the constructor exactly match (in any order) all @Assisted
* parameters the method's parameter.
*/
private boolean constructorHasMatchingParams(TypeLiteral<?> type, Constructor<?> constructor, List<Key<?>> paramList, Errors errors) throws ErrorsException {
List<TypeLiteral<?>> params = type.getParameterTypes(constructor);
Annotation[][] paramAnnotations = constructor.getParameterAnnotations();
int p = 0;
List<Key<?>> constructorKeys = Lists.newArrayList();
for (TypeLiteral<?> param : params) {
Key<?> paramKey = Annotations.getKey(param, constructor, paramAnnotations[p++], errors);
constructorKeys.add(paramKey);
}
// Require that every key exist in the constructor to match up exactly.
for (Key<?> key : paramList) {
// If it didn't exist in the constructor set, we can't use it.
if (!constructorKeys.remove(key)) {
return false;
}
}
// If any keys remain and their annotation is Assisted, we can't use it.
for (Key<?> key : constructorKeys) {
if (key.getAnnotationType() == Assisted.class) {
return false;
}
}
// All @Assisted params match up to the method's parameters.
return true;
}
use of com.google.inject.TypeLiteral in project guice by google.
the class FactoryModuleBuilderTest method testParameterizedClassesWithNoImplements.
public void testParameterizedClassesWithNoImplements() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(new TypeLiteral<Foo.Factory<String>>() {
}));
}
});
Foo.Factory<String> factory = injector.getInstance(Key.get(new TypeLiteral<Foo.Factory<String>>() {
}));
@SuppressWarnings("unused") Foo<String> foo = factory.create(new Bar());
}
use of com.google.inject.TypeLiteral in project guice by google.
the class FactoryProvider2Test 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 FactoryProviderTest method testAssistedFactoryForParameterizedType.
public void testAssistedFactoryForParameterizedType() {
final TypeLiteral<InsuranceFactory<Mustang>> mustangInsuranceFactoryType = new TypeLiteral<InsuranceFactory<Mustang>>() {
};
final TypeLiteral<InsuranceFactory<Camaro>> camaroInsuranceFactoryType = new TypeLiteral<InsuranceFactory<Camaro>>() {
};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Double.class).annotatedWith(Names.named("lowLimit")).toInstance(50000.0d);
bind(Double.class).annotatedWith(Names.named("highLimit")).toInstance(100000.0d);
bind(mustangInsuranceFactoryType).toProvider(FactoryProvider.newFactory(mustangInsuranceFactoryType, TypeLiteral.get(MustangInsurance.class)));
bind(camaroInsuranceFactoryType).toProvider(FactoryProvider.newFactory(camaroInsuranceFactoryType, TypeLiteral.get(CamaroInsurance.class)));
}
});
InsuranceFactory<Mustang> mustangInsuranceFactory = injector.getInstance(Key.get(mustangInsuranceFactoryType));
InsuranceFactory<Camaro> camaroInsuranceFactory = injector.getInstance(Key.get(camaroInsuranceFactoryType));
Mustang mustang = new Mustang(5000d, Color.BLACK);
MustangInsurance mustangPolicy = (MustangInsurance) mustangInsuranceFactory.create(mustang, 800.0d);
assertEquals(800.0d, mustangPolicy.premium, 0.0);
assertEquals(50000.0d, mustangPolicy.limit, 0.0);
Camaro camaro = new Camaro(3000, 1967, Color.BLUE);
CamaroInsurance camaroPolicy = (CamaroInsurance) camaroInsuranceFactory.create(camaro, 800.0d);
assertEquals(800.0d, camaroPolicy.premium, 0.0);
assertEquals(100000.0d, camaroPolicy.limit, 0.0);
}
use of com.google.inject.TypeLiteral in project guice by google.
the class FactoryProviderTest method testAssistedFactoryForTypeVariableParameters.
public void testAssistedFactoryForTypeVariableParameters() {
final TypeLiteral<InsuranceFactory<Camaro>> camaroInsuranceFactoryType = new TypeLiteral<InsuranceFactory<Camaro>>() {
};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Double.class).toInstance(50000.0d);
bind(camaroInsuranceFactoryType).toProvider(FactoryProvider.newFactory(camaroInsuranceFactoryType, new TypeLiteral<AutoInsurance<Camaro>>() {
}));
}
});
InsuranceFactory<Camaro> camaroInsuranceFactory = injector.getInstance(Key.get(camaroInsuranceFactoryType));
Camaro camaro = new Camaro(3000, 1967, Color.BLUE);
AutoInsurance<?> camaroPolicy = (AutoInsurance<?>) camaroInsuranceFactory.create(camaro, 800.0d);
assertEquals(800.0d, camaroPolicy.premium, 0.0);
assertEquals(50000.0d, camaroPolicy.limit, 0.0);
assertEquals(camaro, camaroPolicy.car);
}
Aggregations