public String convert(Object value) { if(value != null) { Class class1 = value.getClass(); if(class1.isEnum() && super.lookup(class1) == null) super.register(new EnumConverter(class1), class1); else if(class1.isArray() && class1.getComponentType().isEnum()) { if(super.lookup(class1) == null) { ArrayConverter arrayconverter = new ArrayConverter(class1, new EnumConverter(class1.getComponentType()), 0); arrayconverter.setOnlyFirstToString(false); super.register(arrayconverter, class1); } Converter converter = super.lookup(class1); return (String)converter.convert(String.class, value); } } return super.convert(value); }
@Override public String convert(Object value) { if (value != null) { Class<?> type = value.getClass(); if (type.isEnum() && super.lookup(type) == null) { super.register(new EnumConverter(type), type); } else if (type.isArray() && type.getComponentType().isEnum()) { if (super.lookup(type) == null) { ArrayConverter arrayConverter = new ArrayConverter(type, new EnumConverter(type.getComponentType()), 0); arrayConverter.setOnlyFirstToString(false); super.register(arrayConverter, type); } Converter converter = super.lookup(type); return ((String) converter.convert(String.class, value)); } } return super.convert(value); }
@Override public int[][][] convert(String str) { if (str == null || str.isEmpty()) { return new int[0][][]; } // 格式 1,2;3,4|5,6 => [[[1, 2], [3, 4]], [[5,6]]] IntegerConverter integerConverter = new IntegerConverter(); // 基本类型 ArrayConverter arrayConverter1 = new ArrayConverter(int[].class, integerConverter); // 一维数组 默认使用逗号分割 ArrayConverter arrayConverter2 = new ArrayConverter(int[][].class, arrayConverter1); // 二维数组 arrayConverter2.setDelimiter(';'); // 使用分号分割 arrayConverter2.setAllowedChars(new char[]{','}); ArrayConverter arrayConverter3 = new ArrayConverter(int[][].class, arrayConverter2); // 二维数组 arrayConverter3.setDelimiter('|'); // 使用竖线分割 arrayConverter3.setAllowedChars(new char[]{';', ','}); return arrayConverter3.convert(int[][][].class, str); }
@Override public int[][][] convert(String str) { if (str == null || str.isEmpty()) { return new int[0][][]; } // 格式 1,2;3,4|5,6 => [[[1, 2], [3, 4]], [[5,6]]] // 基本类型 IntegerConverter integerConverter = new IntegerConverter(); // 一维数组 默认使用逗号分割 ArrayConverter arrayConverter1 = new ArrayConverter(int[].class, integerConverter); // 二维数组 ArrayConverter arrayConverter2 = new ArrayConverter(int[][].class, arrayConverter1); // 使用分号分割 arrayConverter2.setDelimiter(';'); arrayConverter2.setAllowedChars(new char[]{','}); // 二维数组 ArrayConverter arrayConverter3 = new ArrayConverter(int[][].class, arrayConverter2); // 使用竖线分割 arrayConverter3.setDelimiter('|'); arrayConverter3.setAllowedChars(new char[]{';', ','}); return arrayConverter3.convert(int[][][].class, str); }
@SuppressWarnings("rawtypes") @Override public Object convert(Object value, Class targetType) { if (super.lookup(targetType) == null) { if (targetType.isEnum()) { super.register(new EnumConverter(targetType), targetType); } else if (targetType.isArray() && targetType.getComponentType().isEnum()) { ArrayConverter arrayConverter = new ArrayConverter(targetType, new EnumConverter(targetType.getComponentType()), 0); arrayConverter.setOnlyFirstToString(false); super.register(arrayConverter, targetType); } } return super.convert(value, targetType); }
/** * Register basic types of JDK like boolean, int, and String to use with BeanUtils. All these * types are found in the {@code java.lang} package. * @param cub * Instance of {@link ConvertUtilsBean} to register types with. */ private static void registerIntegralTypes(ConvertUtilsBean cub) { cub.register(new BooleanConverter(), Boolean.TYPE); cub.register(new BooleanConverter(), Boolean.class); cub.register(new ArrayConverter( boolean[].class, new BooleanConverter()), boolean[].class); cub.register(new ByteConverter(), Byte.TYPE); cub.register(new ByteConverter(), Byte.class); cub.register(new ArrayConverter(byte[].class, new ByteConverter()), byte[].class); cub.register(new CharacterConverter(), Character.TYPE); cub.register(new CharacterConverter(), Character.class); cub.register(new ArrayConverter(char[].class, new CharacterConverter()), char[].class); cub.register(new DoubleConverter(), Double.TYPE); cub.register(new DoubleConverter(), Double.class); cub.register(new ArrayConverter(double[].class, new DoubleConverter()), double[].class); cub.register(new FloatConverter(), Float.TYPE); cub.register(new FloatConverter(), Float.class); cub.register(new ArrayConverter(float[].class, new FloatConverter()), float[].class); cub.register(new IntegerConverter(), Integer.TYPE); cub.register(new IntegerConverter(), Integer.class); cub.register(new ArrayConverter(int[].class, new IntegerConverter()), int[].class); cub.register(new LongConverter(), Long.TYPE); cub.register(new LongConverter(), Long.class); cub.register(new ArrayConverter(long[].class, new LongConverter()), long[].class); cub.register(new ShortConverter(), Short.TYPE); cub.register(new ShortConverter(), Short.class); cub.register(new ArrayConverter(short[].class, new ShortConverter()), short[].class); cub.register(new RelaxedStringArrayConverter(), String[].class); // BigDecimal, BigInteger, Class, Date, String, Time, TimeStamp // do not use defaults in the default configuration of ConvertUtilsBean }
/** * Test read properties to alias bean1. */ @Test public void testToAliasBean1(){ ArrayConverter arrayConverter = new ArrayConverter(String[].class, new StringConverter(), 2); char[] allowedChars = { ':' }; arrayConverter.setAllowedChars(allowedChars); ConvertUtils.register(arrayConverter, String[].class); DangaMemCachedConfig dangaMemCachedConfig = toAliasBean(getResourceBundle("messages.memcached"), DangaMemCachedConfig.class); assertThat( dangaMemCachedConfig, allOf(// hasProperty("serverList", arrayContaining("172.20.3-1.23:11211", "172.20.31.22:11211")), hasProperty("poolName", is("sidsock2")), hasProperty("expireTime", is(180)), hasProperty("weight", arrayContaining(2)), hasProperty("initConnection", is(10)), hasProperty("minConnection", is(5)), hasProperty("maxConnection", is(250)), hasProperty("maintSleep", is(30)), hasProperty("nagle", is(false)), hasProperty("socketTo", is(3000)), hasProperty("aliveCheck", is(false)) // )); }
public Object convert(Object value, Class targetType) { if(super.lookup(targetType) == null) if(targetType.isEnum()) super.register(new EnumConverter(targetType), targetType); else if(targetType.isArray() && targetType.getComponentType().isEnum()) { ArrayConverter arrayconverter = new ArrayConverter(targetType, new EnumConverter(targetType.getComponentType()), 0); arrayconverter.setOnlyFirstToString(false); super.register(arrayconverter, targetType); } return super.convert(value, targetType); }