public void testCustomTypeConversion() throws CreationException { final Date result = new Date(); Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { convertToTypes( Matchers.only(TypeLiteral.get(Date.class)), mockTypeConverter(result)); bindConstant().annotatedWith(NumericValue.class).to("Today"); bind(DateHolder.class); } }); assertSame(result, injector.getInstance(DateHolder.class).date); Binding<Date> binding = injector.getBinding(Key.get(Date.class, NumericValue.class)); assertTrue(binding instanceof ConvertedConstantBinding<?>); TypeConverterBinding converterBinding = ((ConvertedConstantBinding<?>) binding).getTypeConverterBinding(); assertEquals("CustomConverter", converterBinding.getTypeConverter().toString()); assertTrue(injector.getTypeConverterBindings().contains(converterBinding)); }
public void testCustomTypeConversion() throws CreationException { final Date result = new Date(); Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { convertToTypes(Matchers.only(TypeLiteral.get(Date.class)) , mockTypeConverter(result)); bindConstant().annotatedWith(NumericValue.class).to("Today"); bind(DateHolder.class); } }); assertSame(result, injector.getInstance(DateHolder.class).date); Binding<Date> binding = injector.getBinding(Key.get(Date.class, NumericValue.class)); assertTrue(binding instanceof ConvertedConstantBinding<?>); TypeConverterBinding converterBinding = ((ConvertedConstantBinding<?>)binding).getTypeConverterBinding(); assertEquals("CustomConverter", converterBinding.getTypeConverter().toString()); assertTrue(injector.getTypeConverterBindings().contains(converterBinding)); }
public void testCustomTypeConversion() throws CreationException { final Date result = new Date(); Injector injector = Guice.createInjector(new AbstractModule() { protected void configure() { convertToTypes(Matchers.only(TypeLiteral.get(Date.class)) , mockTypeConverter(result)); bindConstant().annotatedWith(NumericValue.class).to("Today"); bind(DateHolder.class); } }); assertSame(result, injector.getInstance(DateHolder.class).date); Binding<Date> binding = injector.getBinding(Key.get(Date.class, NumericValue.class)); assertTrue(binding instanceof ConvertedConstantBinding<?>); TypeConverterBinding converterBinding = ((ConvertedConstantBinding<?>)binding).getTypeConverterBinding(); assertEquals("CustomConverter", converterBinding.getTypeConverter().toString()); assertTrue(injector.getTypeConverterBindings().contains(converterBinding)); }
/** * Visitor for {@link ConvertedConstantBinding}. The {@link Binding}'s {@link Key} will be of an * annotated primitive type, and the value of {@link ConvertedConstantBinding#getSourceKey()} * will be of a {@link String} with the same annotation. */ @Override public Collection<Edge> visit(ConvertedConstantBinding<?> binding) { return ImmutableList.<Edge>of( new BindingEdge( NodeId.newTypeId(binding.getKey()), NodeId.newTypeId(binding.getSourceKey()), BindingEdge.Type.CONVERTED_CONSTANT)); }
public void testVisitConvertedConstant() { Binding<?> binding = getBinding(Key.get(Integer.class, Names.named("number")), new ConvertedConstantModule()); Collection<Key<?>> dependencies = visitor.visit((ConvertedConstantBinding<?>) binding); assertDependencies(dependencies, Key.get(String.class, Names.named("number"))); }
@Override public String toString() { return MoreObjects.toStringHelper(ConvertedConstantBinding.class) .add("key", getKey()) .add("sourceKey", getSourceKey()) .add("value", value) .toString(); }
public void testVisitConvertedConstant() { Binding<?> binding = getBinding(Key.get(Integer.class, Names.named("number")), new ConvertedConstantModule()); Collection<Key<?>> dependencies = visitor.visit( (ConvertedConstantBinding<?>) binding); assertDependencies(dependencies, Key.get(String.class, Names.named("number"))); }
@Override public String toString() { return Objects.toStringHelper(ConvertedConstantBinding.class) .add("key", getKey()) .add("sourceKey", getSourceKey()) .add("value", value) .toString(); }
@Override public Optional<TypeLiteral<?>> visit(ConvertedConstantBinding<?> binding) { // Return the provisioned object's type return of(TypeLiteral.get(binding.getValue().getClass())); }
@Override public Object visit(ConvertedConstantBinding<? extends T> convertedConstantBinding) { // TODO: What do I do here?? return super.visit(convertedConstantBinding); }
@Override public Void visit(ConvertedConstantBinding<? extends T> binding) { // Cannot be scoped binding.applyTo(binder); return null; }
@Override public Collection<Key<?>> visit(ConvertedConstantBinding<?> binding) { return visitHasDependencies(binding); }
@Override public Indexer.IndexedBinding visit(ConvertedConstantBinding<? extends Object> binding) { return new Indexer.IndexedBinding( binding, BindingType.CONSTANT, scope(binding), binding.getValue()); }
/** * Visitor for {@link ConvertedConstantBinding}. The {@link Binding}'s {@link Key} will be of an * annotated primitive type, and the value of {@link ConvertedConstantBinding#getSourceKey()} * will be of a {@link String} with the same annotation. */ @Override public Collection<Edge> visit(ConvertedConstantBinding<?> binding) { return ImmutableList.<Edge>of(new BindingEdge(NodeId.newTypeId(binding.getKey()), NodeId.newTypeId(binding.getSourceKey()), BindingEdge.Type.CONVERTED_CONSTANT)); }