final String[] values = new String[1]; values[0] = (String) value; newValue = ConvertUtils.convert(values, type); newValue = ConvertUtils.convert((String[]) value, type); newValue = ConvertUtils.convert((String) value, type.getComponentType()); newValue = ConvertUtils.convert(((String[]) value)[0], type.getComponentType()); newValue = ConvertUtils.convert((String) value, type); newValue = ConvertUtils.convert(((String[]) value)[0], type);
@Override public void visit(SimpleBeanModelNode node, Element parent) { Element nodeElement = parent.getOwnerDocument().createElement(SCHEMA_NODE_ELEMENT); nodeElement.setAttribute(SCHEMA_NODE_CLASS_ATTRIBUTE, node.getNodeType().getCanonicalName()); for (Entry<String, Object> keyValue : node.getSettingsValues().entrySet()) { // I don't think the API is intended to be used like that // but ConvertUtils wouldn't use the convertToString methods // defined in the converters otherwise. // Even when a built-in converter is available, objects are // still converted with Object::toString which fucks up the // conversion... String value = (String) ConvertUtils.lookup(keyValue.getValue().getClass()).convert(String.class, keyValue.getValue()); if (value == null) { continue; } Element setting = parent.getOwnerDocument().createElement(SCHEMA_PROPERTY_ELEMENT); setting.setAttribute(SCHEMA_PROPERTY_NAME, keyValue.getKey()); setting.setAttribute(SCHEMA_PROPERTY_TYPE, node.getSettingsTypes().get(keyValue.getKey()).getCanonicalName()); setting.appendChild(parent.getOwnerDocument().createCDATASection(value)); nodeElement.appendChild(setting); } parent.appendChild(nodeElement); super.visit(node, nodeElement); }
/** * Convert a string to a specified type. * * @param value The value. * @param type The type. * @return The conversion. */ protected Object convert(String value, Class type) { if (ConvertUtils.lookup(type) != null) { return ConvertUtils.convert(value, type); } else if (Enum.class.isAssignableFrom(type)) { return Enum.valueOf(type, value); } else { throw new UnsupportedOperationException(); } }
/** * Register a new converter for a given enum type, * using the given default value. * * @param type enum type to convert * @param defaultValue default value to use. * @see EnumConverter */ public static <E extends Enum> void registerEnumConverter(Class<E> type, Object defaultValue) { if (EnumConverter.isEnabled(type, type) && ConvertUtils.lookup(type) == null) { Converter converter = new EnumConverter<E>(type, defaultValue); LOGGER.info("for type : " + type + " : " + converter); ConvertUtils.register(converter, type); } }
/** * Convert a string to a specified type. * * @param value The value. * @param type The type. * @return The conversion. */ protected Object convert(String value, Class type) { if (ConvertUtils.lookup(type) != null) { return ConvertUtils.convert(value, type); } else if (Enum.class.isAssignableFrom(type)) { return Enum.valueOf(type, value); } else { throw new UnsupportedOperationException(); } }
public DigesterConfiguratorImpl() { // registers our custom color converter ConvertUtils.register(colorConverter, Color.class); }
/** * Usually the type is the type of the property of the descriptor. Only in the * case of a list, the type is the type of the list members. */ static void ensureConverter(final PropertyDescriptor descriptor, final Class<?> type) { final Converter converter = ConvertUtils.lookup(type); if (converter != null) { return; } final PropertyValueRange<?> range = descriptor.getValueRange(); if (range != null) { ConvertUtils.register(new PropertyValueRangeConverter(range, type), type); } else { ConvertUtils.register(new FromStringTypeConverter(type), type); } }
final String[] values = new String[1]; values[0] = (String) value; newValue = ConvertUtils.convert(values, type); newValue = ConvertUtils.convert((String[]) value, type); newValue = ConvertUtils.convert((String) value, type.getComponentType()); newValue = ConvertUtils.convert(((String[]) value)[0], type.getComponentType()); newValue = ConvertUtils.convert((String) value, type); newValue = ConvertUtils.convert(((String[]) value)[0], type);
/** * Convert a string to a specified type. * * @param value * The value. * @param type * The type. * @return The conversion. */ public Object convert(final String value, final Class type) { if (ConvertUtils.lookup(type) != null) { return ConvertUtils.convert(value, type); } else if (Enum.class.isAssignableFrom(type)) { return Enum.valueOf(type, value); } else { throw new UnsupportedOperationException("A converter was not found for " + type.getName()); } } }
/** * Adds supported core value type. * * @param clazz The core value type. * @param converter Converter for the specified core value type. */ public static void addCoreValueType(Class<?> clazz, Converter converter) { ConvertUtils.register(converter, clazz); values.add(clazz); }
public DefaultFieldMapping(Class<?> entityType, String csvFieldName, String objFieldName, Class<?> objFieldType, boolean required) { super(entityType, csvFieldName, objFieldName, required); _objFieldType = objFieldType; _converter = ConvertUtils.lookup(objFieldType); if (_converter == null && objFieldType.equals(Object.class)) _converter = new DefaultConverter(); }
private void parseSingleProperty(Element propertyElement, SimpleBeanModelNode owner) { String typeName = propertyElement.getAttribute(SCHEMA_PROPERTY_TYPE); String name = propertyElement.getAttribute(SCHEMA_PROPERTY_NAME); Class<?> type; try { type = ClassUtils.getClass(typeName); } catch (ClassNotFoundException e) { e.printStackTrace(); return; } ConvertUtils.convert(new Object()); Object value = ConvertUtils.convert(propertyElement.getTextContent(), type); owner.addProperty(name, value, type); }
/** * Serializes the specified user-defined value type object to * <code>JsonPrimitive</code> representation. */ public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { Class<?> type = Types.getRawType(typeOfSrc); Converter converter = ConvertUtils.lookup(type); if ((converter != null && converter instanceof AbstractConverter)) { String string = (String) ConvertUtils.convert(src, String.class); if (string != null) { return new JsonPrimitive(string); } } return new JsonPrimitive(src.toString()); }
void registerConverter(Class<?> clazz, Converter converter) { convertedClasses.add(clazz); // yuck, configuring the beanutils singleton - should be OK though, it's unlikely a 3rd party would use our model classes. ConvertUtils.register(converter, clazz); }
public static void registerConverter(Class<?> type) throws IllegalAccessException, InstantiationException, ClassNotFoundException { if (ConvertUtils.lookup(type) == null) { registerConverter0(type); } }
@Override public Object createObject(Attributes attributes) throws Exception { // create callparamrule int paramIndex = Integer.parseInt(attributes.getValue("paramnumber")); String attributeName = attributes.getValue("attrname"); String type = attributes.getValue("type"); String value = attributes.getValue("value"); Rule objectParamRule = null; // type name is requried if (type == null) { throw new RuntimeException("Attribute 'type' is required."); } // create object instance Object param = null; Class<?> clazz = Class.forName(type); if (value == null) { param = clazz.newInstance(); } else { param = ConvertUtils.convert(value, clazz); } if (attributeName == null) { objectParamRule = new ObjectParamRule(paramIndex, param); } else { objectParamRule = new ObjectParamRule(paramIndex, attributeName, param); } return objectParamRule; } }
/** * Marshals the specified user-defined value type object to single XML value * string representation. * * @throws If the specified object is not a user-defined value type. */ @Override public String marshal(BoundType v) throws Exception { Class<? extends Object> type = v.getClass(); if (!Types.isUserDefinedValueType(type)) { throw new IllegalArgumentException("Type [" + type + "] must be an user-defined value type; " + "@XmlJavaTypeAdapter(ValueTypeXmlAdapter.class) " + "can be annotated to user-defined value type and field only"); } Converter converter = ConvertUtils.lookup(type); if ((converter != null && converter instanceof AbstractConverter)) { String string = (String) ConvertUtils.convert(v, String.class); if (string != null) { return string; } } return v.toString(); }
private static void registDateConvert(){ //日期特殊处理 CustDateConvert dateConverter = new CustDateConvert(); //date -> string的转换,处理BeanUtils.populate中的日期转换 ConvertUtils.register(dateConverter, Date.class); //string -> date的转换,处理BeanUtils.describe中的日期转换 ConvertUtils.register(dateConverter, String.class); }
/** * Delegates to {@link ConvertUtils#lookup(java.lang.Class)} to create a * converter instance. * * @see ConverterFactory#createConverter(Class, Class, Hints). */ public Converter createConverter(Class source, Class target, Hints hints) { //only do strings if ( source.equals( String.class ) ) { org.apache.commons.beanutils.Converter converter = ConvertUtils.lookup( target ); if ( converter != null ) { return new CommonsConverterWrapper( converter ); } } return null; }