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:org.eclipse.scada.da.core.VariantBeanHelper.java

public static Map<String, Variant> extractSafe(final Object source) {
    final Map<String, Variant> result = new HashMap<String, Variant>();

    BeanInfo bi;
    try {// www  .  j  a v  a2 s .  c om
        bi = Introspector.getBeanInfo(source.getClass());
    } catch (final IntrospectionException e) {
        return result;
    }

    for (final PropertyDescriptor pd : bi.getPropertyDescriptors()) {
        final Method m = pd.getReadMethod();
        if (m != null) {
            try {
                result.put(pd.getName(), Variant.valueOf(m.invoke(source)));
            } catch (final Exception e) {
                // ignore
            }
        }
    }
    return result;
}

From source file:org.psnively.scala.beans.ScalaBeanInfo.java

private static PropertyDescriptor[] initPropertyDescriptors(BeanInfo beanInfo) {
    Map<String, PropertyDescriptor> propertyDescriptors = new TreeMap<String, PropertyDescriptor>(
            new PropertyNameComparator());
    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        propertyDescriptors.put(pd.getName(), pd);
    }//from  w ww .  jav  a 2 s.c om

    for (MethodDescriptor md : beanInfo.getMethodDescriptors()) {
        Method method = md.getMethod();

        if (ReflectionUtils.isObjectMethod(method)) {
            continue;
        }
        if (isScalaSetter(method)) {
            addScalaSetter(propertyDescriptors, method);
        } else if (isScalaGetter(method)) {
            addScalaGetter(propertyDescriptors, method);
        }
    }
    return propertyDescriptors.values().toArray(new PropertyDescriptor[propertyDescriptors.size()]);
}

From source file:PropertyUtils.java

/**
 * Get map with property descriptors for the specified bean class
 *///from  w w  w .  j  a v  a 2s .  c o  m
private static Map getPropertyDescriptors(Class clazz) {
    HashMap map = (HashMap) descriptorCache.get(clazz);
    if (map == null) {
        BeanInfo beanInfo = null;
        try {
            beanInfo = Introspector.getBeanInfo(clazz);
        } catch (IntrospectionException e) {
            return Collections.EMPTY_MAP;
        }
        PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        if (descriptors == null)
            descriptors = new PropertyDescriptor[0];
        map = new HashMap(descriptors.length);
        for (int i = 0; i < descriptors.length; i++)
            map.put(descriptors[i].getName(), descriptors[i]);
        descriptorCache.put(clazz, map);
    }
    return map;
}

From source file:org.shaman.rpg.editor.dbviewer.TreeTableModelFactory.java

private static String getFieldName(Method method) {
    try {// w ww . j a v a 2 s  .  c o m
        Class<?> clazz = method.getDeclaringClass();
        BeanInfo info = Introspector.getBeanInfo(clazz);
        PropertyDescriptor[] props = info.getPropertyDescriptors();
        for (PropertyDescriptor pd : props) {
            if (method.equals(pd.getWriteMethod()) || method.equals(pd.getReadMethod())) {
                //               System.out.println(pd.getDisplayName());
                return pd.getName();
            }
        }
    } catch (Exception e) {
        LOG.log(Level.FINE, "unable to get field name for method " + method, e);
    }

    return method.getName();
}

From source file:org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl.java

/**
 * Get the PropertyDescriptor for aClass and aPropertyName
 *///w  w w.ja v a 2 s .c o m
protected static PropertyDescriptor findPropertyDescriptor(Class aClass, String aPropertyName) {
    BeanInfo info;
    PropertyDescriptor[] pd;
    PropertyDescriptor descriptor = null;

    try {
        info = Introspector.getBeanInfo(aClass);
        pd = info.getPropertyDescriptors();
        for (int i = 0; i < pd.length; i++) {
            if (pd[i].getName().equals(aPropertyName)) {
                descriptor = pd[i];
                break;
            }
        }
        if (descriptor == null) {
            /*
            * Daren Drummond:    Throw here so we are consistent
            *                with PersistentFieldDefaultImpl.
            */
            throw new MetadataException("Can't find property " + aPropertyName + " in " + aClass.getName());
        }
        return descriptor;
    } catch (IntrospectionException ex) {
        /*
        * Daren Drummond:    Throw here so we are consistent
        *                with PersistentFieldDefaultImpl.
        */
        throw new MetadataException("Can't find property " + aPropertyName + " in " + aClass.getName(), ex);
    }
}

From source file:cn.fql.utility.ClassUtility.java

/**
 * Export property desc of specified class
 *
 * @param beanClass specified class/*from   www  .  j  a va2s. c o  m*/
 * @return @see java.beans.BeanInfo#getPropertyDescriptors
 * @throws java.beans.IntrospectionException
 *          if there is exception occurs
 */
public static PropertyDescriptor[] exportPropertyDesc(Class beanClass) throws IntrospectionException {
    BeanInfo bi;
    PropertyDescriptor[] pds;
    bi = Introspector.getBeanInfo(beanClass);
    pds = bi.getPropertyDescriptors();
    return pds;
}

From source file:org.saiku.adhoc.utils.TemplateUtils.java

public static void mergeElementFormats(SaikuElementFormat source, SaikuElementFormat target) throws Exception {

    BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());

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

        if (descriptor.getWriteMethod() != null) {

            Object sourceValue = descriptor.getReadMethod().invoke(source);
            if (sourceValue != null) {
                descriptor.getWriteMethod().invoke(target, sourceValue);
            }//from   ww  w  .  j a v a2 s . co  m

        }
    }

}

From source file:org.openengsb.core.util.BeanUtilsExtended.java

/**
 * Analyzes the bean and returns a map containing the property-values. Works similar to
 * {@link BeanUtils#describe(Object)} but does not convert everything to strings.
 * //from  w  w w . ja  va  2  s  .  com
 * @throws IllegalArgumentException if the bean cannot be analyzed properly. Probably because some getter throw an
 *         Exception
 */
public static Map<String, Object> buildObjectAttributeMap(Object bean) throws IllegalArgumentException {
    BeanInfo beanInfo;
    try {
        beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class);
    } catch (IntrospectionException e1) {
        throw new IllegalArgumentException(e1);
    }
    Map<String, Object> result;
    result = Maps.newHashMap();
    for (PropertyDescriptor pDesc : beanInfo.getPropertyDescriptors()) {
        String name = pDesc.getName();
        Object propertyValue;
        try {
            propertyValue = pDesc.getReadMethod().invoke(bean);
        } catch (IllegalAccessException e) {
            // this should never happen since the Introspector only returns accessible read-methods
            LOGGER.error("WTF: got property descriptor with inaccessible read-method");
            throw new IllegalStateException(e);
        } catch (InvocationTargetException e) {
            ReflectionUtils.handleInvocationTargetException(e);
            throw new IllegalStateException("Should never get here");
        }
        if (propertyValue != null) {
            result.put(name, propertyValue);
        }
    }
    return result;
}

From source file:com.palantir.ptoss.util.Reflections.java

/**
 * Returns a {@link Function} that will read values from the named field from a passed object.
 * @param klass type to read values from
 * @param returnType return type of read field
 * @param getter name of the field//from w w w  .j av  a  2 s  .  co m
 * @return a {@link Function} object that, when applied to an instance of <code>klass</code>, returns the
 * of type <code>returnType</code> that resides in field <code>getter</code>
 */
public static <F, T> Function<F, T> getterFunction(final Class<F> klass, final Class<T> returnType,
        String getter) {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(klass);
        PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
        Method method = null;
        for (PropertyDescriptor descriptor : props) {
            if (descriptor.getName().equals(getter)) {
                method = descriptor.getReadMethod();
                break;
            }
        }
        if (method == null) {
            throw new IllegalStateException();
        }
        final Method readMethod = method;
        return new Function<F, T>() {
            public T apply(F from) {
                try {
                    return returnType.cast(readMethod.invoke(from));
                } catch (Exception e) {
                    Throwables.throwUncheckedException(e);
                    return null;
                }
            }
        };
    } catch (IntrospectionException e) {
        Throwables.throwUncheckedException(e);
        return null;
    }
}

From source file:org.opendaylight.controller.cluster.datastore.DatastoreContextIntrospector.java

/**
 * Introspects the DataStoreProperties interface that is generated from the DataStoreProperties
 * yang grouping. We use the bean Introspector to find the types of all the properties defined
 * in the interface (this is the type returned from the getter method). For each type, we find
 * the appropriate constructor that we will use.
 *//* w ww. j  a v a2  s.  co  m*/
private static void introspectDataStoreProperties() throws IntrospectionException {
    BeanInfo beanInfo = Introspector.getBeanInfo(DataStoreProperties.class);
    for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
        processDataStoreProperty(desc.getName(), desc.getPropertyType());
    }

    // Getter methods that return Boolean and start with "is" instead of "get" aren't recognized as
    // properties and thus aren't returned from getPropertyDescriptors. A getter starting with
    // "is" is only supported if it returns primitive boolean. So we'll check for these via
    // getMethodDescriptors.
    for (MethodDescriptor desc : beanInfo.getMethodDescriptors()) {
        String methodName = desc.getName();
        if (Boolean.class.equals(desc.getMethod().getReturnType()) && methodName.startsWith("is")) {
            String propertyName = WordUtils.uncapitalize(methodName.substring(2));
            processDataStoreProperty(propertyName, Boolean.class);
        }
    }
}