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

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

Introduction

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

Prototype

public static boolean isReadable(Object bean, String name) 

Source Link

Document

Return true if the specified property name identifies a readable property on the specified bean; otherwise, return false.

For more details see PropertyUtilsBean.

Usage

From source file:io.milton.http.annotated.ContactDataAnnotationHandler.java

public String execute(AnnoContactResource contactRes) {
    Object source = contactRes.getSource();
    try {//from w ww  .ja  v  a 2 s . com
        Object value = null;
        ControllerMethod cm = getBestMethod(source.getClass());
        if (cm == null) {
            // look for an annotation on the source itself
            java.lang.reflect.Method m = annoResourceFactory.findMethodForAnno(source.getClass(), annoClass);
            if (m != null) {
                value = m.invoke(source, (Object) null);
            } else {
                for (String nameProp : PROP_NAMES) {
                    if (PropertyUtils.isReadable(source, nameProp)) {
                        Object oPropVal = PropertyUtils.getProperty(source, nameProp);
                        value = oPropVal;
                        break;
                    }
                }
            }
        } else {
            value = invoke(cm, contactRes);
        }
        if (value != null) {
            if (value instanceof String) {
                return (String) value;
            } else if (value instanceof byte[]) {
                byte[] bytes = (byte[]) value;
                return new String(bytes, "UTF-8");
            } else if (value instanceof InputStream) {
                InputStream in = (InputStream) value;
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                IOUtils.copy(in, bout);
                return bout.toString("UTF-8");
            } else {
                return value.toString();
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception executing " + getClass() + " - " + source.getClass(), e);
    }
}

From source file:io.milton.http.annotated.ICalDataAnnotationHandler.java

public String execute(AnnoEventResource eventRes) {
    Object source = eventRes.getSource();
    try {//  ww w .  j  a  v a2s .co  m
        Object value = null;
        ControllerMethod cm = getBestMethod(source.getClass());
        if (cm == null) {
            // look for an annotation on the source itself
            java.lang.reflect.Method m = annoResourceFactory.findMethodForAnno(source.getClass(), annoClass);
            if (m != null) {
                value = m.invoke(source, (Object) null);
            } else {
                for (String nameProp : CTAG_PROP_NAMES) {
                    if (PropertyUtils.isReadable(source, nameProp)) {
                        Object oPropVal = PropertyUtils.getProperty(source, nameProp);
                        value = oPropVal;
                        break;
                    }
                }
            }
        } else {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            value = invoke(cm, eventRes, bout);
            // These methods will often write to output stream, so must provide one as alternative to returning a value
            if (value == null) { // probably means void return type, so use outputstream
                byte[] arr = bout.toByteArray();
                if (arr.length > 0) {
                    value = arr;
                }
            }
        }
        if (value != null) {
            if (value instanceof String) {
                return (String) value;
            } else if (value instanceof byte[]) {
                byte[] bytes = (byte[]) value;
                return new String(bytes, "UTF-8");
            } else if (value instanceof InputStream) {
                InputStream in = (InputStream) value;
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                IOUtils.copy(in, bout);
                return bout.toString("UTF-8");
            } else {
                return value.toString();
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception executing " + getClass() + " - " + source.getClass(), e);
    }
}

From source file:com.afeng.common.utils.reflection.MyBeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }//from  ww w .  j ava  2 s .  c om
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */
    {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        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(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

From source file:com.eryansky.common.utils.reflection.MyBeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }/*from ww  w  .  j a  v  a2s.  c o  m*/
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */
    {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        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(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (java.lang.IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

From source file:com.spectralogic.ds3contractcomparator.print.utils.HtmlRowGeneratorUtils.java

/**
 * Retrieves the unique property from the Ds3 object
 *///from w ww.  j a va  2  s  .  c o  m
public static <T> String getUniqueProperty(final T object) {
    if (object == null) {
        throw new IllegalArgumentException("Cannot get unique property from null object");
    }
    //Most Ds3 modules have a name parameter
    if (PropertyUtils.isReadable(object, "name")) {
        return "name";
    }
    //Used for Ds3ResponseCode
    if (PropertyUtils.isReadable(object, "code")) {
        return "code";
    }
    //Used for Ds3ResponseType
    if (PropertyUtils.isReadable(object, "type")) {
        return "type";
    }
    //Unknown unique property
    LOG.warn("Unknown unique property for class {}", object.getClass().toString());
    return "";
}

From source file:com.eryansky.common.utils.reflection.BeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }/*  w  ww. j a v  a 2 s .  c o m*/
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */ {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        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(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

From source file:com.ponysdk.core.export.util.PropertyUtil.java

public static Object getPropertyValue(final Object bean, final String propertyName)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Object propertyValue;//  w  w  w . j  a  va2 s .c  om
    if (PropertyUtils.isReadable(bean, propertyName)) {
        propertyValue = PropertyUtils.getProperty(bean, propertyName);
        if (propertyValue == null) {
            propertyValue = NA;
        }
    } else {
        propertyValue = NA;
    }
    return propertyValue;
}

From source file:ca.sqlpower.testutil.TestUtils.java

/**
 * Sets all the settable properties on the given target object which are not
 * in the given ignore set./*w ww.  j  ava2s.  com*/
 * <p>
 * TODO merge this with what is in Architect's TestUtils class. This was
 * originally refactored out of there.
 * 
 * @param target
 *            The object to change the properties of
 * @param propertiesToIgnore
 *            The properties of target not to modify or read
 * @return A Map describing the new values of all the non-ignored, readable
 *         properties in target.
 */
public static Map<String, Object> setAllInterestingProperties(Object target, Set<String> propertiesToIgnore,
        NewValueMaker valueMaker) throws Exception {

    PropertyDescriptor props[] = PropertyUtils.getPropertyDescriptors(target);
    for (int i = 0; i < props.length; i++) {
        Object oldVal = null;
        if (PropertyUtils.isReadable(target, props[i].getName()) && props[i].getReadMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {
            oldVal = PropertyUtils.getProperty(target, props[i].getName());
        }
        if (PropertyUtils.isWriteable(target, props[i].getName()) && props[i].getWriteMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {

            Object newVal = valueMaker.makeNewValue(props[i].getPropertyType(), oldVal, props[i].getName());

            System.out.println("Changing property \"" + props[i].getName() + "\" to \"" + newVal + "\"");
            PropertyUtils.setProperty(target, props[i].getName(), newVal);

        }
    }

    // read them all back at the end in case there were dependencies between properties
    return TestUtils.getAllInterestingProperties(target, propertiesToIgnore);
}

From source file:edu.northwestern.bioinformatics.studycalendar.domain.tools.hibernate.ScheduledActivityStateType.java

public Object getPropertyValue(Object component, int property) throws HibernateException {
    String name = getPropertyNames()[property];
    if (PropertyUtils.isReadable(component, name)) {
        try {//  w w w.  ja  v a 2 s.co m
            return PropertyUtils.getProperty(component, name);
        } catch (IllegalAccessException e) {
            throw new HibernateException("Failed to read property " + name + " from " + component, e);
        } catch (InvocationTargetException e) {
            throw new HibernateException("Failed to read property " + name + " from " + component, e);
        } catch (NoSuchMethodException e) {
            throw new HibernateException("Failed to read property " + name + " from " + component, e);
        }
    } else {
        return null;
    }
}

From source file:io.milton.http.annotated.CTagAnnotationHandler.java

public String execute(AnnoCollectionResource col) {
    Object source = col.getSource();
    try {//from w w  w.j  av  a2 s. co  m
        Object rawId = null;
        ControllerMethod cm = getBestMethod(source.getClass());
        if (cm == null) {
            // look for an annotation on the source itself
            java.lang.reflect.Method m = annoResourceFactory.findMethodForAnno(source.getClass(), annoClass);
            if (m != null) {
                rawId = m.invoke(source);
                if (log.isDebugEnabled()) {
                    log.debug("Got ctag from source object. ctag=" + rawId);
                }

            } else {
                for (String nameProp : CTAG_PROP_NAMES) {
                    if (PropertyUtils.isReadable(source, nameProp)) {
                        Object oPropVal = PropertyUtils.getProperty(source, nameProp);
                        rawId = oPropVal;
                        if (log.isDebugEnabled()) {
                            log.debug("Got ctag from bean property:" + nameProp + "  ctag=" + rawId);
                        }

                        break;
                    }
                }
                if (rawId == null) {
                    // last ditch effort, use latest mod date on the collection or any member
                    rawId = deriveCtag(col);
                    if (log.isInfoEnabled()) {
                        log.debug(
                                "Derived ctag from directory members. This is not recommended, you should implement an @CTag method. Ctag="
                                        + rawId);
                    }
                }

            }
        } else {
            rawId = cm.method.invoke(cm.controller, source);
            if (log.isDebugEnabled()) {
                log.debug("Got ctag from annotated method. ctag=" + rawId);
            }
        }
        if (rawId != null) {
            String s = rawId.toString();
            if (s.length() == 0) {
                log.warn("CTAG value is blank");
            }
            return s;
        } else {
            log.warn("CTAG value is null");
            return null;
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception executing " + getClass() + " - " + source.getClass(), e);
    }
}