Hi all guys,
I've been developing a set of TypeConverters to convert values read
from properties files. The questions I'd ask are:

1) Is i possible to override the default TypeConverter? I yes, how? I
need, for example, override the converter for boolean values, since in
my con files the 'true' value can be expressed as "true", "yes", "y",
"on", "1"; even if I can register my custom converter, when trying to
convert to boolean, guice rises the following error:

    Multiple converters can convert '0'

2) Since I need to manage also arrays of types, where values are CSV,
I wrote this abstract converter able to converts arrays; it works for
java.lang.Boolean[] but not for boolean[], rising the following error:

              Type mismatch converting 'true, no' to boolean[] using
booleanconver...@481958 which matches only(boolean[])[...] Converter
returned [...@659078.  while locating boolean[]

follows class implementation; what's wrong?

Thanks in advance, best regards,
Simo

abstract class AbstractConverter implements TypeConverter {

    private static final String DEFAULT_DELIMITER = ",";

    public final Object convert(String value, TypeLiteral<?> toType) {
        if (GenericArrayType.class.isInstance(toType.getType())) {
            StringTokenizer tokenizer = new StringTokenizer(value,
DEFAULT_DELIMITER);
            Class<?> arrayType =
MoreTypes.getRawType(((GenericArrayType)
toType.getType()).getGenericComponentType());
            Object array = Array.newInstance(arrayType,
tokenizer.countTokens());

            int i = 0;
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken().trim();
                Array.set(array, i++, this.simpleConvert(token, arrayType));
            }

            return array;
        }

        return this.simpleConvert(value,
MoreTypes.getRawType(toType.getType()));
    }

    protected abstract Object simpleConvert(String value, Class<?> toType);

}

-- 
http://people.apache.org/~simonetripodi/
-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.


Reply via email to