本文整理汇总了Java中org.apache.commons.beanutils.Converter.convert方法的典型用法代码示例。如果您正苦于以下问题:Java Converter.convert方法的具体用法?Java Converter.convert怎么用?Java Converter.convert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.beanutils.Converter
的用法示例。
在下文中一共展示了Converter.convert方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: translateFromCSVToObject
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
public void translateFromCSVToObject(CsvEntityContext context,
Map<String, Object> csvValues, BeanWrapper object) {
if (isMissingAndOptional(csvValues))
return;
Converter converter = create(context);
String entityId = (String) csvValues.get(_csvFieldName);
Object entity = converter.convert(_objFieldType, entityId);
object.setPropertyValue(_objFieldName, entity);
}
示例2: testGetConverter
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
/**
* Test of getConverter method, of class UtilsBeanFactory.
*/
@Test
public void testGetConverter() {
System.out.println("getConverter");
Class from = TypeHandler.class;
Class to = Integer.class;
Converter expResult = null;
Converter result = UtilsBeanFactory.getConverter(from, to);
assertNotNull(result);
result = UtilsBeanFactory.getConverter(Date2.class, String.class);
String res = result.convert(String.class, Calendar.getInstance().getTime());
assertNotNull(result);
result = UtilsBeanFactory.getConverter(Date2.class, UtilsBeanFactoryTest.class);
assertNull(result);
}
示例3: convert
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
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);
}
示例4: convert
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
/**
* 数据转换
*
* @param source
* 源数据
*
* @param targetType
* 目标类型
*
* @return
*/
public static Object convert(Object source, Class targetType) {
Object result = source;
if (null == targetType) {
return result;
}
Converter converter = converterMap.get(targetType);
if (null != converter) {
result = converter.convert(targetType, source);
}
return result;
}
示例5: convert
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
@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);
}
示例6: convertType
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
public static <S, D> D convertType(Object obj, Class<S> srcClass, Class<D> destClass) {
if (srcClass.equals(destClass)) {
return (D) JsonUtil.copyObject(obj);
}
Converter converter = convertUtils.lookup(srcClass, destClass);
return converter.convert(destClass, obj);
}
示例7: convert
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
/**
* Convert the given object to be castable to the given <code>goal</code> type.
*
* @param obj
* the object to convert
* @param goal
* the goal type
*
* @return the converted object
*/
public static Object convert(Object obj, Class<?> goal) {
if (null == obj) {
return obj;
} else {
if (goal.isInstance(obj)) {
return obj;
} else if (asPrimitive(goal).equals(asPrimitive(obj.getClass()))) {
return obj;
} else {
Converter converter = ConvertUtils.lookup(goal);
return (null != converter) ? converter.convert(goal, obj) : obj;
}
}
}
示例8: testConvert
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
/**
* Test of convert method, of class ClassConverter.
*/
@Test
public void testConvert() {
LOGGER.info("class convert test");
Converter lookup = UtilsBeanFactory.getConverter(String.class, Class.class);
String hex = ClassConverter.class.getName();
Class convert = lookup.convert(Class.class, hex);
assertNotNull(convert);
assertEquals(hex, convert.getName());
Class fromString = (Class) TypeHandler.fromString(Class.class, hex);
assertNotNull(fromString);
assertEquals(hex, fromString.getName());
fromString = (Class) TypeHandler.fromString(Class.class, "no.such.class");
assertNull(fromString);
}
示例9: convertValue
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
private Object convertValue(final Object value, final Class<?> type) {
if (value == null) {
return null;
}
final Converter converter = getBeanUtilsBean().getConvertUtils().lookup(type);
if (converter != null) {
return converter.convert(type, value);
} else {
return value;
}
}
示例10: convert
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
/**
* Convert an object to a specified type.
*
* @param <T>
* @param to
* @param val
* @return
*/
public static <T> T convert(Class<T> to, Object val) {
Converter lookup = UtilsBeanFactory.getConverter(val.getClass(), to);
if (lookup != null) {
return lookup.convert(to, val);
}
return null;
}
示例11: convert
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
/**
* オブジェクトを指定したクラスのインスタンスに変換して返します。
* 変換の必要が無い場合はそのまま返します。
*
* @param expectedClass 戻り型として期待するクラス
* @param value 変換する値
* @return 変換後の値 (expectedClassのインスタンス)
*/
public static Object convert(Class expectedClass, Object value) {
if (Object.class.equals(expectedClass) ||
(value != null && expectedClass.isAssignableFrom(value.getClass()))) {
return value;
}
if (String.class.equals(expectedClass)) {
return (value != null) ? value.toString() : null;
}
if (Boolean.class.equals(expectedClass) || Boolean.TYPE.equals(expectedClass)) {
if (value instanceof Boolean) {
return value;
}
return Boolean.valueOf(ObjectUtil.booleanValue(value, false));
}
Converter converter = ConvertUtils.lookup(expectedClass);
if (converter != null) {
if (value != null) {
return converter.convert(expectedClass, value);
}
if (expectedClass.isPrimitive() || Number.class.isAssignableFrom(expectedClass)) {
if (BigInteger.class.isAssignableFrom(expectedClass)) {
return BigInteger.ZERO;
} else if (BigDecimal.class.isAssignableFrom(expectedClass)) {
return BigDecimal.valueOf(0);
}
return converter.convert(expectedClass, value);
}
}
return value;
}
示例12: convert
import org.apache.commons.beanutils.Converter; //导入方法依赖的package包/类
/**
* Convert an object to a specified type.
*
* @param <T> the type.
* @param to to target type.
* @param val the from value.
* @return a new object of converted values.
*/
public static <T> T convert(Class<T> to, Object val) {
Converter lookup = UtilsBeanFactory.getConverter(val.getClass(), to);
return lookup.convert(to, val);
}