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:de.micromata.genome.util.types.Converter.java
/** * Copy convert int./*from w ww.j a v a 2 s .com*/ * * @param source the source * @param sourceName the source name * @param target the target * @param targetName the target name * @return true, if successful */ public static boolean copyConvertInt(Object source, String sourceName, Object target, String targetName) { try { Object so = PropertyUtils.getProperty(source, sourceName); Integer bd = null; if (so instanceof Integer) { bd = (Integer) so; } else { String s = (String) so; bd = Integer.parseInt(s); } PropertyUtils.setProperty(target, targetName, bd); return true; } catch (Throwable ex) { // NOSONAR "Illegal Catch" framework return false; } }
From source file:io.milton.http.annotated.AbstractAnnotationHandler.java
/** * Returns true if it was able to set the property * * @param source//from w ww.j av a 2 s . co m * @param value * @param propNames * @return * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException */ protected boolean attemptToSetProperty(Object source, Object value, String... propNames) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { for (String propName : propNames) { if (PropertyUtils.isWriteable(source, propName)) { PropertyUtils.setProperty(source, propName, value); return true; } } return false; }
From source file:com.sunchenbin.store.feilong.core.bean.PropertyUtil.java
/** * {@link PropertyUtils#setProperty(Object, String, Object)} ?(<b>??</b>). * * @param bean//from w w w .ja v a 2 s. c om * Bean whose property is to be modified * @param propertyName * Possibly indexed and/or nested name of the property to be modified * @param value * Value to which this property is to be set * @see org.apache.commons.beanutils.BeanUtils#setProperty(Object, String, Object) * @see org.apache.commons.beanutils.PropertyUtils#setProperty(Object, String, Object) * @see com.sunchenbin.store.feilong.core.bean.BeanUtil#setProperty(Object, String, Object) */ public static void setProperty(Object bean, String propertyName, Object value) { try { //Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions. // PropertyUtilsBeanUtils,????? PropertyUtils.setProperty(bean, propertyName, value); } catch (Exception e) { LOGGER.error(e.getClass().getName(), e); throw new BeanUtilException(e); } }
From source file:corner.orm.tapestry.component.gain.GainPoint.java
/** * ?entity/* w ww . ja v a2 s . co m*/ */ private <T> void entityWorkshop() { Object entity = null; int Size = this.getForegroundLength(); Class entityClass = null; try { entityClass = Class.forName(getEntityClass()); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } neatenPersistentEntity(); //?? Object temp = null; //?? Iterator FEList = null; String epname = null; for (int i = 0; i < Size; i++) { if (i < this.getPersistentSize()) { entity = this.getEntitys().get(i); } else { try { entity = entityClass.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } FEList = this.getForegroundEntitys().iterator(); for (int j = 0; j < this.getForegroundEntitys().size(); j++) { List foregroundEntity = (List) FEList.next(); try { temp = foregroundEntity.get(i); if (temp != null && !temp.equals("")) { epname = this.getEntityPropertys().get(j); if (getPagePersistentId().equals(epname)) { //??id????id epname = getPersistentId(); } PropertyUtils.setProperty(entity, epname, temp); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } this.getSaveOrUpdateEntitys().add(entity); } // delNullEntity(); }
From source file:com.khubla.cbean.serializer.impl.DefaultClassHasher.java
/** * populate class from hash/*from w w w .j a va 2s . c o m*/ */ @Override public Object hashToClass(Hashtable<String, String> hashtable) throws SerializerException { try { /* * get a proxied class to use */ // final Object ret = getProxiedInstance(clazz); final Object ret = getInstance(clazz); /* * get the fields */ 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 String fieldValue = hashtable.get(field.getName()); if (null != serializer) { serializer.deserialize(ret, field, fieldValue); } else if (CBean.isEntity(fieldClazz)) { /* * recurse */ final Property property = field.getAnnotation(Property.class); if (property.cascadeLoad()) { final CBean<Object> cBean = CBeanServer.getInstance().getCBean(fieldClazz); /* * this load may come back as a null. If it does, the contained object referred to has been deleted. This is ok, just set the value to null. */ final Object o = cBean.load(new CBeanKey(fieldValue)); PropertyUtils.setProperty(ret, field.getName(), o); } else { final Object o = getProxiedInstance(fieldClazz, fieldValue); PropertyUtils.setProperty(ret, field.getName(), o); } } else { throw new SerializerException("Unsupported serialization type '" + fieldClazz.getName() + "'"); } } return ret; } catch (final Exception e) { throw new SerializerException(e); } }
From source file:com.esofthead.mycollab.module.crm.ui.components.RelatedEditItemField.java
@Override public void fireValueChange(Object data) { try {/*w ww . j a va2s . c om*/ if (data instanceof SimpleAccount) { PropertyUtils.setProperty(bean, "typeid", ((SimpleAccount) data).getId()); itemField.setValue(((SimpleAccount) data).getAccountname()); } else if (data instanceof SimpleCampaign) { PropertyUtils.setProperty(bean, "typeid", ((SimpleCampaign) data).getId()); itemField.setValue(((SimpleCampaign) data).getCampaignname()); } else if (data instanceof SimpleContact) { PropertyUtils.setProperty(bean, "typeid", ((SimpleContact) data).getId()); itemField.setValue(((SimpleContact) data).getContactName()); } else if (data instanceof SimpleLead) { PropertyUtils.setProperty(bean, "typeid", ((SimpleLead) data).getId()); itemField.setValue(((SimpleLead) data).getLeadName()); } else if (data instanceof SimpleOpportunity) { PropertyUtils.setProperty(bean, "typeid", ((SimpleOpportunity) data).getId()); itemField.setValue(((SimpleOpportunity) data).getOpportunityname()); } else if (data instanceof SimpleCase) { PropertyUtils.setProperty(bean, "typeid", ((SimpleCase) data).getId()); itemField.setValue(((SimpleCase) data).getSubject()); } } catch (Exception e) { LOG.error("Error when fire value", e); } }
From source file:com.timtripcony.AbstractSmartDocumentModel.java
@Override public void setValue(final Object key, final Object value) { try {/*w w w. j a v a 2 s .c om*/ PropertyUtils.setProperty(this, key.toString(), value); } catch (Throwable t) { super.setValue(key, value); } }
From source file:hermes.browser.dialog.EditNamingConfigDialog.java
@SuppressWarnings("unchecked") public void doSelectionChanged() { try {//from ww w .j ava 2s. c om final String selectedConfig = (String) comboBox.getSelectedItem(); final NamingConfig config = (NamingConfig) namingConfigsByName.get(selectedConfig); final PropertySetConfig propertySet = config.getProperties(); if (currentSelection == null || !currentSelection.equals(selectedConfig)) { currentSelection = selectedConfig; bean = new JNDIContextFactory(); LoaderSupport.populateBean(bean, propertySet); final Map properties = PropertyUtils.describe(bean); final List list = new ArrayList(); classpathIdProperty = new Property("loader", "Classpath Loader to use.", String.class) { /** * */ private static final long serialVersionUID = -3071689960943636606L; private String classpathId = config.getClasspathId(); public void setValue(Object value) { classpathId = value.toString(); } public Object getValue() { return classpathId; } public boolean hasValue() { return true; } }; classpathIdProperty.setEditorContext(ClasspathIdCellEdtitor.CONTEXT); list.add(classpathIdProperty); for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) { final Map.Entry entry = (Map.Entry) iter.next(); final String propertyName = (String) entry.getKey(); final Object propertyValue = entry.getValue() != null ? entry.getValue() : ""; if (!propertyName.equals("class") && !propertyName.equals("name")) { Property displayProperty = new Property(propertyName, propertyName, PropertyUtils.getPropertyType(bean, propertyName)) { /** * */ private static final long serialVersionUID = 1751773758147906036L; public void setValue(Object value) { try { PropertyUtils.setProperty(bean, propertyName, value); } catch (Exception e) { log.error(e.getMessage(), e); } } public Object getValue() { try { return PropertyUtils.getProperty(bean, propertyName); } catch (Exception e) { log.error(e.getMessage(), e); } return null; } public boolean hasValue() { return true; } }; list.add(displayProperty); } } final PropertyTableModel model = new PropertyTableModel(list); final PropertyTable table = new PropertyTable(model); table.setAutoResizeMode(PropertyTable.AUTO_RESIZE_ALL_COLUMNS); PropertyPane pane = new PropertyPane(table); pane.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { } }); model.expandAll(); scrollPane.setViewportView(pane); } } catch (Exception e) { log.error(e.getMessage(), e); HermesBrowser.getBrowser().showErrorDialog(e); } }
From source file:com.blackbear.flatworm.FileCreator.java
/** * Write information to the output file. Make sure you have called the * setBean() method with the needed beans before calling this method.<br> * /*from www .ja va 2s . c o m*/ * @param recordName * The name specified in your flatworm configuration file for this * record * @throws IOException * - If the file system has a problem with you writing information * to the recently opened file. * @throws FlatwormCreationException * - wraps varius Exceptions so client doesn't have to handle too * many */ public void write(String recordName) throws IOException, FlatwormCreatorException { Record record = ff.getRecord(recordName); RecordDefinition recDef = record.getRecordDefinition(); List<Line> lines = recDef.getLines(); // Iterate over lines boolean first = true; for (Iterator<Line> itLines = lines.iterator(); itLines.hasNext();) { Line line = itLines.next(); String delimit = line.getDelimeter(); if (null == delimit) delimit = ""; // record-ident contain what is considered hard-coded data // for the output line, these can be used to uniquely identify // lines for parsers. We need to write them out. // For multiline records they should only be written for the first line - // Dave Derry 11/2009 List<String> recIdents = record.getFieldIdentMatchStrings(); if (first) { for (Iterator<String> itRecIdents = recIdents.iterator(); itRecIdents.hasNext();) { String id = itRecIdents.next(); bufOut.write(id + delimit); } } // Iterate over record-element items List<LineElement> recElements = line.getElements(); for (Iterator<LineElement> itRecElements = recElements.iterator(); itRecElements.hasNext();) { LineElement lineElement = itRecElements.next(); if (lineElement instanceof RecordElement) { RecordElement recElement = (RecordElement) lineElement; Map<String, ConversionOption> convOptions = recElement.getConversionOptions(); int length = 0; String beanRef = ""; String type = ""; try { beanRef = recElement.getBeanRef(); type = recElement.getType(); length = recElement.getFieldLength(); } catch (FlatwormUnsetFieldValueException ex) { throw new FlatwormCreatorException( "Could not deduce field length (please provide more data in your xml file for : " + beanRef + " " + ex.getMessage()); } String val = ""; ConversionHelper convHelper = ff.getConvertionHelper(); try { if (beanRef != null) { // Extract property name Object bean = null; String property = ""; try { int posOfFirstDot = beanRef.indexOf('.'); bean = beans.get(beanRef.substring(0, posOfFirstDot)); property = beanRef.substring(posOfFirstDot + 1); } catch (ArrayIndexOutOfBoundsException ex) { throw new FlatwormCreatorException("Had trouble parsing : " + beanRef + " Its format should be <bean_name>.<property_name>"); } // Convert to String for output Object value = PropertyUtils.getProperty(bean, property); val = convHelper.convert(type, value, convOptions, beanRef); PropertyUtils.setProperty(bean, property, value); } // end beanRef != null // Handle any conversions that need to occur if (val == null) { val = ""; } val = convHelper.transformString(val, recElement.getConversionOptions(), recElement.getFieldLength()); if (itRecElements.hasNext()) bufOut.write(val + delimit); else bufOut.write(val); } catch (Exception ex) { throw new FlatwormCreatorException("Exception getting/converting bean property : " + beanRef + " : " + ex.getMessage()); } } } // end for all record elements if (null != recordSeperator) bufOut.write(recordSeperator); first = false; } // end for all lines }
From source file:com.bstek.dorado.idesupport.RuleSetBuilder.java
private void applyProperty(Object source, Object target, String propertyName) throws Exception { Object value = PropertyUtils.getProperty(source, propertyName); if (value != null) { PropertyUtils.setProperty(target, propertyName, value); }/*from w w w . ja v a 2 s .c om*/ }