public static ConverterManager getInstance() { if (instance == null) instance = new ConverterManager(); return instance; }
public static <SS, TT> Converter<SS, TT>[] cloneIfSupported(Converter<SS, TT>[] prototypes) { Converter[] result = new Converter[prototypes.length]; for (int i = 0; i < prototypes.length; i++) result[i] = cloneIfSupported(prototypes[i]); return result; }
@SuppressWarnings("unchecked") protected CommaSeparatedListConverter(Class<T> targetComponentType) { super(ConverterManager.getInstance().createConverter(String.class, targetComponentType)); this.targetComponentType = targetComponentType; this.targetType = ArrayUtil.arrayType(targetComponentType); }
return createConverter(sourceType, wrapperClass); result = createToStringConverter(sourceType); if (result != null) return result; result = tryToCreateStringConverter(targetType); if (result != null) return result; result = tryToCreateBooleanConverter(targetType); if (result != null) return result; return new ToCollectionConverter(targetType); result = tryToCreateFactoryConverter(sourceType, targetType); if (result != null) return result;
public <S, T> Converter<S, T> createConverter(Class<S> sourceType, Class<T> targetType) { // check preconditions if (targetType == null) throw new ConversionException("targetType must be specified"); // check if we already know how to do this conversion ConversionTypes conversionTypes = new ConversionTypes(sourceType, targetType); Converter result = converterPrototypes.get(conversionTypes); if (result != null) return cloneIfSupported(result); // we need to investigate... result = searchAppropriateConverter(sourceType, targetType); // ...and cache the result for future requests if (result != null && result.isParallelizable()) converterPrototypes.put(conversionTypes, result); // inject context if appropriate injectContext(result); // done return result; }
private ConverterManager() { init(); }
@Override public void setContext(Context context) { this.context = context; for (Converter converter : converterPrototypes.values()) injectContext(converter); }
@Override public Object convert(String sourceValue) throws ConversionException { return ConverterManager.convertAll(ArrayFormat.parse(sourceValue, ",", String.class), realConverter, targetComponentType); }
protected void init() { this.configuredConverterClasses = new OrderedMap<ConversionTypes, Class<? extends Converter>>(); this.converterPrototypes = new HashMap<ConversionTypes, Converter>(); try { if (IOUtil.isURIAvailable(CUSTOM_SETUP_FILENAME)) { CONFIG_LOGGER.debug("Reading custom converter config: {}", CUSTOM_SETUP_FILENAME); readConfigFile(CUSTOM_SETUP_FILENAME); } readConfigFile(DEFAULT_SETUP_FILENAME); } catch (IOException e) { throw new ConfigurationError("Error reading setup file: " + DEFAULT_SETUP_FILENAME); } }
private Converter tryToCreateFactoryConverter(Class sourceType, Class targetType) { { // find instance method <targetType>Value() in source type String methodName = StringUtil.uncapitalize(targetType.getSimpleName()) + "Value"; Method typeValueMethod = BeanUtil.findMethod(sourceType, methodName, new Class[0]); if (typeValueMethod != null && (typeValueMethod.getModifiers() & Modifier.STATIC) == 0) return new SourceClassMethodInvoker(sourceType, targetType, typeValueMethod); } { // find static getInstance() method in target type Method getInstanceMethod = BeanUtil.findMethod(targetType, "getInstance", sourceType); if (getInstanceMethod != null && (getInstanceMethod.getModifiers() & Modifier.STATIC) == Modifier.STATIC) return new StaticTargetClassMethodInvoker(sourceType, targetType, getInstanceMethod); } { // find static valueOf() method in target type Method valueOfMethod = BeanUtil.findMethod(targetType, "valueOf", sourceType); if (valueOfMethod != null && (valueOfMethod.getModifiers() & Modifier.STATIC) == Modifier.STATIC) return new StaticTargetClassMethodInvoker(sourceType, targetType, valueOfMethod); } { // find target type constructor which takes source type argument Constructor constructor = BeanUtil.findConstructor(targetType, sourceType); if (constructor != null) return new ConstructorInvoker(sourceType, constructor); } return findPoorConfiguredMatch(sourceType, targetType); }
@Override public void reset() { init(); }
/** * Converts an object of a given type to an object of the target type. * @param source the object to convert * @param targetType the target type of the conversion * @param datePattern the date pattern to apply * @param timePattern the time pattern to apply * @param timestampPattern the timestamp pattern to apply * @param <TT> the target type * @return an object of the target type * @throws ConversionException if conversion fails */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <TT> TT convert(Object source, Class<TT> targetType, String datePattern, String timePattern, String timestampPattern) throws ConversionException { if (logger.isDebugEnabled()) logger.debug("Converting " + source + (source != null ? " (" + source.getClass().getName() + ")" : "") + " to " + targetType); if (source == null || targetType.equals(source.getClass())) return (TT) source; Converter converter = ConverterManager.getInstance().createConverter(source.getClass(), targetType); return (TT) converter.convert(source); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Object clone() { try { MultiConverterWrapper copy = (MultiConverterWrapper) super.clone(); copy.components = ConverterManager.cloneIfSupported(this.components); return copy; } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } }
params[i] = null; else { Converter converter = ConverterManager.getInstance().createConverter(arg.getClass(), paramTypes[i]); params[i] = converter.convert(arg);
return XMLNode2StringConverter.format(source); } else { ConverterManager manager = ConverterManager.getInstance(); Converter converter = manager.createConverter(sourceType, String.class); return (String) converter.convert(source);