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:no.abmu.finances.domain.DomainTestHelper.java
public static Object populateBean(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; if (propertyDescriptor.getName().equals("id")) { continue; }// ww w . j a v a 2 s . c o m if (P_TYPES.contains(propertyDescriptor.getPropertyType()) && propertyDescriptor.getWriteMethod() != null) { Object obje; obje = ConvertUtils.convert( String.valueOf((int) (System.currentTimeMillis() + (int) (Math.random() * 100d))), propertyDescriptor.getPropertyType()); PropertyUtils.setProperty(obj, propertyDescriptor.getName(), obje); } } return obj; }
From source file:no.abmu.test.domainmodels.DomainTestHelper.java
public static Object populateBean(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; if (propertyDescriptor.getName().equals("id")) { continue; } else if (propertyDescriptor.getName().equals("valueAsString")) { continue; } else if (P_TYPES.contains(propertyDescriptor.getPropertyType()) && propertyDescriptor.getWriteMethod() != null) { Object obje;/*from w w w . j a v a 2 s .c o m*/ // obje=ConvertUtils.convert(String.valueOf(System.currentTimeMillis() // +(long)(Math.random()*100d)),propertyDescriptor.getPropertyType()); obje = ConvertUtils.convert( String.valueOf((int) (System.currentTimeMillis() + (int) (Math.random() * 100d))), propertyDescriptor.getPropertyType()); PropertyUtils.setProperty(obj, propertyDescriptor.getName(), obje); } } return obj; }
From source file:no.abmu.test.utilh3.DomainTestHelper.java
public static Object populateBean(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; if (propertyDescriptor.getName().equals("id")) { continue; } else if (propertyDescriptor.getName().equals("valueAsString")) { continue; } else if (P_TYPES.contains(propertyDescriptor.getPropertyType()) && propertyDescriptor.getWriteMethod() != null) { Object obje;/* w w w . j ava2 s .c o m*/ // obje=ConvertUtils.convert(String.valueOf(System.currentTimeMillis() // +(long)(Math.random()*100d)),propertyDescriptor.getPropertyType()); obje = ConvertUtils.convert( String.valueOf((int) (System.currentTimeMillis() + (int) (Math.random() * 100d))), propertyDescriptor.getPropertyType()); PropertyUtils.setProperty(obj, propertyDescriptor.getName(), obje); } } return obj; }
From source file:nz.co.senanque.messaging.GenericEndpoint.java
private void unpackRoot(Element element, Object context) { ValidationSessionHolder validationSessonHolder = new ValidationSessionHolderImpl(getValidationEngine()); validationSessonHolder.bind(context); try {/*w w w . j ava 2 s . c om*/ @SuppressWarnings("unchecked") Iterator<Text> itr = (Iterator<Text>) element .getDescendants(new ContentFilter(ContentFilter.TEXT | ContentFilter.CDATA)); while (itr.hasNext()) { Text text = itr.next(); String name = getName(text); if (name.equals("id") || name.equals("version")) { continue; } try { Class<?> targetType = PropertyUtils.getPropertyType(context, name); Object value = ConvertUtils.convert(text.getValue(), targetType); PropertyUtils.setProperty(context, name, value); log.debug("name {} value {}", name, text.getValue()); } catch (IllegalAccessException e) { // Ignore these and move on log.debug("{} {}", name, e.getMessage()); } catch (InvocationTargetException e) { // Ignore these and move on log.debug("{} {}", name, e.getTargetException().toString()); } catch (NoSuchMethodException e) { // Ignore these and move on log.debug("{} {}", name, e.getMessage()); } } } finally { validationSessonHolder.close(); } }
From source file:nz.co.senanque.messaging.OrderReplyEndpoint.java
public void issueResponseFor(Message<org.w3c.dom.Document> orderResponse) { log.debug("processed orderResponse: correlationId {}", orderResponse.getHeaders().get(IntegrationMessageHeaderAccessor.CORRELATION_ID, Long.class)); org.w3c.dom.Document doc = orderResponse.getPayload(); Document document = new DOMBuilder().build(doc); Element root = document.getRootElement(); Order context = new Order(); @SuppressWarnings("unchecked") Iterator<Text> itr = (Iterator<Text>) root .getDescendants(new ContentFilter(ContentFilter.TEXT | ContentFilter.CDATA)); while (itr.hasNext()) { Text text = itr.next();/*ww w .j av a2s .c o m*/ log.debug("name {} value {}", text.getParentElement().getName(), text.getValue()); String name = getName(text); try { Class<?> targetType = PropertyUtils.getPropertyType(context, name); Object value = ConvertUtils.convert(text.getValue(), targetType); PropertyUtils.setProperty(context, name, value); } catch (IllegalAccessException e) { // Ignore these and move on log.debug("{} {}", name, e.getMessage()); } catch (InvocationTargetException e) { // Ignore these and move on log.debug("{} {}", name, e.getMessage()); } catch (NoSuchMethodException e) { // Ignore these and move on log.debug("{} {}", name, e.getMessage()); } } }
From source file:org.alfresco.rest.framework.resource.parameters.Params.java
@Override public T getParameter(String parameterName, Class<T> clazz) throws InvalidArgumentException { String param = getParameter(parameterName); if (param == null) return null; Object obj = ConvertUtils.convert(param, clazz); if (obj != null && obj.getClass().equals(clazz)) { return (T) obj; }/* ww w . j a v a 2s . c o m*/ throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { parameterName }); }
From source file:org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker.java
/** * Get the property value, converted to the requested type. * //from w w w .ja va 2 s . co m * @param propertyName name of the parameter * @param type int * @param returnType type of object to return * @return the converted parameter value. Null, if the property has no * value. * @throws IllegalArgumentException when no conversion for the given * returnType is available or if returnType is null. * @throws InvalidArgumentException when conversion to the given type was * not possible due to an error while converting */ @SuppressWarnings("unchecked") public <T extends Object> T getProperty(String propertyName, int type, Class<T> returnType) { if (returnType == null) { throw new IllegalArgumentException("ReturnType cannot be null"); } try { Object result = null; String stringValue = getProperty(propertyName, type); if (stringValue != null) { result = ConvertUtils.convert(stringValue, returnType); if ((result instanceof String) && (!returnType.equals(String.class))) { // If a string is returned, no converter has been found (for non-String return type) throw new IllegalArgumentException( "Unable to convert parameter to type: " + returnType.getName()); } } return (T) result; } catch (ConversionException ce) { // Conversion failed, wrap in Illegal throw new InvalidArgumentException("Query property value for '" + propertyName + "' should be a valid " + returnType.getSimpleName()); } }
From source file:org.alfresco.rest.workflow.api.impl.WorkflowRestImpl.java
/** * Get the first parameter value, converted to the requested type. * @param parameters used to extract parameter value from * @param parameterName name of the parameter * @param returnType type of object to return * @return the converted parameter value. Null, if the parameter has no value. * @throws IllegalArgumentException when no conversion for the given returnType is available or if returnType is null. * @throws InvalidArgumentException when conversion to the given type was not possible */// w w w. ja va 2s .c o m @SuppressWarnings("unchecked") public <T extends Object> T getParameter(Parameters parameters, String parameterName, Class<T> returnType) { if (returnType == null) { throw new IllegalArgumentException("ReturnType cannot be null"); } try { Object result = null; String stringValue = parameters.getParameter(parameterName); if (stringValue != null) { result = ConvertUtils.convert(stringValue, returnType); if (result instanceof String) { // If a string is returned, no converter has been found throw new IllegalArgumentException( "Unable to convert parameter to type: " + returnType.getName()); } } return (T) result; } catch (ConversionException ce) { // Conversion failed, wrap in Illegal throw new InvalidArgumentException( "Parameter value for '" + parameterName + "' should be a valid " + returnType.getSimpleName()); } }
From source file:org.aludratest.cloud.web.config.ConfigPreferences.java
@Override public Object put(String key, Object value) { if (value == null) { return localValues.put(key, null); }/* ww w.j a v a 2s .com*/ try { // try to auto-convert return localValues.put(key, (String) ConvertUtils.convert(value, String.class)); } catch (ConversionException e) { return localValues.put(key, value.toString()); } }
From source file:org.andromda.cartridges.database.DatabaseTemplateObject.java
/** * Instantiates an Object from the initial value of an attribute and returns * its value.//from ww w.ja va 2 s.co m * * @param attribute the attribute to retrieve the initial value from. * @return Object the initial value. */ public Object getInitialValue(AttributeFacade attribute) { Object initialValue = null; if (attribute != null) { final String methodName = "DatbaseTemplateObject.getInitialValue"; String exp = attribute.getDefaultValue(); if (StringUtils.isNotEmpty(exp)) { ClassifierFacade type = attribute.getType(); if (type != null) { String typeString = attribute.getType().getFullyQualifiedName(); if (StringUtils.isNotEmpty(typeString)) { try { Class typeClass = ClassUtils.loadClass(typeString); initialValue = StringUtils.trimToEmpty(exp); if (StringUtils.isNotEmpty(initialValue.toString())) { initialValue = ConvertUtils.convert(exp, typeClass); } } catch (Exception ex) { String errMsg = "Error performing " + methodName; logger.error(errMsg, ex); throw new MetafacadeException(errMsg, ex); } } } } } return initialValue; }