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.sunchenbin.store.feilong.core.bean.PropertyUtil.java
/** * {@link PropertyUtils#getProperty(Object, String)} ?. * * @param <T>/*from ww w . j a va 2 s . c om*/ * the generic type * @param bean * Bean whose property is to be extracted * @param propertyName * Possibly indexed and/or nested name of the property to be extracted * @return {@link PropertyUtils#getProperty(Object, String)} ? * @see com.sunchenbin.store.feilong.core.bean.BeanUtil#getProperty(Object, String) * @see org.apache.commons.beanutils.BeanUtils#getProperty(Object, String) * @see org.apache.commons.beanutils.PropertyUtils#getProperty(Object, String) * @see org.apache.commons.beanutils.PropertyUtilsBean */ @SuppressWarnings("unchecked") public static <T> T getProperty(Object bean, String propertyName) { //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. try { return (T) PropertyUtils.getProperty(bean, propertyName); } catch (Exception e) { LOGGER.error(e.getClass().getName(), e); throw new BeanUtilException(e); } }
From source file:hermes.browser.dialog.EditNamingConfigDialog.java
@SuppressWarnings("unchecked") public void doSelectionChanged() { try {/*ww w . ja v a 2s. c o m*/ 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.bstek.dorado.idesupport.output.RuleSetOutputter.java
private void setElementAttribute(Element element, Object object, String propertyName) throws Exception { Object value = PropertyUtils.getProperty(object, propertyName); if (value == null || Boolean.FALSE.equals(value) || (value instanceof Number && ((Number) value).intValue() == 0)) { return;/* w w w . ja va 2 s .com*/ } String text = null; if (propertyName.equals("clientTypes")) { text = ClientType.toString(((Integer) value).intValue()); } else if (value instanceof String[]) { text = StringUtils.join((String[]) value, ','); } else if (value instanceof Class<?>) { if (value != String.class) text = ((Class<?>) value).getName(); } else { text = value.toString(); } if (StringUtils.isEmpty(text)) { return; } element.addAttribute(propertyName, text); }
From source file:de.iteratec.iteraplan.presentation.tags.TagUtils.java
/** * Locate and return the specified property of the specified bean, from an optionally specified * scope, in the specified page context. * /*from www . ja v a2s. c o m*/ * @param pageContext * Page context to be searched * @param name * Name of the bean to be retrieved * @param property * Name of the property to be retrieved, or <code>null</code> to retrieve the bean itself * @param scope * Scope to be searched (page, request, session, application) or <code>null</code> to use * <code>findAttribute()</code> instead * @return property of specified JavaBean * @throws JspException * if an invalid scope name is requested * @throws JspException * if the specified bean is not found * @throws JspException * if accessing this property causes an IllegalAccessException, * IllegalArgumentException, InvocationTargetException, or NoSuchMethodException */ public static Object lookup(PageContext pageContext, String name, String property, String scope) throws JspException { // Look up the requested bean, and return if requested Object bean = lookup(pageContext, name, scope); if (bean == null) { if (scope == null) { throw new JspException("Could not find bean '" + name + "' in any scope."); } else { throw new JspException("Could not find bean '" + name + "' in scope '" + scope + "'."); } } if (property == null) { return bean; } // Locate and return the specified property try { return PropertyUtils.getProperty(bean, property); } catch (IllegalAccessException e) { throw new JspException( String.format("Invalid access looking up property '%s' of bean %s", property, name), e); } catch (IllegalArgumentException e) { throw new JspException( String.format("Invalid argument looking up property '%s' of bean %s", property, name), e); } catch (InvocationTargetException e) { throw new JspException( String.format("Exception thrown by getter for property '%s' of bean %s", property, name), e); } catch (NoSuchMethodException e) { throw new JspException(String.format("No getter method for property '%s' of bean %s", property, name), 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 w ww.ja v a 2 s .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.project.framework.util.ReflectionUtils.java
/** * ????(getter), ??List.// www . j a v a 2s .c o m * * @param collection ???. * @param propertyName ??????. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static List convertElementPropertyToList(final Collection collection, final String propertyName) { List list = new ArrayList(); try { for (Object obj : collection) { list.add(PropertyUtils.getProperty(obj, propertyName)); } } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } return list; }
From source file:com.belle.yitiansystem.merchant.service.impl.MerchantOperationLogServiceImpl.java
@Override public String buildMerchantAccountOperationNotes(MerchantUser source, MerchantUser target) throws Exception { if (target == null) { throw new NullPointerException("target"); }//from w w w . ja v a2 s .c o m if (source == null) { return "??"; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : ACCOUNT_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { if (StringUtils.equals("status", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "?" : "?"; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "?" : "?"; } else if (StringUtils.equals("isAdministrator", entry.getKey()) || StringUtils.equals("deleteFlag", entry.getKey()) || StringUtils.equals("isYougouAdmin", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "" : "?"; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "" : "?"; } sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); }
From source file:com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils.java
public static Collection<? extends PropertyExpansion> extractPropertyExpansions(ModelItem modelItem, Object target, String propertyName) { List<PropertyExpansion> result = new ArrayList<PropertyExpansion>(); Set<String> expansions = new HashSet<String>(); try {// w w w .j a va2 s . c om Object property = PropertyUtils.getProperty(target, propertyName); if (property instanceof String && PropertyUtils.isWriteable(target, propertyName)) { String str = property.toString(); if (!StringUtils.isNullOrEmpty(str)) { int ix = str.indexOf("${"); while (ix != -1) { // TODO handle nested property-expansions.. int ix2 = str.indexOf('}', ix + 2); if (ix2 == -1) break; String expansion = str.substring(ix + 2, ix2); if (!expansions.contains(expansion)) { MutablePropertyExpansion tp = createMutablePropertyExpansion(expansion, modelItem, target, propertyName); if (tp != null) { result.add(tp); expansions.add(expansion); } } str = str.substring(ix2); ix = str.indexOf("${"); } } } } catch (Exception e) { SoapUI.logError(e); } return result; }
From source file:com.yougou.merchant.api.common.MerchantLogTools.java
public static String buildRejectAddressOperationNotes(RejectedAddressVo source, RejectedAddressVo target) throws Exception { if (target == null) { throw new NullPointerException("target"); }//from w w w . ja v a 2 s.co m if (source == null) { return "??"; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : AFTER_SERVICE_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); }