本文整理汇总了Java中org.apache.commons.beanutils.Converter类的典型用法代码示例。如果您正苦于以下问题:Java Converter类的具体用法?Java Converter怎么用?Java Converter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Converter类属于org.apache.commons.beanutils包,在下文中一共展示了Converter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ConverterFacade
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
/**
* Construct a converter which delegates to the specified
* {@link Converter} implementation.
*
* @param converter The converter to delegate to
*/
public ConverterFacade(final Converter converter) {
if (converter == null) {
throw new IllegalArgumentException("Converter is missing");
}
this.converter = converter;
}
示例2: mapToBean
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
/**
* map to bean
* 转换过程中,由于属性的类型不同,需要分别转换。
* java 反射机制,转换过程中属性的类型默认都是 String 类型,否则会抛出异常,而 BeanUtils 项目,做了大量转换工作,比 java 反射机制好用
* BeanUtils 的 populate 方法,对 Date 属性转换,支持不好,需要自己编写转换器
*
* @param map 待转换的 map
* @param bean 满足 bean 格式,且需要有无参的构造方法; bean 属性的名字应该和 map 的 key 相同
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void mapToBean(Map<String, Object> map, Object bean) throws IllegalAccessException, InvocationTargetException {
//注册几个转换器
ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
ConvertUtils.register(new SqlTimestampConverter(null), java.sql.Timestamp.class);
//注册一个类型转换器 解决 common-beanutils 为 Date 类型赋值问题
ConvertUtils.register(new Converter() {
// @Override
public Object convert(Class type, Object value) { // type : 目前所遇到的数据类型。 value :目前参数的值。
// System.out.println(String.format("value = %s", value));
if (value == null || value.equals("") || value.equals("null"))
return null;
Date date = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = dateFormat.parse((String) value);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.populate(bean, map);
}
示例3: injectMembers
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
@Override
public void injectMembers(T t)
{
try {
LOG.debug("Processing " + annotation + " for field " + field);
String value = conf.get(annotation.key());
if (value == null) {
if (annotation.optional() == false) {
throw new IllegalArgumentException("Cannot inject " + annotation);
}
return;
}
Converter c = converters.lookup(field.getType());
if (c == null) {
throw new IllegalArgumentException("Cannot find a converter for: " + field);
}
field.set(t, c.convert(field.getType(), value));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
示例4: ArrayConverter
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
/**
* Construct an <b>array</b> <code>Converter</code> with the specified
* <b>component</b> <code>Converter</code> that throws a
* <code>ConversionException</code> if an error occurs.
*
* @param defaultType The default array type this
* <code>Converter</code> handles
* @param elementConverter Converter used to convert
* individual array elements.
*/
public ArrayConverter(final Class<?> defaultType, final Converter elementConverter) {
super();
if (defaultType == null) {
throw new IllegalArgumentException("Default type is missing");
}
if (!defaultType.isArray()) {
throw new IllegalArgumentException("Default type must be an array.");
}
if (elementConverter == null) {
throw new IllegalArgumentException("Component Converter is missing.");
}
this.defaultType = defaultType;
this.elementConverter = elementConverter;
}
示例5: 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);
}
示例6: check
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
public static void check()
{
if (classLoaders.putIfAbsent(Thread.currentThread().getContextClassLoader(), Boolean.TRUE) == null) {
loadDefaultConverters();
for (Map.Entry<Class<?>, Class<? extends StringCodec<?>>> entry : codecs.entrySet()) {
try {
final StringCodec<?> codecInstance = entry.getValue().newInstance();
ConvertUtils.register(new Converter()
{
@Override
public Object convert(Class type, Object value)
{
return value == null ? null : codecInstance.fromString(value.toString());
}
}, entry.getKey());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}
示例7: 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);
}
示例8: 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);
}
示例9: convertToString
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
public String[] convertToString(Object[] objects) {
String[] strings = new String[objects.length];
int i = 0;
for (Object object : objects) {
if (object != null) {
Converter converter = ConvertUtils.lookup(object.getClass());
if (converter != null) {
strings[i++] = converter.convert(object.getClass(), object).toString();
} else {
strings[i++] = object.toString();
}
} else {
strings[i++] = "";
}
}
return strings;
}
示例10: 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;
}
示例11: 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);
}
示例12: create
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
/****
* {@link ConverterFactory}
****/
@Override
public Converter create(CsvEntityContext context) {
GtfsReaderContext ctx = (GtfsReaderContext) context.get(GtfsReader.KEY_CONTEXT);
return new ConverterImpl(ctx);
}
示例13: lookup
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
@Override
public Converter lookup(Class<?> clazz) {
final Converter converter = super.lookup(clazz);
if (converter == null && clazz.isEnum()) {
return ENUM_CONVERTER;
} else {
return converter;
}
}
示例14: lookup
import org.apache.commons.beanutils.Converter; //导入依赖的package包/类
@Override
public Converter lookup(Class clazz) {
//如果是枚举类型则设置为Enum.class
if (clazz.isEnum()) {
clazz = Enum.class;
}
return super.lookup(clazz);
}
示例15: 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);
}