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:ir.caebabe.ninja.providers.ProcessEngineProvider.java
private void setFildValue(String value, Field field, Object instance) { Class<?> type = field.getType(); Object convertedObject = ConvertUtils.convert(value, type); try {//from www . j a va2 s . co m field.setAccessible(true); field.set(instance, convertedObject); logger.debug("field '{}' updated", field.getName()); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
From source file:com.helpinput.core.Utils.java
public static void setFieldValue(Object target, Field theField, Object value) { if (theField != null) { setAccess(theField);// www . j a v a 2 s . co m try { Object newValue = ConvertUtils.convert(value, theField.getType()); theField.set(target, newValue); } catch (Exception e) { e.printStackTrace(); logger.info(MessageFormat.format("convert error:{0},{1},{2}", target, theField, value)); } } }
From source file:com.helpinput.utils.Utils.java
public static void setFieldValue(Object target, Field theField, Object value) { if (theField != null) { accessible(theField);//from w w w .j av a2s . c om try { Object newValue = ConvertUtils.convert(value, theField.getType()); theField.set(target, newValue); } catch (Exception e) { e.printStackTrace(); logger.info(MessageFormat.format("convert error:{0},{1},{2}", target, theField, value)); } } }
From source file:com.bstek.dorado.data.method.MethodAutoMatchingUtils.java
/** * ???//from w ww. ja va 2 s . c o m * * @param methodDescriptor * ?? * @param object * ?? * @param parameters * ?? * @return ? * @throws Exception */ @SuppressWarnings("unchecked") private static Object invokeMethod(MethodDescriptor methodDescriptor, Object object, Object[] parameters) throws Exception { Method method = methodDescriptor.getMethod(); Class<?>[] parameterTypes = method.getParameterTypes(); int[] argIndexs = methodDescriptor.getArgIndexs(); Object[] realArgs = new Object[argIndexs.length]; for (int i = 0; i < argIndexs.length; i++) { Object arg = parameters[argIndexs[i]]; if (arg != null) { Class<?> defType = parameterTypes[i]; Class<?> argType = ProxyBeanUtils.getProxyTargetType(arg.getClass()); if (!defType.isAssignableFrom(argType)) { if (Number.class.isAssignableFrom(defType) && Number.class.isAssignableFrom(argType)) { arg = NumberUtils.convertNumberToTargetClass((Number) arg, (Class<? extends Number>) defType); } else if (isSimpleType(defType) || isSimpleType(argType)) { arg = ConvertUtils.convert(arg, defType); } } } realArgs[i] = arg; } return method.invoke(object, realArgs); }
From source file:com.helpinput.core.Utils.java
@SuppressWarnings("unchecked") public static <T> T convert(Object value, Class<? extends T> targetType) { return (T) ConvertUtils.convert(value, targetType); }
From source file:com.appleframework.jmx.core.services.MBeanServiceImpl.java
/** * Map keys are of the format://from w w w . j a v a 2s . com * attr+<applicationId>+<attrName> * */ private List<ObjectAttribute> buildAttributeList(Map<String, String[]> attributes, ApplicationConfig appConfig, ObjectAttributeInfo[] objAttributes, ServerConnection connection, ObjectName objectName) { String applicationId = appConfig.getApplicationId(); Iterator<String> it = attributes.keySet().iterator(); List<ObjectAttribute> attributeList = new LinkedList<ObjectAttribute>(); while (it.hasNext()) { String param = it.next(); // look for keys which only start with "attr+" if (param.startsWith("attr+")) { StringTokenizer tokenizer = new StringTokenizer(param, "+"); if (tokenizer.countTokens() != 3) { throw new RuntimeException("Invalid param name: " + param); } tokenizer.nextToken(); // equals to "attr" if (applicationId.equals(tokenizer.nextToken())) { // applicationId String attrName = tokenizer.nextToken(); String attrType = getAttributeType(connection, objAttributes, attrName, objectName); String[] attrValues = attributes.get(param); Object typedValue = null; if (attrType.startsWith("[")) { // it is an array // the first elements in the array is dummy to allow // empty string to be saved String[] actualValue = new String[attrValues.length - 1]; for (int i = 0; i < actualValue.length; i++) { actualValue[i] = attrValues[i + 1]; } try { typedValue = ConvertUtils.convert(actualValue, Class.forName(attrType)); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } else { typedValue = getTypedValue(appConfig, attrValues[0], attrType); } attributeList.add(new ObjectAttribute(attrName, typedValue)); } } } return attributeList; }
From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java
/** * Convert.//from w w w . j av a2s . co m * * @param value the value * @param type the type * @return the object */ protected static Object convert(String value, Class<?> type) { Object newValue = null; if (type.isArray()) { // Indexed value into array newValue = ConvertUtils.convert(value, type.getComponentType()); } else { // Value into scalar newValue = ConvertUtils.convert(value, type); } return newValue; }
From source file:com.appleframework.jmx.core.services.MBeanServiceImpl.java
public static Object getTypedValue(ApplicationConfig appConfig, String value, String type) { if (type.equals("int")) { type = "java.lang.Integer"; } else if (type.equals("long")) { type = "java.lang.Long"; } else if (type.equals("short")) { type = "java.lang.Short"; } else if (type.equals("float")) { type = "java.lang.Float"; } else if (type.equals("double")) { type = "java.lang.Double"; } else if (type.equals("char")) { type = "java.lang.Character"; } else if (type.equals("boolean")) { type = "java.lang.Boolean"; } else if (type.equals("byte")) { type = "java.lang.Byte"; }/* www . j a va2 s .co m*/ try { /* handle ObjectName as a special type */ if (type.equals("javax.management.ObjectName")) { Class<?> clazz = Class.forName(type, true, appConfig.getApplicationClassLoader()); try { Constructor<?> ctor = clazz.getConstructor(new Class[] { String.class }); return ctor.newInstance(new Object[] { value }); } catch (Exception e) { throw new RuntimeException(e); } } /* other types */ return ConvertUtils.convert(value, Class.forName(type)); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }
From source file:com.feilong.core.bean.ConvertUtil.java
/** * Convert an array of specified values to an array of objects of the specified class (if possible). * /*from www . j av a2s.c o m*/ * <p> * If the specified Java class is itself an array class, this class will be the type of the returned value.<br> * Otherwise, an array will be constructed whose component type is the specified class. * </p> * * @param <T> * the generic type * @param toBeConvertedValue * the values * @param targetType * ?? * @return <code>toBeConvertedValue</code> null,null<br> * <code>targetType</code> null, {@link NullPointerException}<br> * ? {@link ConvertUtils#convert(String[], Class)} * @see org.apache.commons.beanutils.ConvertUtils#convert(String[], Class) * @see org.apache.commons.beanutils.ConvertUtilsBean#convert(String[], Class) * @since 1.6.0 */ @SuppressWarnings("unchecked") public static <T> T[] toArray(String[] toBeConvertedValue, Class<T> targetType) { return null == toBeConvertedValue ? null : (T[]) ConvertUtils.convert(toBeConvertedValue, targetType); }
From source file:com.feilong.core.bean.ConvertUtil.java
/** * <code>toBeConvertedValue</code> ? <code>targetType</code> . * /* w w w. j av a 2 s . co m*/ * <h3>:</h3> * * <blockquote> * * <pre class="code"> * ConvertUtil.convert("1", Integer.class) =1 * ConvertUtil.convert("", Integer.class) =0 * ConvertUtil.convert("1", Long.class) =1 * </pre> * * </blockquote> * * <h3>,??, Type[] ? Class []:</h3> * * <blockquote> * * ?: * * <pre class="code"> * * Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); * int length = actualTypeArguments.length; * Class{@code <?>}[] klasses = new Class{@code <?>}[length]; * for (int i = 0, j = length; i {@code <} j; ++i){ * klasses[i] = (Class{@code <?>}) actualTypeArguments[i]; * } * * return klasses; * * </pre> * * ???: * * <pre class="code"> * Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); * return convert(actualTypeArguments, Class[].class); * </pre> * * </blockquote> * * <h3>?:</h3> * * <blockquote> * * <ol> * * <li><code>targetType</code>?,<b>value</b>,<br> * <code>ConvertUtil.convert("zh_CN", Locale.class)</code> ?converter,"zh_CN". * </li> * * <li>??,</li> * * <li> <code>toBeConvertedValue</code> <code>toBeConvertedValue.getClass().isArray()</code> {@link Collection} * <blockquote> * * <dl> * <dt> <code>targetType</code> ?</dt> * <dd> * <p> * <span style="color:red">?</span>?,<br> * ??{@link AbstractConverter#convert(Class, Object)}, {@link AbstractConverter#convertArray(Object)} * </p> * </dd> * * <dt> <code>targetType</code> </dt> * <dd>?? {@link org.apache.commons.beanutils.converters.ArrayConverter#convertToType(Class, Object) ArrayConverter#convertToType(Class, * Object)} targetType ,? <code>toBeConvertedValue</code>?, ? <code>toBeConvertedValue</code> ??</dd> * * </dl> * * </blockquote> * </li> * </ol> * </blockquote> * * @param <T> * the generic type * @param toBeConvertedValue * ??/ * @param targetType * ?? * @return <code>toBeConvertedValue</code> null,null<br> * <code>targetType</code> null, {@link NullPointerException}<br> * ? {@link org.apache.commons.beanutils.ConvertUtils#convert(Object, Class)} * @see org.apache.commons.beanutils.ConvertUtils#convert(Object, Class) * @see org.apache.commons.beanutils.converters.AbstractConverter#convert(Class, Object) * @see org.apache.commons.beanutils.converters.ArrayConverter#convertToType(Class, Object) */ @SuppressWarnings("unchecked") public static <T> T convert(Object toBeConvertedValue, Class<T> targetType) { Validate.notNull(targetType, "targetType can't be null!"); return null == toBeConvertedValue ? null : (T) ConvertUtils.convert(toBeConvertedValue, targetType); }