Hi, Simo. 1) Afaik No. The primitive type converters are designed to convert from java primitive type literals to values. I think this is the most obvious default. I'd go the same way.
2) 'true, no' should have 'true, false' or something. I'd check if Boolean converter can convert 'no'. Why not just stick to java type literals? The simplest thing I can think of is to write a converter that changes the properties to java type literals. E.g. you read '1' but before passing to Guice you change it to 'true'. Something along those lines. Cheers Alen On Jan 14, 9:20 pm, Simone Tripodi <[email protected]> wrote: > 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.
