@Override public TypeConverterBinding getConverter( String stringValue, TypeLiteral<?> type, Errors errors, Object source) { TypeConverterBinding matchingConverter = null; for (State s = this; s != State.NONE; s = s.parent()) { for (TypeConverterBinding converter : s.getConvertersThisLevel()) { if (converter.getTypeMatcher().matches(type)) { if (matchingConverter != null) { errors.ambiguousTypeConversion(stringValue, source, type, matchingConverter, converter); } matchingConverter = converter; } } } return matchingConverter; }
ConvertedConstantBindingImpl( InjectorImpl injector, Key<T> key, T value, Binding<String> originalBinding, TypeConverterBinding typeConverterBinding) { super( injector, key, originalBinding.getSource(), new ConstantFactory<T>(Initializables.of(value)), Scoping.UNSCOPED); this.value = value; provider = Providers.of(value); this.originalBinding = originalBinding; this.typeConverterBinding = typeConverterBinding; }
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)); }
@Override public Boolean visit(TypeConverterBinding command) { injector.state.addConverter( new TypeConverterBinding( command.getSource(), command.getTypeMatcher(), command.getTypeConverter())); return true; }
public Errors converterReturnedNull( String stringValue, Object source, TypeLiteral<?> type, TypeConverterBinding typeConverterBinding) { return addMessage( "Received null converting '%s' (bound at %s) to %s%n using %s.", stringValue, convert(source), type, typeConverterBinding); }
public Errors conversionTypeError( String stringValue, Object source, TypeLiteral<?> type, TypeConverterBinding typeConverterBinding, Object converted) { return addMessage( "Type mismatch converting '%s' (bound at %s) to %s%n" + " using %s.%n" + " Converter returned %s.", stringValue, convert(source), type, typeConverterBinding, converted); }
public Errors conversionError( String stringValue, Object source, TypeLiteral<?> type, TypeConverterBinding typeConverterBinding, RuntimeException cause) { return errorInUserCode( cause, "Error converting '%s' (bound at %s) to %s%n using %s.%n Reason: %s", stringValue, convert(source), type, typeConverterBinding, cause); }
public Errors ambiguousTypeConversion( String stringValue, Object source, TypeLiteral<?> type, TypeConverterBinding a, TypeConverterBinding b) { return addMessage( "Multiple converters can convert '%s' (bound at %s) to %s:%n" + " %s and%n" + " %s.%n" + " Please adjust your type converter configuration to avoid overlapping matches.", stringValue, convert(source), type, a, b); }
public TypeConverterBinding getConverter( String stringValue, TypeLiteral<?> type, Errors errors, Object source) { TypeConverterBinding matchingConverter = null; for (State s = this; s != State.NONE; s = s.parent()) { for (TypeConverterBinding converter : s.getConvertersThisLevel()) { if (converter.getTypeMatcher().matches(type)) { if (matchingConverter != null) { errors.ambiguousTypeConversion(stringValue, source, type, matchingConverter, converter); } matchingConverter = converter; } } } return matchingConverter; }
ConvertedConstantBindingImpl( InjectorImpl injector, Key<T> key, T value, Binding<String> originalBinding, TypeConverterBinding typeConverterBinding) { super(injector, key, originalBinding.getSource(), new ConstantFactory<T>(Initializables.of(value)), Scoping.UNSCOPED); this.value = value; provider = Providers.of(value); this.originalBinding = originalBinding; this.typeConverterBinding = typeConverterBinding; }
public Errors conversionTypeError(String stringValue, Object source, TypeLiteral<?> type, TypeConverterBinding typeConverterBinding, Object converted) { return addMessage("Type mismatch converting '%s' (bound at %s) to %s%n" + " using %s.%n" + " Converter returned %s.", stringValue, convert(source), type, typeConverterBinding, converted); }
public Errors conversionError(String stringValue, Object source, TypeLiteral<?> type, TypeConverterBinding typeConverterBinding, RuntimeException cause) { return errorInUserCode(cause, "Error converting '%s' (bound at %s) to %s%n" + " using %s.%n" + " Reason: %s", stringValue, convert(source), type, typeConverterBinding, cause); }
public Errors ambiguousTypeConversion(String stringValue, Object source, TypeLiteral<?> type, TypeConverterBinding a, TypeConverterBinding b) { return addMessage("Multiple converters can convert '%s' (bound at %s) to %s:%n" + " %s and%n" + " %s.%n" + " Please adjust your type converter configuration to avoid overlapping matches.", stringValue, convert(source), type, a, b); }
@Override public Set<TypeConverterBinding> getTypeConverterBindings() { return injector.getTypeConverterBindings(); }
@Override public V visit(TypeConverterBinding binding) { return message(binding, "Binding type converter " + binding.getTypeConverter() + " to classes matching " + binding.getTypeMatcher()); }
@Override public Set<TypeConverterBinding> getTypeConverterBindings() { return null; }
public final Set<TypeConverterBinding> getTypeConverterBindings() { return _injector.getTypeConverterBindings(); }
@Override public Set<TypeConverterBinding> getTypeConverterBindings() { return injector().getTypeConverterBindings(); }
@Override public Set<TypeConverterBinding> getTypeConverterBindings() { return delegateInjector.getTypeConverterBindings(); }
private static void internalConvertToTypes( InjectorImpl injector, Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { injector.state.addConverter( new TypeConverterBinding(SourceProvider.UNKNOWN_SOURCE, typeMatcher, converter)); }
@Override public Iterable<TypeConverterBinding> getConvertersThisLevel() { return converters; }
@Override public void addConverter(TypeConverterBinding typeConverterBinding) { converters.add(typeConverterBinding); }
@Override public void addConverter(TypeConverterBinding typeConverterBinding) { throw new UnsupportedOperationException(); }
@Override public TypeConverterBinding getConverter( String stringValue, TypeLiteral<?> type, Errors errors, Object source) { throw new UnsupportedOperationException(); }
@Override public Iterable<TypeConverterBinding> getConvertersThisLevel() { return ImmutableSet.of(); }
/** Returns the matching converter for {@code type}, or null if none match. */ TypeConverterBinding getConverter( String stringValue, TypeLiteral<?> type, Errors errors, Object source);
/** Returns all converters at this level only. */ Iterable<TypeConverterBinding> getConvertersThisLevel();
@Override public TypeConverterBinding getTypeConverterBinding() { return typeConverterBinding; }
@Override public Set<TypeConverterBinding> getTypeConverterBindings() { return ImmutableSet.copyOf(state.getConvertersThisLevel()); }