List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyDescriptors
public static PropertyDescriptor[] getPropertyDescriptors(Object bean)
Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.
For more details see PropertyUtilsBean
.
From source file:com.mycollab.db.persistence.service.DefaultService.java
@Override public void removeByCriteria(S criteria, Integer accountId) { boolean isValid = false; try {//from w w w. jav a 2s . c om PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(criteria); for (PropertyDescriptor descriptor : propertyDescriptors) { String propName = descriptor.getName(); if ((descriptor.getPropertyType().getGenericSuperclass() == SearchField.class) && (PropertyUtils.getProperty(criteria, propName) != null)) { isValid = true; break; } } } catch (Exception e) { LOG.debug("Error while validating criteria", e); } if (isValid) { getSearchMapper().removeByCriteria(criteria); } }
From source file:com.agimatec.validation.util.PropertyAccess.java
public Type getJavaType() { /*if(Map.class.isAssignableFrom(beanClass)) { return beanClass. /*from ww w . ja v a 2 s .c om*/ }*/ if (rememberField != null) { // use cached field of previous access return rememberField.getGenericType(); } for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(beanClass)) { if (each.getName().equals(propertyName) && each.getReadMethod() != null) { return each.getReadMethod().getGenericReturnType(); } } try { // try public field return beanClass.getField(propertyName).getGenericType(); } catch (NoSuchFieldException ex2) { // search for private/protected field up the hierarchy Class theClass = beanClass; while (theClass != null) { try { return theClass.getDeclaredField(propertyName).getGenericType(); } catch (NoSuchFieldException ex3) { // do nothing } theClass = theClass.getSuperclass(); } } return Object.class; // unknown type: allow any type?? }
From source file:com.mb.framework.util.property.PropertyUtilExt.java
/** * Copy property values from the origin bean to the destination bean for all * cases where the property names are the same. For each property, a * conversion is attempted as necessary. All combinations of standard * JavaBeans and DynaBeans as origin and destination are supported. * Properties that exist in the origin bean, but do not exist in the * destination bean (or are read-only in the destination bean) are silently * ignored./* w w w . j a v a 2s.c o m*/ * <p> * In addition to the method with the same name in the * <code>org.apache.commons.beanutils.PropertyUtils</code> class this method * can also copy properties of the following types: * <ul> * <li>java.lang.Integer</li> * <li>java.lang.Double</li> * <li>java.lang.Long</li> * <li>java.lang.Short</li> * <li>java.lang.Float</li> * <li>java.lang.String</li> * <li>java.lang.Boolean</li> * <li>java.sql.Date</li> * <li>java.sql.Time</li> * <li>java.sql.Timestamp</li> * <li>java.math.BigDecimal</li> * <li>a container-managed relations field.</li> * </ul> * * @param dest * Destination bean whose properties are modified * @param orig * Origin bean whose properties are retrieved * @throws IllegalAccessException * if the caller does not have access to the property accessor * method * @throws InvocationTargetException * if the property accessor method throws an exception * @throws NoSuchMethodException * if an accessor method for this propety cannot be found * @throws ClassNotFoundException * if an incorrect relations class mapping exists. * @throws InstantiationException * if an object of the mapped relations class can not be * constructed. */ public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, Exception { PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if (PropertyUtils.getPropertyDescriptor(dest, name) != null) { Object origValue = PropertyUtils.getSimpleProperty(orig, name); String origParamType = origDescriptors[i].getPropertyType().getName(); try { // edited // if (origValue == null)throw new NullPointerException(); PropertyUtils.setSimpleProperty(dest, name, origValue); } catch (Exception e) { try { String destParamType = PropertyUtils.getPropertyType(dest, name).getName(); if (origValue instanceof String) { if (destParamType.equals("java.lang.Integer")) { Integer intValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { intValue = new Integer(sValue); } PropertyUtils.setSimpleProperty(dest, name, intValue); } else if (destParamType.equals("java.lang.Byte")) { Byte byteValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { byteValue = new Byte(sValue); } PropertyUtils.setSimpleProperty(dest, name, byteValue); } else if (destParamType.equals("java.lang.Double")) { Double doubleValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { doubleValue = new Double(sValue); } PropertyUtils.setSimpleProperty(dest, name, doubleValue); } else if (destParamType.equals("java.lang.Long")) { Long longValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { longValue = new Long(sValue); } PropertyUtils.setSimpleProperty(dest, name, longValue); } else if (destParamType.equals("java.lang.Short")) { Short shortValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { shortValue = new Short(sValue); } PropertyUtils.setSimpleProperty(dest, name, shortValue); } else if (destParamType.equals("java.lang.Float")) { Float floatValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { floatValue = new Float(sValue); } PropertyUtils.setSimpleProperty(dest, name, floatValue); } else if (destParamType.equals("java.sql.Date")) { java.sql.Date dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Date(DATE_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.sql.Time")) { java.sql.Time dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Time(TIME_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.sql.Timestamp")) { java.sql.Timestamp dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Timestamp(TIMESTAMP_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.lang.Boolean")) { Boolean bValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { bValue = Boolean.valueOf(sValue); } PropertyUtils.setSimpleProperty(dest, name, bValue); } else if (destParamType.equals("java.math.BigDecimal")) { BigDecimal bdValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { bdValue = new BigDecimal(sValue); } PropertyUtils.setSimpleProperty(dest, name, bdValue); } } else if ((origValue != null) && (destParamType.equals("java.lang.String"))) { // we're transferring a business-layer value object // into a String-based Struts form bean.. if ("java.sql.Date".equals(origParamType)) { PropertyUtils.setSimpleProperty(dest, name, DATE_FORMATTER.format(origValue)); } else if ("java.sql.Timestamp".equals(origParamType)) { PropertyUtils.setSimpleProperty(dest, name, TIMESTAMP_FORMATTER.format(origValue)); } else if ("java.sql.Blob".equals(origParamType)) { // convert a Blob to a String.. Blob blob = (Blob) origValue; BufferedInputStream bin = null; try { int bytesRead; StringBuffer result = new StringBuffer(); byte[] buffer = new byte[READ_BUFFER_LENGTH]; bin = new BufferedInputStream(blob.getBinaryStream()); do { bytesRead = bin.read(buffer); if (bytesRead != -1) { result.append(new String(buffer, 0, bytesRead)); } } while (bytesRead == READ_BUFFER_LENGTH); PropertyUtils.setSimpleProperty(dest, name, result.toString()); } finally { if (bin != null) try { bin.close(); } catch (IOException ignored) { } } } else { PropertyUtils.setSimpleProperty(dest, name, origValue.toString()); } } } catch (Exception e2) { throw e2; } } } } }
From source file:com.timesoft.kaitoo.ws.hibernate.AbstractPojo.java
public String toValueString() { PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(this); StringBuffer buffer = new StringBuffer(); SimpleDateFormat simple = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH); if (((List) callStack.get()).contains(this)) { buffer.append("Cyclic Reference!!!"); } else {// w ww . j av a 2 s. co m ((List) callStack.get()).add(this); for (int index = 0; index < pd.length; ++index) { if ((null != PropertyUtils.getReadMethod(pd[index])) && (pd[index].getPropertyType() != Class.class)) { if (buffer.length() > 0) { buffer.append(", "); } String prop_name = pd[index].getName(); try { if (null == PropertyUtils.getProperty(this, prop_name)) { buffer.append("\" \""); } else { if (pd[index].getPropertyType() == Calendar.class) { buffer.append("\"" + simple.format( ((Calendar) PropertyUtils.getProperty(this, prop_name)).getTime()) + "\""); } else if (pd[index].getPropertyType() == Date.class) { buffer.append( "\"" + simple.format(PropertyUtils.getProperty(this, prop_name) + "\"")); } else { buffer.append("\"" + PropertyUtils.getProperty(this, prop_name) + "\""); } } } catch (Exception e) { buffer.append(e.getMessage()); } } } ((List) callStack.get()).remove(this); } buffer.append(" \n"); return buffer.toString(); }
From source file:com.esofthead.mycollab.core.persistence.service.DefaultService.java
@Override public void removeByCriteria(S criteria, int accountId) { boolean isValid = false; try {//w w w . jav a2s. co m PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(criteria); for (PropertyDescriptor descriptor : propertyDescriptors) { String propName = descriptor.getName(); if ((descriptor.getPropertyType().getGenericSuperclass() == SearchField.class) && (PropertyUtils.getProperty(criteria, propName) != null)) { isValid = true; break; } } } catch (Exception e) { LOG.debug("Error while validating criteria", e); } if (isValid) { getSearchMapper().removeByCriteria(criteria); } }
From source file:com.iorga.webappwatcher.util.BasicParameterHandler.java
private PropertyDescriptor findFieldPropertyDescriptor(final String fieldName) { final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(ownerClass); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (fieldName.equals(propertyDescriptor.getName())) { return propertyDescriptor; }/*from w w w. j a va2 s .c om*/ } throw new IllegalStateException("Couldn't find getter/setter for " + ownerClass + "." + fieldName); }
From source file:com.lingxiang2014.template.directive.BaseDirective.java
protected List<Filter> getFilters(Map<String, TemplateModel> params, Class<?> type, String... ignoreProperties) throws TemplateModelException { List<Filter> filters = new ArrayList<Filter>(); PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(type); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String propertyName = propertyDescriptor.getName(); Class<?> propertyType = propertyDescriptor.getPropertyType(); if (!ArrayUtils.contains(ignoreProperties, propertyName) && params.containsKey(propertyName)) { Object value = FreemarkerUtils.getParameter(propertyName, propertyType, params); filters.add(Filter.eq(propertyName, value)); }/* ww w . jav a 2 s . c o m*/ } return filters; }
From source file:com.afeng.common.utils.reflection.MyBeanUtils.java
/** * ?/*from w w w.j av a2 s . co m*/ * ??? * * @param databean * @param tobean * @throws NoSuchMethodException * copy */ public static void copyBeanNotNull2Bean(Object databean, Object tobean) throws Exception { PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); // String type = origDescriptors[i].getPropertyType().toString(); if ("class".equals(name)) { continue; // No point in trying to set an object's class } if (PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) { try { Object value = PropertyUtils.getSimpleProperty(databean, name); if (value != null) { copyProperty(tobean, name, value); } } catch (IllegalArgumentException ie) { ; // Should not happen } catch (Exception e) { ; // Should not happen } } } }
From source file:com.eryansky.common.utils.reflection.MyBeanUtils.java
/** * ?/*from w w w .j a va2 s.co m*/ * ??? * * @param databean * @param tobean * @throws NoSuchMethodException * copy */ public static void copyBeanNotNull2Bean(Object databean, Object tobean) throws Exception { PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); // String type = origDescriptors[i].getPropertyType().toString(); if ("class".equals(name)) { continue; // No point in trying to set an object's class } if (PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) { try { Object value = PropertyUtils.getSimpleProperty(databean, name); if (value != null) { copyProperty(tobean, name, value); } } catch (java.lang.IllegalArgumentException ie) { ; // Should not happen } catch (Exception e) { ; // Should not happen } } } }
From source file:de.iteratec.iteraplan.businesslogic.exchange.common.vbb.impl.util.VisualVariableHelper.java
/** * Derives all visual variables from a given class and adds the variables as EAttributes to the supplied EClass. * @param vbbClass the class that defines the visual variables via annotated methods {@link VisualVariable} * @param eClass the EClass to which the introspected variables should be added as EAttributes *///from w ww . ja v a2 s . c om public static void addAllVisualVariables(Class<? extends VBB> vbbClass, EClass eClass) { for (PropertyDescriptor vvCandidate : PropertyUtils.getPropertyDescriptors(vbbClass)) { VisualVariable vvAnnotation = getVVAnnotation(vvCandidate); if (vvAnnotation != null) { EAttribute att = EcoreFactory.eINSTANCE.createEAttribute(); att.setName(vvCandidate.getName()); att.setLowerBound(vvAnnotation.mandatory() ? 1 : 0); setMultiplicityAndDataType(eClass.getEPackage(), att, vvCandidate); if (att.getEType() != null) { eClass.getEStructuralFeatures().add(att); } else { LOGGER.warn("Could not introspect type for field " + vvCandidate.getName() + "."); } } } }