List of usage examples for org.apache.commons.beanutils PropertyUtils getSimpleProperty
public static Object getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return the value of the specified simple property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:com.discursive.jccook.bean.NestedPropertyExample.java
private void start() { // Create an author Author wolfe = new Author("Tom Wolfe", "Green"); Book theRightStuff = new Book("The Right Stuff", wolfe); try {//from w w w.j ava 2s .com String bookName = (String) PropertyUtils.getSimpleProperty(theRightStuff, "name"); String authorName = (String) PropertyUtils.getNestedProperty(theRightStuff, "author.name"); System.out.println("The book is " + bookName); System.out.println("The author is " + authorName); } catch (Exception e) { System.out.println("There was a problem getting a bean property."); e.printStackTrace(); } }
From source file:de.cbb.mplayer.mapping.MappingUtil.java
private static void mapFieldToPresenter(Field field, Object entity, Object presenter) { if (MappingFactory.EXCLUSIONS.contains(field.getName())) { return;/* w w w . j a va2s .c o m*/ } try { Object value1 = PropertyUtils.getSimpleProperty(entity, field.getName()); String fieldname = field.getName(); MappingFactory.buildMapper(presenter, fieldname, value1).map(value1); } catch (Exception ex) { log.debug("Unmapped attribute: " + field.getName() + " [" + ex.toString() + "]"); } }
From source file:net.minder.config.impl.BeanConfigurationAdapter.java
@Override public String getConfigurationValue(String name) throws ConfigurationException { try {/*ww w . j a v a 2 s .com*/ Object obj = PropertyUtils.getSimpleProperty(bean, name); String str = obj != null ? obj.toString() : null; return str; } catch (Exception e) { throw new ConfigurationException(String.format(""), e); } }
From source file:com.eviware.soapui.support.ModelItemPropertyEditorModel.java
public String getEditorText() { try {//from ww w .ja v a2s . c o m Object value = PropertyUtils.getSimpleProperty(modelItem, propertyName); return value == null ? "" : String.valueOf(value); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.rolex.explore.beanutils.service.BeanUtilsSpecificService.java
public void exploreBeanUtil() { SampleBean bean = new SampleBean(); String property1 = "name"; String property2 = "currentAddress.city"; String property3 = "previousAddresses[0].city"; String property4 = "previousAddresses[3].city"; String property5 = "vehicleLicenseModel(R60)"; Place place1 = new Place("Sentosa", "Singapore"); Place place2 = new Place("Colombo", "Sri Lanka"); List<Place> places = new ArrayList<Place>(); places.add(place1);/*from w ww . j a v a 2 s . c o m*/ places.add(place2); String property6 = "yearlyPlacesVisited(2000)"; String property7 = "placesVisited"; String property8 = "placesVisited[0]"; TourismAward award = new TourismAward("World Award Committee", "USA"); String property9 = "yearlyPlacesVisited(2000)[0].tourismAwards[0]"; try { PropertyUtils.setSimpleProperty(bean, property1, "Rolex Rolex"); String value1 = (String) PropertyUtils.getSimpleProperty(bean, property1); System.out.println("###Reverse1: " + value1); PropertyUtils.setNestedProperty(bean, property2, "Hoffman Estates"); String value2 = (String) PropertyUtils.getNestedProperty(bean, property2); System.out.println("###Reverse2: " + value2); PropertyUtils.setNestedProperty(bean, property3, "Schaumburg"); String value3 = (String) PropertyUtils.getNestedProperty(bean, property3); System.out.println("###Reverse3: " + value3); PropertyUtils.setNestedProperty(bean, property4, "Des Plaines"); String value4 = (String) PropertyUtils.getNestedProperty(bean, property4); System.out.println("###Reverse4: " + value4); Address[] arrayValue1 = (Address[]) PropertyUtils.getSimpleProperty(bean, "previousAddresses"); System.out.println("###ReverseArray: " + Arrays.toString(arrayValue1)); PropertyUtils.setMappedProperty(bean, property5, "Sonata"); String value5 = (String) PropertyUtils.getMappedProperty(bean, property5); System.out.println("###Reverse5: " + value5); PropertyUtils.setMappedProperty(bean, property6, places); List<Place> value6 = (List<Place>) PropertyUtils.getMappedProperty(bean, property6); System.out.println("###Reverse6: " + value6.get(0)); PropertyUtils.setSimpleProperty(bean, property7, places); List<Place> value7 = (List<Place>) PropertyUtils.getSimpleProperty(bean, property7); System.out.println("###Reverse7: " + value7.get(0)); PropertyUtils.setIndexedProperty(bean, property8, place2); Place value8 = (Place) PropertyUtils.getIndexedProperty(bean, property8); System.out.println("###Reverse8: " + value8); PropertyUtils.setNestedProperty(bean, property9, award); TourismAward value9 = (TourismAward) PropertyUtils.getNestedProperty(bean, property9); System.out.println("###Reverse8: " + value8); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.qrmedia.commons.persistence.hibernate.clone.property.AbstractValueAwarePropertyCloner.java
public boolean clone(Object source, Object target, String propertyName, HibernateEntityGraphCloner entityGraphCloner) throws IllegalArgumentException { try {/* ww w. j a v a2 s . c o m*/ return cloneValue(source, target, propertyName, PropertyUtils.getSimpleProperty(source, propertyName), entityGraphCloner); } catch (Exception exception) { throw new IllegalArgumentException( "Unable to copy property '" + propertyName + "' from " + source + " to " + target + " due to " + exception.getClass().getSimpleName() + ": " + exception.getMessage()); } }
From source file:com.bstek.dorado.data.entity.PropertyPathUtils.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static void setValueByPath(EntityDataType dataType, Object object, String propertyPath, Object value) throws Exception { String[] paths = StringUtils.split(propertyPath, '.'); Object currentEntity = object; EntityDataType currentDataType = dataType; for (int i = 0; i < paths.length - 1; i++) { String path = paths[i];//from w w w .j a va 2s . co m Object tempEntity; boolean isMap = currentEntity instanceof Map; if (EntityUtils.isEntity(currentEntity)) { tempEntity = EntityUtils.getValue(currentEntity, path); } else if (currentEntity instanceof Map) { tempEntity = ((Map) currentEntity).get(path); } else { tempEntity = PropertyUtils.getSimpleProperty(currentEntity, path); } if (tempEntity == null) { Class<?> subEntityType = null; if (currentDataType != null) { PropertyDef propertyDef = currentDataType.getPropertyDef(path); if (propertyDef != null) { DataType propertyDataType = propertyDef.getDataType(); if (propertyDataType instanceof EntityDataType) { currentDataType = (EntityDataType) propertyDataType; subEntityType = currentDataType.getCreationType(); if (subEntityType == null) { subEntityType = currentDataType.getMatchType(); } } } } else if (isMap) { tempEntity = new HashMap(); currentDataType = null; } if (tempEntity == null) { if (subEntityType == null) { subEntityType = PropertyUtils.getPropertyType(currentEntity, path); } if (subEntityType.isAssignableFrom(Map.class)) { tempEntity = new HashMap(); } else if (!subEntityType.isInterface()) { tempEntity = subEntityType.newInstance(); } currentDataType = null; } if (tempEntity != null) { if (isMap) { ((Map) currentEntity).put(path, tempEntity); } else { PropertyUtils.setSimpleProperty(currentEntity, path, tempEntity); } } else { throw new IllegalArgumentException("Can not write value to [" + StringUtils.join(paths, '.') + "] on [" + ObjectUtils.identityToString(object) + "]."); } } currentEntity = tempEntity; } String path = paths[paths.length - 1]; if (EntityUtils.isEntity(currentEntity)) { EntityUtils.setValue(currentEntity, path, value); } else if (currentEntity instanceof Map) { ((Map) currentEntity).put(path, value); } else { PropertyUtils.setSimpleProperty(currentEntity, path, value); } }
From source file:net.sf.ij_plugins.util.DialogUtil.java
/** * Utility to automatically create ImageJ's GenericDialog for editing bean properties. It uses BeanInfo to extract * display names for each field. If a fields type is not supported irs name will be displayed with a tag * "[Unsupported type: class_name]"./*from w ww .j a va 2 s . co m*/ * * @param bean * @return <code>true</code> if user closed bean dialog using OK button, <code>false</code> otherwise. */ static public boolean showGenericDialog(final Object bean, final String title) { final BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(bean.getClass()); } catch (IntrospectionException e) { throw new IJPluginsRuntimeException("Error extracting bean info.", e); } final GenericDialog genericDialog = new GenericDialog(title); // Create generic dialog fields for each bean's property final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); try { for (int i = 0; i < propertyDescriptors.length; i++) { final PropertyDescriptor pd = propertyDescriptors[i]; final Class type = pd.getPropertyType(); if (type.equals(Class.class) && "class".equals(pd.getName())) { continue; } final Object o = PropertyUtils.getSimpleProperty(bean, pd.getName()); if (type.equals(Boolean.TYPE)) { boolean value = ((Boolean) o).booleanValue(); genericDialog.addCheckbox(pd.getDisplayName(), value); } else if (type.equals(Integer.TYPE)) { int value = ((Integer) o).intValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 0); } else if (type.equals(Float.TYPE)) { double value = ((Float) o).doubleValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 6, 10, ""); } else if (type.equals(Double.TYPE)) { double value = ((Double) o).doubleValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 6, 10, ""); } else { genericDialog.addMessage(pd.getDisplayName() + "[Unsupported type: " + type + "]"); } } } catch (IllegalAccessException e) { throw new IJPluginsRuntimeException(e); } catch (InvocationTargetException e) { throw new IJPluginsRuntimeException(e); } catch (NoSuchMethodException e) { throw new IJPluginsRuntimeException(e); } // final Panel helpPanel = new Panel(); // helpPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0)); // final Button helpButton = new Button("Help"); //// helpButton.addActionListener(this); // helpPanel.add(helpButton); // genericDialog.addPanel(helpPanel, GridBagConstraints.EAST, new Insets(15,0,0,0)); // Show modal dialog genericDialog.showDialog(); if (genericDialog.wasCanceled()) { return false; } // Read fields from generic dialog into bean's properties. try { for (int i = 0; i < propertyDescriptors.length; i++) { final PropertyDescriptor pd = propertyDescriptors[i]; final Class type = pd.getPropertyType(); final Object propertyValue; if (type.equals(Boolean.TYPE)) { boolean value = genericDialog.getNextBoolean(); propertyValue = Boolean.valueOf(value); } else if (type.equals(Integer.TYPE)) { int value = (int) Math.round(genericDialog.getNextNumber()); propertyValue = new Integer(value); } else if (type.equals(Float.TYPE)) { double value = genericDialog.getNextNumber(); propertyValue = new Float(value); } else if (type.equals(Double.TYPE)) { double value = genericDialog.getNextNumber(); propertyValue = new Double(value); } else { continue; } PropertyUtils.setProperty(bean, pd.getName(), propertyValue); } } catch (IllegalAccessException e) { throw new IJPluginsRuntimeException(e); } catch (InvocationTargetException e) { throw new IJPluginsRuntimeException(e); } catch (NoSuchMethodException e) { throw new IJPluginsRuntimeException(e); } return true; }
From source file:cern.accsoft.steering.jmad.domain.knob.bean.BeanPropertyKnob.java
/** * Gets a double value from a bean-property with the given name. * //from ww w. j av a 2 s . com * @param bean the bean from which to get the value * @param propertyName the name of the property * @return the actual value of the varied property of the bean */ private static final double getBeanValue(Object bean, String propertyName) { try { Double value = (Double) PropertyUtils.getSimpleProperty(bean, propertyName); if (value != null) { return value.doubleValue(); } } catch (Exception e) { LOGGER.error("Cannot read property '" + propertyName + "' of bean '" + bean + "'.", e); } return 0.0; }
From source file:fr.opensagres.struts2.views.xdocreport.LazyCommonsBeanUtilsPopulateContext.java
protected Object getSimpleProperty(Object pojo, PropertyDescriptor descriptor) throws Exception { return PropertyUtils.getSimpleProperty(pojo, descriptor.getName()); }