本文整理匯總了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);
}