List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:com.wavemaker.json.AlternateJSONTransformer.java
private static Object toObjectInternal(JSONState jsonState, JSONObject obj, Object root, FieldDefinition fieldDefinition, TypeState typeState, int arrayLevel, Stack<String> setterQueue) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {/* w w w .j a va2 s . com*/ if (fieldDefinition == null) { throw new NullArgumentException("fieldDefinition"); } else if (fieldDefinition.getTypeDefinition() == null) { throw new WMRuntimeException(MessageResource.JSON_TYPEDEF_REQUIRED); } else if (!(fieldDefinition.getTypeDefinition() instanceof ObjectTypeDefinition)) { throw new WMRuntimeException(MessageResource.JSON_OBJECTTYPEDEF_REQUIRED, fieldDefinition.getTypeDefinition(), fieldDefinition.getTypeDefinition().getClass()); } ObjectTypeDefinition otd = (ObjectTypeDefinition) fieldDefinition.getTypeDefinition(); Object instance = otd.newInstance(); for (Object entryO : obj.entrySet()) { Entry<?, ?> entry = (Entry<?, ?>) entryO; String key = (String) entry.getKey(); FieldDefinition nestedFieldDefinition = otd.getFields().get(key); if (nestedFieldDefinition == null) { throw new WMRuntimeException(MessageResource.JSON_NO_PROP_MATCHES_KEY, key, fieldDefinition); } if (!PropertyUtils.isWriteable(instance, key)) { logger.warn(MessageResource.JSON_NO_WRITE_METHOD.getMessage(fieldDefinition, key)); continue; } // setter list support setterQueue.push(key); jsonState.getSettersCalled().add(getPropertyFromQueue(setterQueue)); Object paramValue = toObjectInternal(jsonState, entry.getValue(), root, nestedFieldDefinition, typeState, 0, setterQueue); PropertyUtils.setProperty(instance, key, paramValue); // end setter list support setterQueue.pop(); } return instance; }
From source file:com.liferay.wsrp.proxy.TypeConvertorUtil.java
private static void _convert(int sourceVersion, String sourcePackage, Object sourceClass, Object sourceChild, String sourceChildName, Object destination) throws Exception { Class<?> sourceChildClass = sourceChild.getClass(); if (sourceChildClass == NavigationalContext.class) { sourceChildName = "navigationalState"; NavigationalContext navigationalContext = (NavigationalContext) sourceChild; sourceChild = navigationalContext.getOpaqueValue(); } else if (sourceChildClass == SessionParams.class) { sourceChildName = "sessionID"; SessionParams sessionParams = (SessionParams) sourceChild; sourceChild = sessionParams.getSessionID(); }//from w w w. ja va2s . c o m if (sourceChild == null) { return; } sourceChildClass = sourceChild.getClass(); Object destinationChild = null; if (sourceChildClass.isArray()) { destinationChild = convert(sourceChild, sourceVersion); } else { destinationChild = sourceChild; sourceChildClass = sourceChild.getClass(); if (sourceChildClass.getName().contains(sourcePackage)) { destinationChild = convert(sourceChild, sourceVersion); } } String destinationChildName = sourceChildName; if (sourceChildName.equals("itemBinary")) { destinationChildName = "markupBinary"; } else if (sourceChildName.equals("itemString")) { destinationChildName = "markupString"; } else if (sourceChildName.equals("markupBinary")) { destinationChildName = "itemBinary"; } else if (sourceChildName.equals("markupString")) { destinationChildName = "itemString"; } else if (sourceChildName.equals("name") && (sourceClass == Property.class)) { QName qName = (QName) destinationChild; destinationChild = qName.getLocalPart(); } else if (sourceChildName.equals("name") && (sourceClass == PropertyDescription.class)) { String name = (String) destinationChild; destinationChild = new QName("namespace", name, "prefix"); } else if (sourceChildName.equals("navigationalState") && (sourceClass == MarkupParams.class)) { String navigationalState = (String) sourceChild; NavigationalContext navigationalContext = new NavigationalContext(); navigationalContext.setOpaqueValue(navigationalState); destinationChild = navigationalContext; destinationChildName = "navigationalContext"; } else if (sourceChildName.equals("requiresRewriting")) { destinationChildName = "requiresUrlRewriting"; } else if (sourceChildName.equals("requiresUrlRewriting")) { destinationChildName = "requiresRewriting"; } else if (sourceChildName.equals("sessionID") && (sourceClass == RuntimeContext.class)) { String sessionID = (String) sourceChild; SessionParams sessionParams = new SessionParams(); sessionParams.setSessionID(sessionID); destinationChild = sessionParams; destinationChildName = "sessionParams"; } try { PropertyUtils.setProperty(destination, destinationChildName, destinationChild); } catch (NoSuchMethodException nsme) { if (_log.isWarnEnabled()) { _log.warn(nsme, nsme); } } }
From source file:it.sample.parser.util.CommonsUtil.java
/** * Metodo di utilita' per rendere null un'istanza di una classe che e' istanziata ma che ha tutti i campi null * /* ww w .j av a2 s . co m*/ * @param instance */ public static <T> void sanitizeObject(T instance) { PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(instance); if (descriptors != null) { for (PropertyDescriptor descriptor : descriptors) { try { String propertyName = descriptor.getName(); if (descriptor.getReadMethod() != null) { Object val = PropertyUtils.getProperty(instance, propertyName); if (val != null && !BeanUtils.isSimpleProperty(val.getClass()) && descriptor.getWriteMethod() != null && !isFilled(val) && !val.getClass().getName().startsWith("java.util")) { PropertyUtils.setProperty(instance, propertyName, null); } } } catch (Exception e) { e.printStackTrace(); } } } }
From source file:com.utest.webservice.builders.Builder.java
protected void populateLocators(Ti result, UriBuilder ub) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Map<?, ?> resultProperties = PropertyUtils.describe(result); for (Object property : resultProperties.keySet()) { if (((String) property).endsWith("Locator")) { String resourcePath = null; Integer resourceId = null; String resourceName = ((String) property).substring(0, ((String) property).indexOf("Locator")); if (resultProperties.containsKey(resourceName + "Id")) { resourceId = (Integer) PropertyUtils.getProperty(result, resourceName + "Id"); if (resourceId != null) { resourcePath = getResourcePath(resourceName.toLowerCase()); }/*from www . j a v a2s.c o m*/ } // represents a user who performed an operation else if (resourceName.endsWith("By")) { resourceId = (Integer) PropertyUtils.getProperty(result, resourceName); if (resourceId != null) { resourcePath = getResourcePath("user"); } } if (resourcePath != null) { String className = getResourceNamedClass(resourceName.toLowerCase()); String name = ""; if (className != null) { try { Class<?> clazz = Class.forName(className); Object namedEntity = factory.getStaticDataService().getEntity(clazz, resourceId); if (namedEntity != null && namedEntity instanceof Named) { name = ((Named) namedEntity).getName(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ResourceLocator resourceLocator = new ResourceLocator(resourceId, ub.clone().path(resourcePath).build(resourceId).toString(), name); PropertyUtils.setProperty(result, (String) property, resourceLocator); } } } }
From source file:com.lioland.harmony.web.dao.ODBClass.java
private static void fillObject(ODocument doc, Object ob) throws NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InvocationTargetException { ODBClass odbc = (ODBClass) ob;//from ww w . ja v a2 s. c o m odbc.setRid(doc.getIdentity().toString()); for (String fieldName : doc.fieldNames()) { Object fieldValue = doc.field(fieldName); System.out.println("fieldName: " + fieldName); if (fieldValue instanceof ODocument) { System.out.println("ODocument found"); fieldValue = transform((ODocument) fieldValue, Class.forName(PACKAGE_PREFIX + ((ODocument) fieldValue).getClassName())); } else if (fieldValue instanceof List) { List<ODocument> list = (List) fieldValue; List newList = new ArrayList(); for (Object item : list) { if (item instanceof ODocument) { ODocument odoc = (ODocument) item; newList.add(transform(odoc, Class.forName(PACKAGE_PREFIX + odoc.getClassName()))); } else { newList.add(item); } } fieldValue = newList; } if (fieldValue != null) { PropertyUtils.setProperty(ob, fieldName, fieldValue); } } }
From source file:net.kamhon.ieagle.util.ReflectionUtil.java
public static void copyPropertiesWithPropertiesList(Object source, Object target, String... properties) { if (source == null) throw new SystemErrorException("source is null"); if (target == null) throw new SystemErrorException("target is null"); if (properties == null) { copyProperties(source, target);/*from ww w . j av a 2s . co m*/ } else { for (String property : properties) { Object value; try { value = PropertyUtils.getProperty(source, property); } catch (Exception e) { throw new SystemErrorException("no property[" + property + "] for source[" + source + "]"); } try { PropertyUtils.setProperty(target, property, value); } catch (Exception e) { throw new SystemErrorException("no property[" + property + "] for target[" + target + "]"); } } } }
From source file:com.krawler.br.spring.RConverterImpl.java
private Object getValue(Object obj, ModuleDefinition md) throws ProcessException { Object retobj = obj;/*w w w.j a va 2s . c o m*/ switch (md.getType()) { case JSON: Iterator itr = md.getPropertyNames().iterator(); JSONObject jobj; try { jobj = obj instanceof String ? new JSONObject((String) obj) : (JSONObject) obj; while (itr.hasNext()) { String pname = (String) itr.next(); ModuleProperty mp = md.getProperty(pname); Object temp = jobj.opt(pname); if (temp == null) { continue; } List l = new ArrayList(); if (mp.isMulti()) { JSONArray jArr = (JSONArray) temp; for (int i = 0; i < jArr.length(); i++) { l.add(getValue(jArr.get(i), mb.getModuleDefinition(md.getProperty(pname).getType()))); } } else { l.add(getValue(temp, mb.getModuleDefinition(md.getProperty(pname).getType()))); } jobj.put(pname, convertToMultiType(l, mp.getMulti(), mp.getType())); } if (obj instanceof String) { retobj = jobj; } } catch (JSONException ex) { throw new ProcessException("can't convert from string to json", ex); } break; case MAP: itr = md.getPropertyNames().iterator(); Map map = new HashMap(); try { jobj = obj instanceof String ? new JSONObject((String) obj) : (JSONObject) obj; while (itr.hasNext()) { String pname = (String) itr.next(); ModuleProperty mp = md.getProperty(pname); Object temp = jobj.opt(pname); if (temp == null) { continue; } List l = new ArrayList(); if (mp.isMulti()) { JSONArray jArr = (JSONArray) temp; for (int i = 0; i < jArr.length(); i++) { l.add(getValue(jArr.get(i), mb.getModuleDefinition(md.getProperty(pname).getType()))); } } else { l.add(getValue(temp, mb.getModuleDefinition(md.getProperty(pname).getType()))); } map.put(pname, convertToMultiType(l, mp.getMulti(), mp.getType())); } retobj = map; } catch (JSONException ex) { throw new ProcessException("can't convert from string to json", ex); } break; case SIMPLE: if (obj instanceof String) { retobj = getSimpleValue(md.getClassName(), (String) obj); } break; case POJO: itr = md.getPropertyNames().iterator(); try { Object pojo = Class.forName(md.getClassName()).newInstance(); jobj = obj instanceof String ? new JSONObject((String) obj) : (JSONObject) obj; while (itr.hasNext()) { String pname = (String) itr.next(); ModuleProperty mp = md.getProperty(pname); Object temp = jobj.opt(pname); if (temp == null) { continue; } List l = new ArrayList(); if (mp.isMulti()) { JSONArray jArr = (JSONArray) temp; for (int i = 0; i < jArr.length(); i++) { l.add(getValue(jArr.get(i), mb.getModuleDefinition(md.getProperty(pname).getType()))); } } else { l.add(getValue(temp, mb.getModuleDefinition(md.getProperty(pname).getType()))); } try { PropertyUtils.setProperty(pojo, pname, convertToMultiType(l, mp.getMulti(), mp.getType())); } catch (IllegalAccessException ex) { throw new ProcessException("property not accessible : " + md.getName() + "." + pname); } catch (InvocationTargetException ex) { throw new ProcessException("property not available : " + md.getName() + "." + pname); } catch (NoSuchMethodException ex) { throw new ProcessException("property not available : " + md.getName() + "." + pname); } } retobj = pojo; } catch (InstantiationException ex) { throw new ProcessException("module not available : " + md.getName()); } catch (IllegalAccessException ex) { throw new ProcessException("module not available : " + md.getName()); } catch (ClassNotFoundException ex) { throw new ProcessException("module not available : " + md.getName()); } catch (JSONException ex) { throw new ProcessException("can't convert from string to json", ex); } break; default: throw new ProcessException("conversion from string to " + md.getType() + " not supported"); } return retobj; }
From source file:com.krawler.br.exp.Variable.java
private void setProperty(Object container, String prop, Object val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ProcessException { if (container == null) { throw new ProcessException("container not found for variable: " + this); }/*from w w w .jav a2 s. com*/ if (container instanceof java.util.Map) { ((java.util.Map) container).put(prop, val); } else if (container instanceof JSONObject) { try { ((JSONObject) container).put(prop, val); } catch (JSONException ex) { throw new ProcessException(ex); } } else { PropertyUtils.setProperty(container, prop, val); } }
From source file:com.expressui.core.view.field.SelectField.java
/** * Listener method invoked when user clicks clear button. *///from w ww . ja v a 2 s. c o m public void itemCleared() { T bean = typedForm.getBean(); try { PropertyUtils.setProperty(bean, propertyId, null); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } Property property = field.getPropertyDataSource(); field.setPropertyDataSource(property); requestRepaintAll(); }
From source file:jp.co.acroquest.endosnipe.report.converter.compressor.SamplingCompressor.java
/** * ??????????//from ww w . j a va2s .com * ??????????????? * * @param rawSamples * @param targetFields ? * @return ?? * @throws IllegalAccessException ????????? * @throws InvocationTargetException ?????? * @throws NoSuchMethodException ?????? * @throws InstantiationException * @throws NoSuchFieldException * @throws SecurityException */ private Object createCompressedSample(List rawSamples, long startTime, long endTime, String measureTimeField, List<CompressOperation> operation, Class clazz) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, SecurityException, NoSuchFieldException { Object returnSample; if (rawSamples.size() < 1) { returnSample = clazz.newInstance(); } else { returnSample = rawSamples.get(0); } BeanUtils.setProperty(returnSample, measureTimeField, new Timestamp(startTime)); for (CompressOperation ope : operation) { Object maxObj = getMaxValueFromSampleList(rawSamples, ope.getCompressField(), clazz); Object minObj = getMinValueFromSampleList(rawSamples, ope.getCompressField(), clazz); Object compressedObj = getCompressedValue(rawSamples, ope, clazz, endTime - startTime); PropertyUtils.setProperty(returnSample, ope.getCompressField() + MAX_VALUE_PROPERTY_SUFFIX_, maxObj); PropertyUtils.setProperty(returnSample, ope.getCompressField() + MIN_VALUE_PROPERTY_SUFFIX_, minObj); PropertyUtils.setProperty(returnSample, ope.getCompressField(), compressedObj); } return returnSample; }