Example usage for java.beans BeanInfo getPropertyDescriptors

List of usage examples for java.beans BeanInfo getPropertyDescriptors

Introduction

In this page you can find the example usage for java.beans BeanInfo getPropertyDescriptors.

Prototype

PropertyDescriptor[] getPropertyDescriptors();

Source Link

Document

Returns descriptors for all properties of the bean.

Usage

From source file:com.cloudbees.plugins.credentials.matchers.BeanPropertyMatcher.java

/**
 * {@inheritDoc}/*  w w  w.  j av  a2  s  .c  om*/
 */
@Override
public boolean matches(@NonNull Credentials item) {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(item.getClass());
        for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
            if (name.equals(pd.getName())) {
                Method readMethod = pd.getReadMethod();
                if (readMethod == null) {
                    return false; // we cannot read it therefore it cannot be a match
                }
                try {
                    Object actual = readMethod.invoke(item);
                    return expected == null ? actual == null : expected.equals(actual);
                } catch (IllegalAccessException e) {
                    return false; // if we cannot access it then it's not a match
                } catch (InvocationTargetException e) {
                    return false; // if we cannot access it then it's not a match
                }
            }
        }
        return false; // if there is no corresponding property then it cannot be a match
    } catch (IntrospectionException e) {
        return false; // if we cannot introspect it then it cannot be a match
    }
}

From source file:org.onebusaway.nyc.presentation.impl.NycConfigurationServiceImpl.java

private ConfigurationBean loadSettings() {

    if (_path == null || !_path.exists())
        return new ConfigurationBean();

    try {// w  ww  .  j a  v  a  2 s  . c o m

        Properties properties = new Properties();
        properties.load(new FileReader(_path));

        ConfigurationBean bean = new ConfigurationBean();
        BeanInfo beanInfo = Introspector.getBeanInfo(ConfigurationBean.class);

        for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
            String name = desc.getName();
            Object value = properties.getProperty(name);
            if (value != null) {
                Converter converter = ConvertUtils.lookup(desc.getPropertyType());
                value = converter.convert(desc.getPropertyType(), value);
                Method m = desc.getWriteMethod();
                m.invoke(bean, value);
            }
        }

        return bean;
    } catch (Exception ex) {
        throw new IllegalStateException("error loading configuration from properties file " + _path, ex);
    }
}

From source file:org.onebusaway.nyc.presentation.impl.NycConfigurationServiceImpl.java

private void saveSettings(ConfigurationBean bean) {

    if (_path == null)
        return;//from w w w .  ja v a 2 s .  c  o m

    try {

        Properties properties = new Properties();
        BeanInfo beanInfo = Introspector.getBeanInfo(ConfigurationBean.class);

        for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {

            String name = desc.getName();

            if (name.equals("class"))
                continue;

            Method m = desc.getReadMethod();
            Object value = m.invoke(bean);
            if (value != null) {
                properties.setProperty(name, value.toString());
            }
        }

        properties.store(new FileWriter(_path), "onebusaway-nyc configuration");
    } catch (Exception ex) {
        throw new IllegalStateException("error saving configuration to properties file " + _path, ex);
    }
}

From source file:org.geotools.filter.function.PropertyExistsFunction.java

/**
 * @return {@link Boolean#TRUE} if the Class of the object passed as
 *         argument defines a property names as the property name passed as
 *         this function argument, following the standard Java Beans naming
 *         conventions for getters. {@link Boolean#FALSE} otherwise.
 *///w  w  w .j  ava  2 s.c o m
public Object evaluate(Object bean) {
    if (bean instanceof SimpleFeature) {
        return evaluate((SimpleFeature) bean);
    }

    final String propName = getPropertyName();

    try {
        Class type = bean.getClass();
        //quick 1
        //            try {
        //                String getName = "get"+propName.substring(0,1).toUpperCase()+propName.substring(1);
        //                if (type.getMethod(getName, new Class[0]) != null) {
        //                    return true;
        //                }
        //            } catch (Exception ignore) {
        //            }
        //            // quick 2
        //            try {
        //                String isName = "is"+propName.substring(0,1).toUpperCase()+propName.substring(1);
        //                if (type.getMethod(isName, new Class[0]) != null) {
        //                    return true;
        //                }
        //            } catch (Exception ignore) {
        //            }
        // okay go for real
        BeanInfo info = Introspector.getBeanInfo(type);
        for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
            if (descriptor.getName().equals(propName)) {
                if (descriptor.getReadMethod() != null) {
                    return true;
                } else {
                    return false; // property found but not writable
                }
            }
        }
        //PropertyUtils.getProperty(bean, propName);
        //return true;
    } catch (IntrospectionException ignore) {
    }
    return false;
}

From source file:org.bibsonomy.model.util.BibTexUtils.java

/**
 * return a bibtex string representation of the given bibtex object
 * // w  w w  .  jav  a 2s .com
 * @param bib - a bibtex object
 * @param mode - the serializing mode (parse misc fields or include misc fields as they are)
 * @return String bibtexString
 * 
 * TODO use BibTex.DEFAULT_OPENBRACKET etc.
 * 
 */
public static String toBibtexString(final BibTex bib, SerializeBibtexMode mode) {
    try {
        final BeanInfo bi = Introspector.getBeanInfo(bib.getClass());

        /*
         * start with entrytype and key
         */
        final StringBuilder buffer = new StringBuilder(
                "@" + bib.getEntrytype() + "{" + bib.getBibtexKey() + ",\n");

        /*
         * append all other fields
         */
        for (final PropertyDescriptor d : bi.getPropertyDescriptors()) {
            final Method getter = d.getReadMethod();
            // loop over all String attributes
            final Object o = getter.invoke(bib, (Object[]) null);
            if (String.class.equals(d.getPropertyType()) && o != null
                    && !EXCLUDE_FIELDS.contains(d.getName())) {

                /*
                 * Strings containing whitespace give empty fields ... we ignore them 
                 */
                String value = ((String) o);
                if (present(value)) {
                    if (!NUMERIC_PATTERN.matcher(value).matches()) {
                        value = DEFAULT_OPENING_BRACKET + value + DEFAULT_CLOSING_BRACKET;
                    }
                    buffer.append("  " + d.getName().toLowerCase() + " = " + value + ",\n");
                }
            }
        }
        /*
         * process miscFields map, if present
         */
        if (present(bib.getMiscFields())) {
            if (mode.equals(SerializeBibtexMode.PARSED_MISCFIELDS) && !bib.isMiscFieldParsed()) {
                // parse misc field, if not yet done
                bib.parseMiscField();
            }
            buffer.append(serializeMiscFields(bib.getMiscFields(), true));
        }

        /*
         * include plain misc fields if desired
         */
        if (mode.equals(SerializeBibtexMode.PLAIN_MISCFIELDS) && present(bib.getMisc())) {
            buffer.append("  " + bib.getMisc() + ",\n");
        }
        /*
         * add month
         */
        final String month = bib.getMonth();
        if (present(month)) {
            // we don't add {}, this is done by getMonth(), if necessary
            buffer.append("  month = " + getMonth(month) + ",\n");
        }
        /*
         * add abstract
         */
        final String bibAbstract = bib.getAbstract();
        if (present(bibAbstract)) {
            buffer.append("  abstract = {" + bibAbstract + "},\n");
        }
        /*
         * remove last comma
         */
        buffer.delete(buffer.lastIndexOf(","), buffer.length());
        buffer.append("\n}");

        return buffer.toString();

    } catch (IntrospectionException ex) {
        ex.printStackTrace();
    } catch (InvocationTargetException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.heren.turtle.server.utils.TransUtils.java

public Map<String, Object> beanToMap(Object obj) {
    Map<String, Object> resultMap = new HashMap<>();
    if (obj == null) {
        return null;
    }/*from  w  w w  . j a  v a 2 s  . com*/
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                resultMap.put(key, value);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("transBean2Map Error " + e);
    }
    return resultMap;
}

From source file:com.heren.turtle.server.utils.TransUtils.java

public Object toTransObject(Object obj) {
    if (obj == null)
        return null;
    try {/*from ww  w.  ja  va  2 s.  c  om*/
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                if (value instanceof String) {
                    Method setter = property.getWriteMethod();
                    Object newValue = toTrans(value);
                    setter.invoke(obj, newValue);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:com.heren.turtle.server.utils.TransUtils.java

public Object U2IObject(Object obj) {
    Object newObj = new Object();
    if (obj == null) {
        return null;
    }/*from   ww  w . ja v a  2  s. co  m*/
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                if (value instanceof String) {
                    Method setter = property.getWriteMethod();
                    Object newValue = U2I((String) value);
                    setter.invoke(obj, newValue);
                }
            }
        }
        newObj = obj;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return newObj;
}

From source file:net.cpollet.jixture.hibernate3.helper.Hibernate3MappingDefinitionHolder.java

private Column getColumnFromGetter(Field field) {
    BeanInfo beanInfo = getBeanInfo(field);

    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        if (isGetterForField(pd, field)) {
            return pd.getReadMethod().getAnnotation(Column.class);
        }/*from  w w  w .  jav  a 2  s  .  co m*/
    }

    return null;
}

From source file:org.dphibernate.serialization.HibernateDeserializer.java

private Object readBean(Object obj) {
    try {// ww w.  ja  va  2  s . c o m
        BeanInfo info = Introspector.getBeanInfo(obj.getClass());
        for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
            String propName = pd.getName();
            if (!"class".equals(propName) && !"annotations".equals(propName)
                    && !"hibernateLazyInitializer".equals(propName)) {
                Object val = pd.getReadMethod().invoke(obj, null);
                if (val != null) {
                    Object newVal = translate(val, pd.getPropertyType());
                    try {
                        Method writeMethod = pd.getWriteMethod();
                        if (writeMethod != null) {
                            writeMethod.invoke(obj, newVal);
                        }
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    } catch (NullPointerException npe) {
                        throw npe;
                    }

                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
    return obj;
}