Java Code Examples for org.apache.commons.beanutils.PropertyUtils#getReadMethod()

The following examples show how to use org.apache.commons.beanutils.PropertyUtils#getReadMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KualiTestAssertionUtils.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Asserts that the non-null non-BO properties of the expected bean are equal to those of the actual bean. Any null or
 * BusinessObject expected properties are ignored. Attributes are reflected bean-style via any public no-argument methods
 * starting with "get" (or "is" for booleans), including inherited methods. The expected and actual beans do not need to be of
 * the same class.
 * <p>
 * Reflection wraps primitives, so differences in primitiveness are ignored. Properties that are BusinessObjects (generally
 * relations based on foreign key properties) are also ignored, because this method is not testing OJB foreign key resolution
 * (e.g., via the <code>refresh</code> method), we do not want to have to put all the related BOs into the test fixture
 * (redundant with the foreign keys), and many (all?) of our BOs implement the <code>equals</code> method in terms of identity
 * so would fail this assertion anyway. This is a data-oriented assertion, for our data-oriented tests and persistence layer.
 * 
 * @param message a description of this test assertion
 * @param expectedBean a java bean containing expected properties
 * @param actualBean a java bean containing actual properties
 * @throws InvocationTargetException if a getter method throws an exception (the cause)
 * @throws NoSuchMethodException if an expected property does not exist in the actualBean
 */
public static void assertSparselyEqualBean(String message, Object expectedBean, Object actualBean) throws InvocationTargetException, NoSuchMethodException {
    if (message == null) {
        message = "";
    }
    else {
        message = message + " ";
    }
    assertNotNull(message + "actual bean is null", actualBean);
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(expectedBean);
    for (int i = 0; i < descriptors.length; i++) {
        PropertyDescriptor descriptor = descriptors[i];
        if (PropertyUtils.getReadMethod(descriptor) != null) {
            try {
                Object expectedValue = PropertyUtils.getSimpleProperty(expectedBean, descriptor.getName());
                if (expectedValue != null && !(expectedValue instanceof BusinessObject)) {
                    assertEquals(message + descriptor.getName(), expectedValue, PropertyUtils.getSimpleProperty(actualBean, descriptor.getName()));
                }
            }
            catch (IllegalAccessException e) {
                throw new AssertionError(e); // can't happen because getReadMethod() returns only public methods
            }
        }
    }
}
 
Example 2
Source File: FieldFilter.java    From para with Apache License 2.0 5 votes vote down vote up
private Object getProperty(Object obj, String prop) {
	if (obj != null && !StringUtils.isBlank(prop)) {
		try {
			Method m = PropertyUtils.getReadMethod(new PropertyDescriptor(prop, obj.getClass()));
			if (m != null && !m.isAnnotationPresent(JsonIgnore.class)) {
				return PropertyUtils.getProperty(obj, prop);
			}
		} catch (Exception e) {	}
	}
	return null;
}
 
Example 3
Source File: Bean.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
public static Method findGetter(Object bean, String property)
		throws Exception {
	PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(
			bean, property);
	if (descriptor != null)
		return PropertyUtils.getReadMethod(descriptor);
	return null;
}