List of usage examples for org.apache.commons.beanutils ConvertUtils convert
public static Object convert(String values[], Class clazz)
Convert an array of specified values to an array of objects of the specified class (if possible).
For more details see ConvertUtilsBean
.
From source file:com.jredrain.dao.BeanResultTransFormer.java
/** * ??Setter/*from w w w .j a v a2 s . c o m*/ * * @param data * @param name * @param value * @throws Exception */ protected void handle(T data, String name, Object value) throws Exception { // if (value == null) return; List<Setter> setterList = setters.get(name.toLowerCase()); if (setterList == null || setterList.isEmpty()) return; //setter if (setterList.size() == 1) { Setter setter = setterList.get(0); if (value != null && !setter.type.isInstance(value)) { value = ConvertUtils.convert(value, setter.type); } setter.invoke(data, value); return; } //? List<Setter> compatibleList = new ArrayList<Setter>(); //? List<Setter> numericList = new ArrayList<Setter>(); for (Setter setter : setterList) { Class<?> type = setter.type; //? if (value != null && type == value.getClass()) { setter.invoke(data, value); } //? if (type.isInstance(value)) { compatibleList.add(setter); } else if (Number.class.isAssignableFrom(type) && value instanceof Number) {//? numericList.add(setter); } } if (compatibleList.size() == 1) { compatibleList.get(0).invoke(data, value); return; } if (compatibleList.isEmpty() && numericList.size() == 1) { value = ConvertUtils.convert(value, numericList.get(0).type); numericList.get(0).invoke(data, value); return; } if (compatibleList.size() > 0 || numericList.size() > 0) { throw new Exception("ambiguous setter methods to call"); } }
From source file:com.siberhus.tdfl.DflBaseTest.java
@Test public void testTemporalTypeConverter() throws Exception { java.sql.Date expectedDate = new java.sql.Date( new SimpleDateFormat("dd/MM/yyyy").parse("11/11/2011").getTime()); java.sql.Date actualDate = (java.sql.Date) ConvertUtils.convert("11/11/2011", java.sql.Date.class); Assert.assertEquals(expectedDate, actualDate); try {//w w w . jav a 2 s .c o m ConvertUtils.convert("invalid", java.sql.Date.class); } catch (Exception e) { Assert.assertEquals("org.apache.commons.beanutils.ConversionException", e.getClass().getName()); } }
From source file:com.easyget.commons.csv.bean.CsvToBean.java
/** * Creates a single object from a line from the csv file. * @param mapper - MappingStrategy//from w ww . j a v a2 s. c om * @param line - array of Strings from the csv file. * @return - object containing the values. * @throws IllegalAccessException - thrown on error creating bean. * @throws InvocationTargetException - thrown on error calling the setters. * @throws InstantiationException - thrown on error creating bean. * @throws IntrospectionException - thrown on error getting the PropertyDescriptor. * @throws ParseException */ protected T processLine(MappingStrategy<T> mapper, Line line, FieldFormater formater) throws IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException, ParseException { T bean = mapper.createBean(); String[] fields = line.getLine(); for (int col = 0; col < fields.length; col++) { PropertyDescriptor prop = mapper.findDescriptor(col); String value = checkForTrim(fields[col], prop); // ? if (formater != null) value = formater.parse(value); try { Class<?> clazz = prop.getPropertyType(); Object ov = ConvertUtils.convert(value, clazz); PropertyUtils.setProperty(bean, prop.getName(), ov); } catch (NoSuchMethodException e) { // } } return bean; }
From source file:com.aistor.modules.cms.service.ArticleService.java
/** * ??/*from w w w.j a v a2s.com*/ * @return new Object[]{?Id,Id,} */ public List<Object[]> findByIds(String ids) { List<Object[]> list = Lists.newArrayList(); Long[] idss = (Long[]) ConvertUtils.convert(StringUtils.split(ids, ","), Long.class); if (idss.length > 0) { List<Article> l = articleDao.findByIdIn(idss); for (Article e : l) { list.add(new Object[] { e.getCategory().getId(), e.getId(), StringUtils.abbr(e.getTitle(), 50) }); } } return list; }
From source file:edu.scripps.fl.pubchem.db.PCAssayResult.java
@Transient public Object getValue(PCAssayColumn column) { Object obj = ConvertUtils.convert(getAllValues().get(column.getTID() - 1), column.getTypeClass()); return obj; }
From source file:com.rodaxsoft.mailgun.MailingListMemberManager.java
ListMember getListMember(String email) throws ContextedException { final String path = "/lists/" + listAddress + "/members/" + email; RESTResponse response = invokeGet(path); ListMember member = null;/*from ww w . j a v a 2s .c om*/ if (response.success()) { JSONObject obj = response.toJSONObject().getJSONObject("member"); member = (ListMember) ConvertUtils.convert(obj, ListMember.class); } return member; }
From source file:com.nfwork.dbfound.json.JSONDynaBean.java
public void set(String name, Object value) { DynaProperty property = getDynaProperty(name); if (property.getType() == null) { throw new NullPointerException("Unspecified property type for " + name); }// w w w . ja v a2 s. c o m if (value == null) { if (property.getType().isPrimitive()) { throw new NullPointerException("Property type for " + name + " is primitive"); } } else if (!isDynaAssignable(property.getType(), value.getClass())) { try { value = ConvertUtils.convert(String.valueOf(value), value.getClass()); } catch (Exception e) { throw new IllegalArgumentException("Unassignable property type for " + name); } } // log.debug( name + " = " + value ); dynaValues.put(name, value); }
From source file:com.creactiviti.piper.core.task.SpelTaskEvaluator.java
private <T> MethodExecutor cast(Class<T> type) { return (ctx, target, args) -> { T value = type.cast(ConvertUtils.convert(args[0], type)); return new TypedValue(value); };// w w w .j a va2 s . c om }
From source file:com.mawujun.utils.BeanUtils.java
/** * Apache BeanUtils?.//from w w w . ja va 2s. c o m * ???Integer??0"10.123"?Intger0 * @param value ?. * @param toType ?. */ public static <T> T convert(String value, Class<T> toType) { //??? if (toType == String.class) { return (T) value; } try { return (T) ConvertUtils.convert(value, toType); } catch (Exception e) { throw ReflectUtils.convertReflectionExceptionToUnchecked(e); } }
From source file:com.mawujun.utils.bean.BeanUtils.java
/** * Apache BeanUtils?.//from ww w .jav a 2 s . c o m * ???Integer??0"10.123"?Intger0 * @param value ?. * @param toType ?. */ public static <T> T convert(String value, Class<T> toType) { // //??? // if(toType==String.class){ // return (T)value; // } try { return (T) ConvertUtils.convert(value, toType); } catch (Exception e) { throw ReflectUtils.convertReflectionExceptionToUnchecked(e); } }