Search in sources :

Example 1 with TypeConverter

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

the class TypeConverterBindingProcessor method prepareBuiltInConverters.

/**
 * Installs default converters for primitives, enums, and class literals.
 */
static void prepareBuiltInConverters(InjectorImpl injector) {
    // Configure type converters.
    convertToPrimitiveType(injector, int.class, Integer.class);
    convertToPrimitiveType(injector, long.class, Long.class);
    convertToPrimitiveType(injector, boolean.class, Boolean.class);
    convertToPrimitiveType(injector, byte.class, Byte.class);
    convertToPrimitiveType(injector, short.class, Short.class);
    convertToPrimitiveType(injector, float.class, Float.class);
    convertToPrimitiveType(injector, double.class, Double.class);
    convertToClass(injector, Character.class, new TypeConverter() {

        @Override
        public Object convert(String value, TypeLiteral<?> toType) {
            value = value.trim();
            if (value.length() != 1) {
                throw new RuntimeException("Length != 1.");
            }
            return value.charAt(0);
        }

        @Override
        public String toString() {
            return "TypeConverter<Character>";
        }
    });
    convertToClasses(injector, Matchers.subclassesOf(Enum.class), new TypeConverter() {

        // Unavoidable, only way to use Enum.valueOf
        @SuppressWarnings("rawtypes")
        @Override
        public Object convert(String value, TypeLiteral<?> toType) {
            return Enum.valueOf((Class) toType.getRawType(), value);
        }

        @Override
        public String toString() {
            return "TypeConverter<E extends Enum<E>>";
        }
    });
    internalConvertToTypes(injector, new AbstractMatcher<TypeLiteral<?>>() {

        @Override
        public boolean matches(TypeLiteral<?> typeLiteral) {
            return typeLiteral.getRawType() == Class.class;
        }

        @Override
        public String toString() {
            return "Class<?>";
        }
    }, new TypeConverter() {

        @Override
        public Object convert(String value, TypeLiteral<?> toType) {
            try {
                return Class.forName(value);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        @Override
        public String toString() {
            return "TypeConverter<Class<?>>";
        }
    });
}
Also used : TypeConverter(com.google.inject.spi.TypeConverter) TypeLiteral(com.google.inject.TypeLiteral)

Example 2 with TypeConverter

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

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() {

            @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);
    }
}
Also used : TypeConverter(com.google.inject.spi.TypeConverter) TypeLiteral(com.google.inject.TypeLiteral) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with TypeConverter

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

the class TypeConverterBindingProcessor method prepareBuiltInConverters.

/**
 * Installs default converters for primitives, enums, and class literals.
 */
static void prepareBuiltInConverters(InjectorImpl injector) {
    // Configure type converters.
    convertToPrimitiveType(injector, int.class, Integer.class);
    convertToPrimitiveType(injector, long.class, Long.class);
    convertToPrimitiveType(injector, boolean.class, Boolean.class);
    convertToPrimitiveType(injector, byte.class, Byte.class);
    convertToPrimitiveType(injector, short.class, Short.class);
    convertToPrimitiveType(injector, float.class, Float.class);
    convertToPrimitiveType(injector, double.class, Double.class);
    convertToClass(injector, Character.class, new TypeConverter() {

        public Object convert(String value, TypeLiteral<?> toType) {
            value = value.trim();
            if (value.length() != 1) {
                throw new RuntimeException("Length != 1.");
            }
            return value.charAt(0);
        }

        @Override
        public String toString() {
            return "TypeConverter<Character>";
        }
    });
    convertToClasses(injector, Matchers.subclassesOf(Enum.class), new TypeConverter() {

        @SuppressWarnings("unchecked")
        public Object convert(String value, TypeLiteral<?> toType) {
            return Enum.valueOf((Class) toType.getRawType(), value);
        }

        @Override
        public String toString() {
            return "TypeConverter<E extends Enum<E>>";
        }
    });
    internalConvertToTypes(injector, new AbstractMatcher<TypeLiteral<?>>() {

        public boolean matches(TypeLiteral<?> typeLiteral) {
            return typeLiteral.getRawType() == Class.class;
        }

        @Override
        public String toString() {
            return "Class<?>";
        }
    }, new TypeConverter() {

        @SuppressWarnings("unchecked")
        public Object convert(String value, TypeLiteral<?> toType) {
            try {
                return Class.forName(value);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        @Override
        public String toString() {
            return "TypeConverter<Class<?>>";
        }
    });
}
Also used : TypeConverter(com.google.inject.spi.TypeConverter) TypeLiteral(com.google.inject.TypeLiteral)

Example 4 with TypeConverter

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

the class TypeConversionTest method testStringIsConvertedOnlyOnce.

public void testStringIsConvertedOnlyOnce() {
    final TypeConverter converter = new TypeConverter() {

        boolean converted = false;

        @Override
        public Object convert(String value, TypeLiteral<?> toType) {
            if (converted) {
                throw new AssertionFailedError("converted multiple times!");
            }
            converted = true;
            return new Date();
        }
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            convertToTypes(Matchers.only(TypeLiteral.get(Date.class)), converter);
            bindConstant().annotatedWith(NumericValue.class).to("unused");
        }
    });
    Date first = injector.getInstance(Key.get(Date.class, NumericValue.class));
    Date second = injector.getInstance(Key.get(Date.class, NumericValue.class));
    assertSame(first, second);
}
Also used : TypeConverter(com.google.inject.spi.TypeConverter) AssertionFailedError(junit.framework.AssertionFailedError) Date(java.util.Date)

Example 5 with TypeConverter

use of com.google.inject.spi.TypeConverter 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
            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);
    }
}
Also used : TypeConverter(com.google.inject.spi.TypeConverter) TypeLiteral(com.google.inject.TypeLiteral) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

TypeConverter (com.google.inject.spi.TypeConverter)6 TypeLiteral (com.google.inject.TypeLiteral)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Date (java.util.Date)2 AssertionFailedError (junit.framework.AssertionFailedError)2