List of usage examples for org.apache.commons.beanutils PropertyUtils getProperty
public static Object getProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return 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.mmj.app.common.core.lang.CollectionUtils.java
/** * List<T> ---> List<Map<key,?>> *//*from w ww. j a v a 2 s .c o m*/ public static <T extends Object> List<Map<String, ?>> toMapList(Collection<T> values, String... propertys) { if (Argument.isEmptyArray(propertys) || values == null || values.isEmpty()) { return Collections.<Map<String, ?>>emptyList(); } List<Map<String, ?>> list = new ArrayList<Map<String, ?>>(); for (T value : values) { Map<String, Object> valueMap = new HashMap<String, Object>(propertys.length); for (String property : propertys) { try { Object propertyValue = PropertyUtils.getProperty(value, property); if (propertyValue == null) { continue; } Object wapper = valueMap.get(property); if (wapper == null) { valueMap.put(property, propertyValue); } } catch (Exception e) { logger.error(e.getMessage(), e); continue; } } list.add(valueMap); } return list; }
From source file:com.rolex.explore.beanutils.service.BeanUtilsService.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);// w w w . j a va 2 s . c om 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.setProperty(bean, property1, "Rolex Rolex"); String value1 = (String) PropertyUtils.getProperty(bean, property1); System.out.println("###Reverse1: " + value1); PropertyUtils.setProperty(bean, property2, "Hoffman Estates"); String value2 = (String) PropertyUtils.getProperty(bean, property2); System.out.println("###Reverse2: " + value2); PropertyUtils.setProperty(bean, property3, "Schaumburg"); String value3 = (String) PropertyUtils.getProperty(bean, property3); System.out.println("###Reverse3: " + value3); PropertyUtils.setProperty(bean, property4, "Des Plaines"); String value4 = (String) PropertyUtils.getProperty(bean, property4); System.out.println("###Reverse4: " + value4); Address[] arrayValue1 = (Address[]) PropertyUtils.getProperty(bean, "previousAddresses"); System.out.println("###ReverseArray: " + Arrays.toString(arrayValue1)); PropertyUtils.setProperty(bean, property5, "Sonata"); String value5 = (String) PropertyUtils.getProperty(bean, property5); System.out.println("###Reverse5: " + value5); PropertyUtils.setProperty(bean, property6, places); List<Place> value6 = (List<Place>) PropertyUtils.getProperty(bean, property6); System.out.println("###Reverse6: " + value6.get(0)); PropertyUtils.setProperty(bean, property7, places); List<Place> value7 = (List<Place>) PropertyUtils.getProperty(bean, property7); System.out.println("###Reverse7: " + value7.get(0)); PropertyUtils.setProperty(bean, property8, place2); Place value8 = (Place) PropertyUtils.getProperty(bean, property8); System.out.println("###Reverse8: " + value8); PropertyUtils.setProperty(bean, property9, award); TourismAward value9 = (TourismAward) PropertyUtils.getProperty(bean, property9); System.out.println("###Reverse8: " + value8); System.out.println("Bean: " + bean); SampleBean copyBean = new SampleBean(); BeanUtils.copyProperties(copyBean, bean); System.out.println("Bean: " + bean); System.out.println("Copy Bean: " + copyBean); } 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.intuit.tank.PropertyComparer.java
/** * @{inheritDoc/*from w w w. j a v a 2s . c om*/ */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public int compare(T src, T tgt) { int retVal = 0; if (src == null && tgt == null) { retVal = 0; } else if (src != null && tgt == null) { retVal = 1; } else if (src == null && tgt != null) { retVal = -1; } else { try { Object property = PropertyUtils.getProperty(src, propertyName); Object property2 = PropertyUtils.getProperty(tgt, propertyName); if (property == null && property2 == null) { retVal = 0; } else if (property == null && property2 != null) { retVal = -1; } else if (property != null && property2 == null) { retVal = 1; } else if (Comparable.class.isAssignableFrom(property.getClass())) { Comparable c1 = (Comparable) property; Comparable c2 = (Comparable) property2; retVal = c1.compareTo(c2); } else { retVal = property.toString().compareTo(property2.toString()); } } catch (IllegalAccessException e) { throw new RuntimeException( "Cannot access the method. Possible error in setting the access type for the getter setters of " + propertyName); } catch (InvocationTargetException e) { throw new RuntimeException(e.getMessage()); } catch (NoSuchMethodException e) { throw new RuntimeException("No getter/setter method found for " + propertyName); } } if (sortOrder == SortOrder.DESCENDING) { retVal = retVal * -1; } return retVal; }
From source file:net.cloudkit.enterprises.infrastructure.utilities.Collections3Utility.java
/** * ????(Getter), ??List.//from w ww.j a v a 2s. c o m * * @param collection ???. * @param propertyName ??????. */ public static List<Object> extractToList(final Collection<?> collection, final String propertyName) { List<Object> list = new ArrayList<Object>(collection.size()); try { for (Object obj : collection) { list.add(PropertyUtils.getProperty(obj, propertyName)); } } catch (Exception e) { throw ReflectionHelper.convertReflectionExceptionToUnchecked(e); } return list; }
From source file:com.esofthead.mycollab.module.ecm.ResourceUtils.java
/** * /*from w w w. j ava2s. c om*/ * @param resource * @return */ public static ResourceType getType(Resource resource) { if (!resource.isExternalResource()) { return ResourceType.MyCollab; } else { try { String storageName = (String) PropertyUtils.getProperty(resource, "storageName"); if (StorageNames.DROPBOX.equals(storageName)) { return ResourceType.Dropbox; } else { throw new Exception("Current support only dropbox resource service"); } } catch (Exception e) { throw new MyCollabException( "Can not define sotrage name of bean " + BeanUtility.printBeanObj(resource)); } } }
From source file:com.esofthead.mycollab.schedule.email.format.DefaultFieldFormat.java
@Override public String formatField(MailContext<?> context) { Object wrappedBean = context.getWrappedBean(); Object value;/* w w w .j av a 2 s.c o m*/ try { value = PropertyUtils.getProperty(wrappedBean, fieldName); if (value == null) { return new Span().write(); } else { return new Span().appendText(value.toString()).write(); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { log.error("Can not generate email field: " + fieldName, e); return new Span().write(); } }
From source file:com.blackbear.flatworm.PropertyUtilsMappingStrategy.java
@Override public void mapBean(Object bean, String beanName, String property, Object value, Map<String, ConversionOption> conv) throws FlatwormConversionException { try {/*from w ww . ja v a 2 s .c o m*/ ConversionOption option = conv.get("append"); if (option != null && "true".equalsIgnoreCase(option.getValue())) { Object currentValue = PropertyUtils.getProperty(bean, property); if (currentValue != null) value = currentValue.toString() + value; } PropertyUtils.setProperty(bean, property, value); } catch (IllegalAccessException e) { log.error("While running set property method for " + beanName + "." + property + "with value '" + value + "'", e); throw new FlatwormConversionException("Setting field " + beanName + "." + property); } catch (InvocationTargetException e) { log.error("While running set property method for " + beanName + "." + property + "with value '" + value + "'", e); throw new FlatwormConversionException("Setting field " + beanName + "." + property); } catch (NoSuchMethodException e) { log.error("While running set property method for " + beanName + "." + property + "with value '" + value + "'", e); throw new FlatwormConversionException("Setting field " + beanName + "." + property); } }
From source file:com.eviware.soapui.model.support.XPathReferenceImpl.java
public XPathReferenceImpl(String label, Operation operation, boolean request, Object target, String xpathPropertyName) { this.label = label; this.operation = operation; this.request = request; this.target = target; this.xpathPropertyName = xpathPropertyName; try {/* w w w .j a v a2s . c om*/ this.xpath = (String) PropertyUtils.getProperty(target, xpathPropertyName); } catch (Exception e) { SoapUI.logError(e); } }
From source file:com.epam.cme.storefront.forms.validation.EqualAttributesValidator.java
@Override public boolean isValid(final Object object, final ConstraintValidatorContext constraintContext) { if (object == null) { return true; }//from w ww.ja va2 s. c o m try { final Object first = PropertyUtils.getProperty(object, firstAttribute); final Object second = PropertyUtils.getProperty(object, secondAttribute); return new EqualsBuilder().append(first, second).isEquals(); } catch (final Exception e) { LOG.error("Could not validate", e); throw new IllegalArgumentException(e); } }
From source file:com.pasteur.ci.action.type_gene_toxicite.CreerTypeGeneToxiciteAction.java
/** * This is the action called from the Struts framework. * * @param mapping The ActionMapping used to select this instance. * @param form The optional ActionForm bean for this request. * @param request The HTTP Request we are processing. * @param response The HTTP Response we are processing. * @throws java.lang.Exception/*from w w w . j a va 2s . co m*/ * @return */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String design_typ_gene_toxicite = (String) PropertyUtils.getProperty(form, "design_typ_gene_toxicite"); double conc_typ_gene_toxicite = (Double) PropertyUtils.getProperty(form, "conc_typ_gene_toxicite"); boolean visible = (Boolean) PropertyUtils.getProperty(form, "visible"); TypeGeneToxicite type_gene_toxicite = new TypeGeneToxicite(); type_gene_toxicite.setDesign_typ_gene_toxicite(design_typ_gene_toxicite); type_gene_toxicite.setVisible(visible); TypeGeneToxiciteDAOImplement gdaoi = new TypeGeneToxiciteDAOImplement(DAOFactory.getInstance()); gdaoi.create(type_gene_toxicite); return mapping.findForward(SUCCESS); }