Example usage for org.apache.commons.beanutils PropertyUtils getPropertyDescriptors

List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyDescriptors

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getPropertyDescriptors.

Prototype

public static PropertyDescriptor[] getPropertyDescriptors(Object bean) 

Source Link

Document

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.

Usage

From source file:org.xmlactions.mapping.xml_to_bean.PopulateClassFromXml.java

public static PropertyDescriptor findPropertyDescriptor(Object object, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    PropertyDescriptor pd = null;
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(object);
    for (PropertyDescriptor p : pds) {
        if (p.getName().equals(name)) {
            pd = p;/*from   w  ww .  j ava  2  s .c o  m*/
            break;
        }
    }
    if (pd != null) {
        log.debug("PropertyDescriptor [" + name + "] - " + " Name:" + pd.getName() + " DisplayName:"
                + pd.getDisplayName() + " ShortDescription:" + pd.getShortDescription() + " PropertyType:"
                + pd.getPropertyType() + " ReadMethod:" + pd.getReadMethod() + " WriteMethod:"
                + pd.getWriteMethod() + " Value:" + pd.getValue(name));
        // } else {
        // log.error("PropertyDescriptor [" + name +
        // "] -  not found in class [" + object.getClass().getName() + "]");
    }
    return pd;

}

From source file:pt.ist.maidSyncher.domain.SynchableObject.java

/**
 * /*  w  w w.  jav  a  2 s . c  om*/
 * @param orig
 * @return a collection with the {@link PropertyDescriptor} s that changed
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws TaskNotVisibleException
 */
public Collection<String> copyPropertiesFrom(Object orig) throws IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, TaskNotVisibleException {
    Set<PropertyDescriptor> propertyDescriptorsThatChanged = new HashSet<PropertyDescriptor>();

    Object dest = this;

    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }
    PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
    for (PropertyDescriptor origDescriptor : origDescriptors) {
        String name = origDescriptor.getName();
        if (PropertyUtils.isReadable(orig, name)) {
            if (PropertyUtils.isWriteable(dest, name)) {
                Object valueDest = PropertyUtils.getSimpleProperty(dest, name);
                Object valueOrigin = PropertyUtils.getSimpleProperty(orig, name);
                if (valueOrigin != null && valueOrigin.getClass().getPackage().getName()
                        .equalsIgnoreCase("org.eclipse.egit.github.core"))
                    continue; //let's skip the properties with egit core objects (they shall be copied from a custom overriden version of this method)
                if (valueOrigin instanceof Date)
                    valueOrigin = new DateTime(valueOrigin);
                if (Objects.equal(valueDest, valueOrigin) == false)
                    propertyDescriptorsThatChanged.add(origDescriptor);
                try {
                    //let's see if this is actually a Date, if so, let's convert it
                    PropertyUtils.setSimpleProperty(dest, name, valueOrigin);
                } catch (IllegalArgumentException ex) {
                    throw new Error("setSimpleProperty returned an exception, dest: "
                            + dest.getClass().getName() + " oid: " + ((DomainObject) dest).getExternalId()
                            + " name : " + name + " valueOrig: " + valueOrigin, ex);
                }
                LOGGER.trace("Copied property " + name + " from " + orig.getClass().getName() + " object to a "
                        + dest.getClass().getName() + " oid: " + getExternalId());
            }
        }
    }

    return Collections2.transform(propertyDescriptorsThatChanged, new Function<PropertyDescriptor, String>() {
        @Override
        public String apply(PropertyDescriptor propertyDescriptor) {
            if (propertyDescriptor == null) {
                return null;
            }
            return propertyDescriptor.getName();

        }
    });
}

From source file:pt.ist.maidSyncher.domain.SynchableObject.java

public void copyPropertiesTo(Object dest) throws IllegalAccessException, InvocationTargetException,
        NoSuchMethodException, TaskNotVisibleException {
    Set<PropertyDescriptor> propertyDescriptorsThatChanged = new HashSet<PropertyDescriptor>();

    Object orig = this;
    checkNotNull(dest);/*from   w  w w. j a va2  s .co  m*/

    PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
    for (PropertyDescriptor origDescriptor : origDescriptors) {
        String name = origDescriptor.getName();
        if (PropertyUtils.isReadable(orig, name)) {
            PropertyDescriptor destDescriptor = PropertyUtils.getPropertyDescriptor(dest,
                    origDescriptor.getName());
            if (PropertyUtils.isWriteable(dest, name) || (destDescriptor != null
                    && MiscUtils.getWriteMethodIncludingFlowStyle(destDescriptor, dest.getClass()) != null)) {
                Object valueDest = PropertyUtils.getSimpleProperty(dest, name);
                Object valueOrigin = PropertyUtils.getSimpleProperty(orig, name);

                LOGGER.debug("OrigDescriptor PropertyType: " + origDescriptor.getPropertyType().getName());
                //                    System.out.println("OrigDescriptor PropertyType: " + origDescriptor.getPropertyType().getName());
                //let's ignore the properties were the values are our domain packages
                if (valueOrigin != null && (SynchableObject.class.isAssignableFrom(valueOrigin.getClass()))) {
                    //                        System.out.println("Skipping");
                    continue; //let's skip these properties
                }
                if (SynchableObject.class.isAssignableFrom(origDescriptor.getPropertyType())) {
                    //                        System.out.println("Skipping");
                    continue;
                }
                if (origDescriptor instanceof IndexedPropertyDescriptor) {
                    IndexedPropertyDescriptor indexedPropertyDescriptor = (IndexedPropertyDescriptor) origDescriptor;
                    //                        System.out.println("OrigDescriptor IndexedPropertyDescriptor: " + indexedPropertyDescriptor.getName());
                    if (SynchableObject.class
                            .isAssignableFrom(indexedPropertyDescriptor.getIndexedPropertyType())) {
                        //                            System.out.println("Skipping");
                        continue;
                    }

                }

                //let's ignore all of the dates - as they should be filled by
                //the system
                if (valueOrigin instanceof LocalTime)
                    continue;
                if (Objects.equal(valueDest, valueOrigin) == false)
                    propertyDescriptorsThatChanged.add(origDescriptor);
                try {
                    if (PropertyUtils.isWriteable(dest, name) == false) {
                        //let's use the flow version
                        Class<?> origPropertyType = origDescriptor.getPropertyType();
                        Method writeMethodIncludingFlowStyle = MiscUtils
                                .getWriteMethodIncludingFlowStyle(destDescriptor, dest.getClass());
                        if (Arrays.asList(writeMethodIncludingFlowStyle.getParameterTypes())
                                .contains(origPropertyType)) {
                            writeMethodIncludingFlowStyle.invoke(dest, valueOrigin);
                        } else {
                            continue;
                        }

                    } else {
                        PropertyUtils.setSimpleProperty(dest, name, valueOrigin);

                    }
                } catch (IllegalArgumentException ex) {
                    throw new Error("setSimpleProperty returned an exception, dest: "
                            + dest.getClass().getName() + " name : " + name + " valueOrig: " + valueOrigin, ex);
                }
                LOGGER.trace("Copied property " + name + " from " + orig.getClass().getName() + " object to a "
                        + dest.getClass().getName() + " oid: " + getExternalId());
            }
            //                System.out.println("--");
        }
    }

}

From source file:pt.ist.maidSyncher.domain.SynchableObject.java

/**
 * // w w  w  .  ja  va 2 s  .c o  m
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws TaskNotVisibleException
 * @throws IllegalArgumentException if the property descriptors of the arguments differ
 * 
 * @return a collection of {@link PropertyDescriptor} that changed, or an empty collection
 */
public static Collection<PropertyDescriptor> propertiesEqual(Object object1, Object object2)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
        TaskNotVisibleException, IllegalArgumentException {

    List<PropertyDescriptor> changedDescriptors = new ArrayList<>();

    if (object1 == null || object2 == null) {
        throw new IllegalArgumentException("Both objects must be non null");
    }
    PropertyDescriptor object1Descriptors[] = PropertyUtils.getPropertyDescriptors(object1);
    PropertyDescriptor object2Descriptors[] = PropertyUtils.getPropertyDescriptors(object2);

    //let's make sure that they match
    checkArgument(ObjectUtils.equals(object1Descriptors, object2Descriptors),
            "Error, object1 : " + object1.getClass().getSimpleName() + " and object2 : "
                    + object2.getClass().getSimpleName() + " don't match");

    for (PropertyDescriptor object1Descriptor : object1Descriptors) {
        String name = object1Descriptor.getName();
        if (PropertyUtils.isReadable(object1, name) && PropertyUtils.isReadable(object2, name)) {

            //if both are readable, let's check on the values
            Object valueObject1 = PropertyUtils.getSimpleProperty(object1, name);
            Object valueObject2 = PropertyUtils.getSimpleProperty(object2, name);
            //                if (isGitHubObject(valueObject1) || isGitHubObject(valueObject2))
            //                    continue; //let's skip the GitHub properties, we won't be able to compare that

            if (!ObjectUtils.equals(valueObject1, valueObject2))
                changedDescriptors.add(object1Descriptor);
        }
    }
    return changedDescriptors;
}

From source file:technology.tikal.gae.pagination.PaginationModelFactory.java

private static String armaUrl(String baseUrl, final FiltroBusqueda filtro, final Object sinceId,
        final Object since, final int maxResults) {
    String r = baseUrl;/*w ww . j av  a2  s.  c  o  m*/
    if (filtro != null) {
        PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(filtro);
        for (PropertyDescriptor x : descriptors) {
            if (x.getName().compareTo("class") != 0) {
                Object obj;
                try {
                    obj = PropertyUtils.getProperty(filtro, x.getName());
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    throw new IllegalArgumentException(
                            "no se puede construir la pagina con el filtro proporcionado");
                }
                if (obj != null) {
                    r = addUrlParameter(r, x.getName(), obj.toString());
                }
            }
        }
    }
    if (sinceId != null) {
        r = addUrlParameter(r, "sinceId", sinceId.toString());
    }
    if (since != null) {
        r = addUrlParameter(r, "since", since.toString());
    }
    if (maxResults > 0) {
        r = addUrlParameter(r, "maxResults", maxResults + "");
    }
    return r;
}

From source file:technology.tikal.hibernate.validation.NotEmptyPojoValidator.java

@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
    if (value == null) {
        return false;
    }//from  ww  w .  j  a  v a2 s  .  co  m
    if (attributes.length > 0) {
        Object x;
        for (String attribute : attributes) {
            try {
                x = PropertyUtils.getProperty(value, attribute);
                if (x != null) {
                    return true;
                }
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            }
        }
    } else {
        PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors(value);
        Object x;
        for (PropertyDescriptor prop : props) {
            try {
                x = PropertyUtils.getProperty(value, prop.getName());
                if (x != null && !(x instanceof Class)) {
                    return true;
                }
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                return false;
            }
        }
    }
    return false;
}