Example usage for java.beans PropertyDescriptor getReadMethod

List of usage examples for java.beans PropertyDescriptor getReadMethod

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor getReadMethod.

Prototype

public synchronized Method getReadMethod() 

Source Link

Document

Gets the method that should be used to read the property value.

Usage

From source file:org.enerj.apache.commons.beanutils.PropertyUtilsBean.java

/**
 * <p>Return <code>true</code> if the specified property name identifies
 * a readable property on the specified bean; otherwise, return
 * <code>false</code>.//from w w w. jav a2  s.c o m
 *
 * @param bean Bean to be examined (may be a {@link DynaBean}
 * @param name Property name to be evaluated
 *
 * @exception IllegalArgumentException if <code>bean</code>
 *  or <code>name</code> is <code>null</code>
 *
 * @since BeanUtils 1.6
 */
public boolean isReadable(Object bean, String name) {

    // Validate method parameters
    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }

    // Return the requested result
    if (bean instanceof DynaBean) {
        // All DynaBean properties are readable
        return (((DynaBean) bean).getDynaClass().getDynaProperty(name) != null);
    } else {
        try {
            PropertyDescriptor desc = getPropertyDescriptor(bean, name);
            if (desc != null) {
                Method readMethod = desc.getReadMethod();
                if ((readMethod == null) && (desc instanceof IndexedPropertyDescriptor)) {
                    readMethod = ((IndexedPropertyDescriptor) desc).getIndexedReadMethod();
                }
                return (readMethod != null);
            } else {
                return (false);
            }
        } catch (IllegalAccessException e) {
            return (false);
        } catch (InvocationTargetException e) {
            return (false);
        } catch (NoSuchMethodException e) {
            return (false);
        }
    }

}

From source file:BeanArrayList.java

/**
 * This method does a string match on values of a property.
 * @param propertyName String value containing the name of the property to match.
 * @param match Value to search for. This is a case-insensitive value that takes % as a multiple character wildcard value.
 * @throws java.lang.IllegalAccessException reflection exception
 * @throws java.beans.IntrospectionException reflection exception
 * @throws java.lang.reflect.InvocationTargetException reflection exception
 * @return a new BeanArrayList filtered on the specified property
 *//*from   ww w  .  jav a2 s  .  co m*/
public BeanArrayList<T> getFilteredStringMatch(String propertyName, String match)
        throws java.lang.IllegalAccessException, java.beans.IntrospectionException,
        java.lang.reflect.InvocationTargetException {
    HashMap cache = new HashMap();
    String currentClass = "";
    PropertyDescriptor pd = null;
    BeanArrayList<T> results = new BeanArrayList<T>();

    for (int i = 0; i < this.size(); i++) {
        T o = this.get(i);

        if (!currentClass.equals(o.getClass().getName())) {
            pd = (PropertyDescriptor) cache.get(o.getClass().getName());

            if (pd == null) {
                PropertyDescriptor[] pds = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();
                boolean foundProperty = false;

                for (int pdi = 0; (pdi < pds.length) && !foundProperty; pdi++) {
                    if (pds[pdi].getName().equals(propertyName)) {
                        pd = pds[pdi];
                        cache.put(o.getClass().getName(), pd);
                        foundProperty = true;
                    }
                }
            }
        }

        String value = pd.getReadMethod().invoke(o).toString().toLowerCase();
        StringTokenizer st = new StringTokenizer(match.toLowerCase(), "%");
        boolean isMatch = true;
        int matchIndex = 0;

        while (st.hasMoreTokens() && isMatch) {
            String tk = st.nextToken();

            if (value.indexOf(tk, matchIndex) == -1) {
                isMatch = false;
            } else {
                matchIndex = value.indexOf(tk, matchIndex) + tk.length();
            }
        }

        if (isMatch) {
            results.add(o);
        }
    }

    return results;
}

From source file:BeanArrayList.java

/**
 * Filters a property using the Comparable.compareTo() on the porperty to
 * test for a range/*from   w  w w  .ja v a 2s.com*/
 * @param propertyName property to filter on
 * @param inclusive include the values of the range limiters
 * @param fromValue low range value
 * @param toValue high range value
 * @throws java.lang.IllegalAccessException reflection exception
 * @throws java.beans.IntrospectionException reflection exception
 * @throws java.lang.reflect.InvocationTargetException reflection exception
 * @return new BeanArrayList filtered on the range
 */
public BeanArrayList<T> getFiltered(String propertyName, boolean inclusive, Comparable fromValue,
        Comparable toValue) throws java.lang.IllegalAccessException, java.beans.IntrospectionException,
        java.lang.reflect.InvocationTargetException {
    HashMap cache = new HashMap();
    String currentClass = "";
    PropertyDescriptor pd = null;
    BeanArrayList<T> results = new BeanArrayList<T>();

    for (int i = 0; i < this.size(); i++) {
        T o = this.get(i);

        if (!currentClass.equals(o.getClass().getName())) {
            pd = (PropertyDescriptor) cache.get(o.getClass().getName());

            if (pd == null) {
                PropertyDescriptor[] pds = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();
                boolean foundProperty = false;

                for (int pdi = 0; (pdi < pds.length) && !foundProperty; pdi++) {
                    if (pds[pdi].getName().equals(propertyName)) {
                        pd = pds[pdi];
                        cache.put(o.getClass().getName(), pd);
                        foundProperty = true;
                    }
                }
            }
        }

        Comparable value = (Comparable) pd.getReadMethod().invoke(o);

        if ((value.compareTo(fromValue) > 0) && (value.compareTo(toValue) < 0)) {
            results.add(o);
        } else if (inclusive && ((value.compareTo(fromValue) == 0) || (value.compareTo(toValue) == 0))) {
            results.add(o);
        }
    }

    return results;
}

From source file:org.usergrid.persistence.Schema.java

public Object getEntityProperty(Entity entity, String property) {
    PropertyDescriptor descriptor = getDescriptorForEntityProperty(entity.getClass(), property);
    if (descriptor != null) {
        try {/*from  www.  j  a v a 2s .  co m*/
            return descriptor.getReadMethod().invoke(entity);
        } catch (Exception e) {
            logger.error("Unable to get entity property " + property, e);
        }
        return null;
    }
    Map<String, Object> properties = entity.getDynamicProperties();
    if (properties != null) {
        return properties.get(property);
    }
    return null;
}

From source file:BeanVector.java

/**
 * This method does a string match on values of a property.
 * @param propertyName String value containing the name of the property to match.
 * @param match Value to search for. This is a case-insensitive value that takes % as a multiple character wildcard value.
 * @throws java.lang.IllegalAccessException reflection exception
 * @throws java.beans.IntrospectionException reflection exception
 * @throws java.lang.reflect.InvocationTargetException reflection exception
 * @return a new BeanVector filtered on the specified property
 *///from  ww w  .  jav  a  2s  .c o m
public BeanVector<T> getFilteredStringMatch(String propertyName, String match)
        throws java.lang.IllegalAccessException, java.beans.IntrospectionException,
        java.lang.reflect.InvocationTargetException {
    Hashtable cache = new Hashtable();
    String currentClass = "";
    PropertyDescriptor pd = null;
    BeanVector<T> results = new BeanVector<T>();

    for (int i = 0; i < this.size(); i++) {
        T o = this.elementAt(i);

        if (!currentClass.equals(o.getClass().getName())) {
            pd = (PropertyDescriptor) cache.get(o.getClass().getName());

            if (pd == null) {
                PropertyDescriptor[] pds = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();
                boolean foundProperty = false;

                for (int pdi = 0; (pdi < pds.length) && !foundProperty; pdi++) {
                    if (pds[pdi].getName().equals(propertyName)) {
                        pd = pds[pdi];
                        cache.put(o.getClass().getName(), pd);
                        foundProperty = true;
                    }
                }
            }
        }

        String value = pd.getReadMethod().invoke(o).toString().toLowerCase();
        StringTokenizer st = new StringTokenizer(match.toLowerCase(), "%");
        boolean isMatch = true;
        int matchIndex = 0;

        while (st.hasMoreTokens() && isMatch) {
            String tk = st.nextToken();

            if (value.indexOf(tk, matchIndex) == -1) {
                isMatch = false;
            } else {
                matchIndex = value.indexOf(tk, matchIndex) + tk.length();
            }
        }

        if (isMatch) {
            results.add(o);
        }
    }

    return results;
}

From source file:BeanVector.java

/**
 * Filters a property using the Comparable.compareTo() on the porperty to
 * test for a range// w w w  .  j av a2s  .c o  m
 * @param propertyName property to filter on
 * @param inclusive include the values of the range limiters
 * @param fromValue low range value
 * @param toValue high range value
 * @throws java.lang.IllegalAccessException reflection exception
 * @throws java.beans.IntrospectionException reflection exception
 * @throws java.lang.reflect.InvocationTargetException reflection exception
 * @return new BeanVector filtered on the range
 */
public BeanVector<T> getFiltered(String propertyName, boolean inclusive, Comparable fromValue,
        Comparable toValue) throws java.lang.IllegalAccessException, java.beans.IntrospectionException,
        java.lang.reflect.InvocationTargetException {
    Hashtable cache = new Hashtable();
    String currentClass = "";
    PropertyDescriptor pd = null;
    BeanVector<T> results = new BeanVector<T>();

    for (int i = 0; i < this.size(); i++) {
        T o = this.elementAt(i);

        if (!currentClass.equals(o.getClass().getName())) {
            pd = (PropertyDescriptor) cache.get(o.getClass().getName());

            if (pd == null) {
                PropertyDescriptor[] pds = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();
                boolean foundProperty = false;

                for (int pdi = 0; (pdi < pds.length) && !foundProperty; pdi++) {
                    if (pds[pdi].getName().equals(propertyName)) {
                        pd = pds[pdi];
                        cache.put(o.getClass().getName(), pd);
                        foundProperty = true;
                    }
                }
            }
        }

        Comparable value = (Comparable) pd.getReadMethod().invoke(o);

        if ((value.compareTo(fromValue) > 0) && (value.compareTo(toValue) < 0)) {
            results.add(o);
        } else if (inclusive && ((value.compareTo(fromValue) == 0) || (value.compareTo(toValue) == 0))) {
            results.add(o);
        }
    }

    return results;
}

From source file:org.schemaspy.Config.java

/**
 * Get the value of the specified parameter.
 * Used for properties that are common to most db's, but aren't required.
 *
 * @param paramName/* w w w.  ja v  a2 s .c om*/
 * @return
 */
public String getParam(String paramName) {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(Config.class);
        PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < props.length; ++i) {
            PropertyDescriptor prop = props[i];
            if (prop.getName().equalsIgnoreCase(paramName)) {
                Object result = prop.getReadMethod().invoke(this, (Object[]) null);
                return result == null ? null : result.toString();
            }
        }
    } catch (Exception failed) {
        failed.printStackTrace();
    }

    return null;
}

From source file:org.agnitas.service.impl.NewImportWizardServiceImpl.java

protected Node createNode(Object context, String name) throws Exception {
    Class type = null;//from www. j av  a2  s. c om

    // If the name represents a JavaBean property of the current context
    // then we derive the type from that...
    PropertyDescriptor propertyDescriptor = Toolkit.getPropertyDescriptor(context, name);
    if (propertyDescriptor != null) {
        type = propertyDescriptor.getPropertyType();
    } else if (context != null && !classMap.containsKey(name)) {
        return new FieldNode(context, name);
    } else {
        // ... otherwise we look for a named type alias
        type = classMap.get(name);
    }

    if (type == null) {
        throw new IllegalArgumentException(
                String.format("No type mapping could be found or derived: name=%s", name));
    }

    // If there's a format for the node's type then we go with that...
    if (type.isAssignableFrom(String.class) || type.isAssignableFrom(Map.class)) {
        return new FieldNode(context, name);
    }

    // ...otherwise we've got some sort of mutable node value.
    Object instance;
    // If the node corresponds to a JavaBean property which already has a
    // value then we mutate that...
    if (propertyDescriptor != null) {
        instance = propertyDescriptor.getReadMethod().invoke(context);
        if (instance == null) {
            instance = type.newInstance();
            propertyDescriptor.getWriteMethod().invoke(context, instance);
        }
    } else {
        // ...otherwise we're just dealing with some non-property instance
        instance = type.newInstance();
    }

    // Collections are handled in a special way - all properties are
    // basically their values
    if (instance instanceof Collection) {
        return new CollectionNode(name, (Collection) instance);
    }
    // Everything else is assumed to be a JavaBean
    return new JavaBeanNode(instance, name);
}

From source file:BeanArrayList.java

/**
 * This method returns the index of an object representing the
 * mode value of a property name.//from   w  w  w  .  j av a 2  s.c  o  m
 * @param propertyName String value of the property name to calculate.
 * @throws java.lang.IllegalAccessException reflection exception
 * @throws java.beans.IntrospectionException reflection exception
 * @throws java.lang.reflect.InvocationTargetException reflection exception
 * @return int value of the mode index
 */
public int getModeIndex(String propertyName) throws java.lang.IllegalAccessException,
        java.beans.IntrospectionException, java.lang.reflect.InvocationTargetException {
    int index = -1;
    int max = 0;
    int count = 0;
    Object o = null;
    Object hold = null;
    HashMap cache = new HashMap();
    String currentClass = "";
    PropertyDescriptor pd = null;
    BeanArrayList bv = new BeanArrayList(this.size(), 0, this);
    bv.sortOnProperty(propertyName);

    for (int i = 0; i < bv.size(); i++) {
        if (!currentClass.equals(bv.get(i).getClass().getName())) {
            pd = (PropertyDescriptor) cache.get(bv.get(i).getClass().getName());

            if (pd == null) {
                PropertyDescriptor[] pds = Introspector.getBeanInfo(bv.get(i).getClass())
                        .getPropertyDescriptors();
                boolean foundProperty = false;

                for (int pdi = 0; (pdi < pds.length) && !foundProperty; pdi++) {
                    if (pds[pdi].getName().equals(propertyName)) {
                        pd = pds[pdi];
                        cache.put(bv.get(i).getClass().getName(), pd);
                        foundProperty = true;
                    }
                }
            }
        }

        if (hold == null) {
            hold = pd.getReadMethod().invoke(bv.get(i));
        } else {
            o = pd.getReadMethod().invoke(bv.get(i));

            if ((o != null) && o.equals(hold)) {
                count++;

                if (count > max) {
                    max = count;
                    index = this.indexOf(bv.get(i));
                }
            } else {
                count = 1;
            }

            hold = o;
        }
    }

    return index;
}

From source file:org.eclipse.wb.tests.designer.core.util.reflect.ReflectionUtilsTest.java

/**
 * Test for {@link ReflectionUtils#getPropertyDescriptors(BeanInfo, Class)}.<br>
 * Public getter and protected setter./*from   w w  w .  j  a  v  a 2s. com*/
 */
public void test_getPropertyDescriptors_publicGetterProtectedSetter() throws Exception {
    @SuppressWarnings("unused")
    class MyButton extends JPanel {
        private static final long serialVersionUID = 0L;

        public String getTitle() {
            return null;
        }

        protected void setTitle(String s) {
        }
    }
    // check properties
    {
        Set<String> names = getPropertyDescriptorNames(MyButton.class).keySet();
        assertThat(names).contains("title");
        assertThat(names).excludes("title(java.lang.String)");
    }
    // both getter and setter should be accessible
    {
        PropertyDescriptor descriptor = getPropertyDescriptorNames(MyButton.class).get("title");
        assertNotNull(descriptor);
        assertNotNull(descriptor.getReadMethod());
        assertNotNull(descriptor.getWriteMethod());
    }
}