Search in sources :

Example 1 with TypeConverterBinding

use of com.google.inject.spi.TypeConverterBinding in project guice by google.

the class InjectorImpl method convertConstantStringBinding.

/**
   * Converts a constant string binding to the required type.
   *
   * @return the binding if it could be resolved, or null if the binding doesn't exist
   * @throws com.google.inject.internal.ErrorsException if there was an error resolving the binding
   */
private <T> BindingImpl<T> convertConstantStringBinding(Key<T> key, Errors errors) throws ErrorsException {
    // Find a constant string binding.
    Key<String> stringKey = key.ofType(STRING_TYPE);
    BindingImpl<String> stringBinding = state.getExplicitBinding(stringKey);
    if (stringBinding == null || !stringBinding.isConstant()) {
        return null;
    }
    // We can't call getProvider().get() because this InstanceBinding may not have been inintialized
    // yet (because we may have been called during InternalInjectorCreator.initializeStatically and
    // instance binding validation hasn't happened yet.)
    @SuppressWarnings("unchecked") String stringValue = ((InstanceBinding<String>) stringBinding).getInstance();
    Object source = stringBinding.getSource();
    // Find a matching type converter.
    TypeLiteral<T> type = key.getTypeLiteral();
    TypeConverterBinding typeConverterBinding = state.getConverter(stringValue, type, errors, source);
    if (typeConverterBinding == null) {
        // No converter can handle the given type.
        return null;
    }
    // Try to convert the string. A failed conversion results in an error.
    try {
        // This cast is safe because we double check below.
        @SuppressWarnings("unchecked") T converted = (T) typeConverterBinding.getTypeConverter().convert(stringValue, type);
        if (converted == null) {
            throw errors.converterReturnedNull(stringValue, source, type, typeConverterBinding).toException();
        }
        if (!type.getRawType().isInstance(converted)) {
            throw errors.conversionTypeError(stringValue, source, type, typeConverterBinding, converted).toException();
        }
        return new ConvertedConstantBindingImpl<T>(this, key, converted, stringBinding, typeConverterBinding);
    } catch (ErrorsException e) {
        throw e;
    } catch (RuntimeException e) {
        throw errors.conversionError(stringValue, source, type, typeConverterBinding, e).toException();
    }
}
Also used : InstanceBinding(com.google.inject.spi.InstanceBinding) TypeConverterBinding(com.google.inject.spi.TypeConverterBinding)

Example 2 with TypeConverterBinding

use of com.google.inject.spi.TypeConverterBinding in project guice by google.

the class TypeConversionTest method testCustomTypeConversion.

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));
}
Also used : ConvertedConstantBinding(com.google.inject.spi.ConvertedConstantBinding) TypeConverterBinding(com.google.inject.spi.TypeConverterBinding) Date(java.util.Date)

Example 3 with TypeConverterBinding

use of com.google.inject.spi.TypeConverterBinding in project roboguice by roboguice.

the class InjectorImpl method convertConstantStringBinding.

/**
   * Converts a constant string binding to the required type.
   *
   * @return the binding if it could be resolved, or null if the binding doesn't exist
   * @throws com.google.inject.internal.ErrorsException if there was an error resolving the binding
   */
private <T> BindingImpl<T> convertConstantStringBinding(Key<T> key, Errors errors) throws ErrorsException {
    // Find a constant string binding.
    Key<String> stringKey = key.ofType(STRING_TYPE);
    BindingImpl<String> stringBinding = state.getExplicitBinding(stringKey);
    if (stringBinding == null || !stringBinding.isConstant()) {
        return null;
    }
    String stringValue = stringBinding.getProvider().get();
    Object source = stringBinding.getSource();
    // Find a matching type converter.
    TypeLiteral<T> type = key.getTypeLiteral();
    TypeConverterBinding typeConverterBinding = state.getConverter(stringValue, type, errors, source);
    if (typeConverterBinding == null) {
        // No converter can handle the given type.
        return null;
    }
    // Try to convert the string. A failed conversion results in an error.
    try {
        // This cast is safe because we double check below.
        @SuppressWarnings("unchecked") T converted = (T) typeConverterBinding.getTypeConverter().convert(stringValue, type);
        if (converted == null) {
            throw errors.converterReturnedNull(stringValue, source, type, typeConverterBinding).toException();
        }
        if (!type.getRawType().isInstance(converted)) {
            throw errors.conversionTypeError(stringValue, source, type, typeConverterBinding, converted).toException();
        }
        return new ConvertedConstantBindingImpl<T>(this, key, converted, stringBinding, typeConverterBinding);
    } catch (ErrorsException e) {
        throw e;
    } catch (RuntimeException e) {
        throw errors.conversionError(stringValue, source, type, typeConverterBinding, e).toException();
    }
}
Also used : TypeConverterBinding(com.google.inject.spi.TypeConverterBinding)

Example 4 with TypeConverterBinding

use of com.google.inject.spi.TypeConverterBinding in project roboguice by roboguice.

the class TypeConversionTest method testCustomTypeConversion.

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));
}
Also used : ConvertedConstantBinding(com.google.inject.spi.ConvertedConstantBinding) TypeConverterBinding(com.google.inject.spi.TypeConverterBinding) Date(java.util.Date)

Aggregations

TypeConverterBinding (com.google.inject.spi.TypeConverterBinding)4 ConvertedConstantBinding (com.google.inject.spi.ConvertedConstantBinding)2 Date (java.util.Date)2 InstanceBinding (com.google.inject.spi.InstanceBinding)1