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:elaborate.tag_analysis.oosm.impl.gson.BaseInterfaceDeserializer.java
@Override public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { try {//w w w .j a v a 2s . c om T object = (T) this.createInstance(); JsonObject jsonObject = (JsonObject) je; Iterator<Entry<String, JsonElement>> it = jsonObject.entrySet().iterator(); //iterate through class fields while (it.hasNext()) { try { Entry<String, JsonElement> entry = it.next(); JsonElement jsonElement = entry.getValue(); Field field = getField(entry.getKey()); if (jsonElement.isJsonArray()) { Object arrayValue = this.processArrayElement(entry.getKey(), jsonElement.getAsJsonArray(), field, jdc); PropertyUtils.setProperty(object, entry.getKey(), arrayValue); } else { Object normalValue = this.processNormalElement(entry.getKey(), jsonElement, field, jdc); PropertyUtils.setProperty(object, entry.getKey(), normalValue); // if(field.getName().equals("type")){ // System.out.println(( (OOSMElement)object ).getType()); // } } } catch (Exception ex) { //System.out.println(object.getClass()); Logger.getLogger(BaseInterfaceDeserializer.class.getName()).log(Level.SEVERE, null, ex); } } return object; } catch (Exception ex) { Logger.getLogger(BaseInterfaceDeserializer.class.getName()).log(Level.SEVERE, null, ex); throw new JsonParseException(ex); } }
From source file:net.sourceforge.fenixedu.util.domain.SlotSelector.java
@Override public void setOrder(ObjectType target, Integer order) { try {//from w w w . j a v a 2 s .c o m PropertyUtils.setProperty(target, this.slotName, order); } catch (IllegalAccessException e) { throw new DomainException("adapter.ordered.relation.no.slot.access", e); } catch (InvocationTargetException e) { throw handleInvocationTargetException(e, "adapter.ordered.relation.invocation.exception"); } catch (NoSuchMethodException e) { throw new DomainException("adapter.ordered.relation.no.slot", e); } }
From source file:com.eviware.soapui.model.support.XPathReferenceImpl.java
public void update() { try {//from w w w.j a v a 2s . co m PropertyUtils.setProperty(target, xpathPropertyName, xpath); } catch (Exception e) { SoapUI.logError(e); } }
From source file:edu.northwestern.bioinformatics.studycalendar.domain.tools.hibernate.ScheduledActivityStateType.java
public void setPropertyValue(Object component, int property, Object value) throws HibernateException { String name = getPropertyNames()[property]; if (PropertyUtils.isWriteable(component, name)) { try {//from ww w . j av a2s . c o m PropertyUtils.setProperty(component, name, value); } catch (NoSuchMethodException e) { throw new HibernateException( "Failed to set property " + name + " on " + component + " with value " + value, e); } catch (IllegalAccessException e) { throw new HibernateException( "Failed to set property " + name + " on " + component + " with value " + value, e); } catch (InvocationTargetException e) { throw new HibernateException( "Failed to set property " + name + " on " + component + " with value " + value, e); } } }
From source file:net.cpollet.jixture.fixtures.generator.fixture.TemplateGenerator.java
/** * Returns the next generated entity.//from w w w. j av a 2 s . c om * * @return the next generated entity. * * @throws java.util.NoSuchElementException if the iteration has no more elements. */ @Override public Object next() { Object object = cloner.deepClone(templateObject); Map<String, Object> values = nextValues(); for (Map.Entry<String, Object> entry : values.entrySet()) { try { PropertyUtils.setProperty(object, entry.getKey(), entry.getValue()); } catch (Exception e) { throw new RuntimeException(e); } } return object; }
From source file:com.khubla.cbean.serializer.impl.DefaultClassHasher.java
/** * get a class as a hash//from ww w . ja v a 2s . com */ @Override public Hashtable<String, String> classToHash(Object o) throws SerializerException { try { /* * update the version */ final Field versionField = CBeanType.getCBeanType(clazz).getVersionField(); if (null != versionField) { Integer version = (Integer) PropertyUtils.getProperty(o, versionField.getName()); if (null == version) { version = new Integer(0); } version = version + 1; PropertyUtils.setProperty(o, versionField.getName(), version); } /* * walk the fields */ final Hashtable<String, String> ret = new Hashtable<String, String>(); final Field[] persistableFields = CBeanType.getCBeanType(clazz).getPersistableFields(); for (int i = 0; i < persistableFields.length; i++) { final Field field = persistableFields[i]; final Class<?> fieldClazz = field.getType(); final FieldSerializer serializer = fieldSerializerFactory.getFieldSerializer(field); final Property property = field.getAnnotation(Property.class); if (null != serializer) { final String value = serializer.serialize(o, field); if (null != value) { /* * only save the value if it's not null */ ret.put(field.getName(), value); } else { if (property.nullable() == false) { throw new CBeanException("Field '" + field.getName() + "' is not nullable"); } if (null != field.getAnnotation(Id.class)) { throw new CBeanException("Field '" + field.getName() + "' is an Id and cannot be null"); } } } else if (CBean.isEntity(fieldClazz)) { /* * recurse */ if (property.cascadeSave()) { final CBean<Object> cBean = CBeanServer.getInstance().getCBean(fieldClazz); final Object oo = PropertyUtils.getProperty(o, field.getName()); if ((null == oo) && (property.nullable() == false)) { throw new CBeanException("Field '" + field.getName() + "' is not nullable"); } cBean.save(oo); final String value = cBean.getId(oo); ret.put(field.getName(), value); } } else { throw new SerializerException("Unsupported serialization type '" + fieldClazz.getName() + "'"); } } return ret; } catch (final Exception e) { throw new SerializerException(e); } }
From source file:net.sf.json.util.DynaBeanToBeanMorpher.java
/** * DOCUMENT ME!//from w ww . ja v a 2 s . c om * * @param value DOCUMENT ME! * * @return DOCUMENT ME! */ public Object morph(Object value) { if (value == null) { return null; } if (!supports(value.getClass())) { throw new MorphException("value is not a DynaBean"); } Object bean = null; try { bean = beanClass.newInstance(); DynaBean dynaBean = (DynaBean) value; DynaClass dynaClass = dynaBean.getDynaClass(); PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(beanClass); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; String name = pd.getName(); DynaProperty dynaProperty = dynaClass.getDynaProperty(name); if (dynaProperty != null) { Class dynaType = dynaProperty.getType(); Class type = pd.getPropertyType(); if (type.isAssignableFrom(dynaType)) { PropertyUtils.setProperty(bean, name, dynaBean.get(name)); } else { if (IdentityObjectMorpher.getInstance() == morpherRegistry.getMorpherFor(type)) { throw new MorphException("Can't find a morpher for target class " + type); } else { PropertyUtils.setProperty(bean, name, morpherRegistry.morph(type, dynaBean.get(name))); } } } } } catch (InstantiationException e) { throw new MorphException(e); } catch (IllegalAccessException e) { throw new MorphException(e); } catch (InvocationTargetException e) { throw new MorphException(e); } catch (NoSuchMethodException e) { throw new MorphException(e); } return bean; }
From source file:com.tonbeller.wcf.expr.ExprUtils.java
public static void setModelReference(ExprContext context, String expr, Object value) { if (expr == null) throw new NullPointerException("expr is null"); // plain "bean" expr? if (!isExpression(expr)) { context.setBean(expr, value);//from w w w .j av a2 s . com return; } if (!expr.endsWith("}")) throw new IllegalArgumentException("expr must end with '}'"); // "#{bean}" expr? if (expr.indexOf('.') < 0) { // no bean, set session attribute String name = expr.substring(2, expr.length() - 1); context.setBean(name, value); return; } // "#{bean.property.path}" expr try { int pos = expr.indexOf('.'); String name = expr.substring(2, pos); Object bean = context.findBean(name); if (bean == null) throw new IllegalArgumentException("bean \"" + name + "\" not found"); String path = expr.substring(pos + 1, expr.length() - 1); PropertyUtils.setProperty(bean, path, value); } catch (IllegalAccessException e) { logger.error("exception caught", e); throw new SoftException(e); } catch (InvocationTargetException e) { logger.error("exception caught", e); throw new SoftException(e); } catch (NoSuchMethodException e) { logger.error("exception caught", e); throw new SoftException(e); } }
From source file:com.alibaba.stonelab.toolkit.sqltester.BeanInitBuilder.java
/** * <pre>/* w w w . j a va2s.com*/ * build logic * TODO: * 1.?, * 2.JDK Enum? * * </pre> * * @param count * @return */ private List<T> doBuild(int count) { List<T> ret = new ArrayList<T>(); for (int i = 0; i < count; i++) { T t = null; try { t = clz.newInstance(); } catch (Exception e) { throw new BuildException("new instance error.", e); } PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clz); for (PropertyDescriptor des : descriptors) { try { // ? if (Number.class.isAssignableFrom(des.getPropertyType())) { PropertyUtils.setProperty(t, des.getName(), DEFAULT_VALUE_NUMBER.get(des.getPropertyType())); } // String? if (String.class.isAssignableFrom(des.getPropertyType())) { if (count == 1) { PropertyUtils.setProperty(t, des.getName(), des.getName()); } else { PropertyUtils.setProperty(t, des.getName(), des.getName() + i); } } // date? if (Date.class.isAssignableFrom(des.getPropertyType())) { PropertyUtils.setProperty(t, des.getName(), DEFAULT_VALUE_DATE); } // JDK? if (Enum.class.isAssignableFrom(des.getPropertyType())) { if (des.getPropertyType().getEnumConstants().length >= 1) { PropertyUtils.setProperty(t, des.getName(), des.getPropertyType().getEnumConstants()[0]); } } } catch (Exception e) { throw new BuildException("build error.", e); } } // ret.add(t); } return ret; }
From source file:com.hihframework.core.utils.CollectionUtils.java
/** * ?ID?,???.//from www . j a v a 2 s . com * http?????idhibernate??????? * ? * ???ID?,ID?ID??. * * @param collection * ?? * @param checkedIds * ? * @param idName * ID?? * @param clazz * ? */ public static <T, ID> void mergeByCheckedIds(Collection<T> collection, Collection<ID> checkedIds, String idName, Class<T> clazz) throws Exception { if (checkedIds == null) { collection.clear(); return; } Iterator<T> it = collection.iterator(); while (it.hasNext()) { T obj = it.next(); if (checkedIds.contains(PropertyUtils.getProperty(obj, idName))) { checkedIds.remove(PropertyUtils.getProperty(obj, idName)); } else { it.remove(); } } for (ID id : checkedIds) { T obj = clazz.newInstance(); PropertyUtils.setProperty(obj, idName, id); collection.add(obj); } }