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.apache.myfaces.el.PropertyResolverImpl.java

public static PropertyDescriptor getPropertyDescriptor(BeanInfo beanInfo, String propertyName) {
    PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors();

    if (propDescriptors != null) {
        // TODO: cache this in classLoader safe way
        for (int i = 0, len = propDescriptors.length; i < len; i++) {
            if (propDescriptors[i].getName().equals(propertyName))
                return propDescriptors[i];
        }//  w w  w .j a  v a  2  s .  c o  m
    }

    throw new PropertyNotFoundException(
            "Bean: " + beanInfo.getBeanDescriptor().getBeanClass().getName() + ", property: " + propertyName);
}

From source file:org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.BeanSupport.java

/**
 * @return {@link PropertyDescriptor} properties for given bean {@link Class}.
 *///from ww  w  .j a  v  a 2  s.  com
public static List<PropertyDescriptor> getPropertyDescriptors(Class<?> beanClass) throws Exception {
    List<PropertyDescriptor> descriptors = Lists.newArrayList();
    // handle interfaces
    if (beanClass.isInterface() || Modifier.isAbstract(beanClass.getModifiers())) {
        List<Class<?>> interfaces = CoreUtils.cast(ClassUtils.getAllInterfaces(beanClass));
        for (Class<?> i : interfaces) {
            BeanInfo beanInfo = Introspector.getBeanInfo(i);
            addDescriptors(descriptors, beanInfo.getPropertyDescriptors());
        }
    }
    // handle bean
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    addDescriptors(descriptors, beanInfo.getPropertyDescriptors());
    //
    return descriptors;
}

From source file:org.jaffa.qm.util.PropertyFilter.java

private static void getFieldList(Class clazz, List<String> fieldList, String prefix, Deque<Class> classStack)
        throws IntrospectionException {
    //To avoid recursion, bail out if the input Class has already been introspected
    if (classStack.contains(clazz)) {
        if (log.isDebugEnabled())
            log.debug("Fields from " + clazz + " prefixed by " + prefix
                    + " will be ignored, since the class has already been introspected as per the stack "
                    + classStack);/*w w  w . j  av  a2 s.  c o  m*/
        return;
    } else
        classStack.addFirst(clazz);

    //Introspect the input Class
    BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
    if (beanInfo != null) {
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        if (pds != null) {
            for (PropertyDescriptor pd : pds) {
                if (pd.getReadMethod() != null && pd.getWriteMethod() != null) {
                    String name = pd.getName();
                    String qualifieldName = prefix == null || prefix.length() == 0 ? name : prefix + '.' + name;
                    Class type = pd.getPropertyType();
                    if (type.isArray())
                        type = type.getComponentType();
                    if (type == String.class || type == Boolean.class || Number.class.isAssignableFrom(type)
                            || IDateBase.class.isAssignableFrom(type) || Currency.class.isAssignableFrom(type)
                            || type.isPrimitive() || type.isEnum())
                        fieldList.add(qualifieldName);
                    else
                        getFieldList(type, fieldList, qualifieldName, classStack);
                }
            }
        }
    }

    classStack.removeFirst();
}

From source file:com.espertech.esper.event.bean.PropertyHelper.java

/**
 * Using the Java Introspector class the method returns the property descriptors obtained through introspection.
 * @param clazz to introspect/*from  ww w  .  ja v a  2s . c  o m*/
 * @return array of property descriptors
 */
protected static PropertyDescriptor[] introspect(Class clazz) {
    BeanInfo beanInfo;

    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException e) {
        return (new PropertyDescriptor[0]);
    }

    return beanInfo.getPropertyDescriptors();
}

From source file:corner.util.ognl.OgnlUtil.java

/**
 * Copies the properties in the object "from" and sets them in the object "to"
 * using specified type converter, or {@link com.opensymphony.xwork.util.CornerTypeConverter} if none is specified.
 *
 * @param from the source object/*from   w  ww  .j  ava  2  s  . com*/
 * @param to the target object
 * @param context the action context we're running under
 */
public static void copy(Object from, Object to, Map context) {
    Map contextFrom = Ognl.createDefaultContext(from);

    Map contextTo = Ognl.createDefaultContext(to);

    BeanInfo beanInfoFrom = null;

    try {
        beanInfoFrom = Introspector.getBeanInfo(from.getClass(), Object.class);
    } catch (IntrospectionException e) {
        log.error("An error occured", e);

        return;
    }

    PropertyDescriptor[] pds = beanInfoFrom.getPropertyDescriptors();

    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];

        try {
            Object expr = compile(pd.getName());
            Object value = Ognl.getValue(expr, contextFrom, from);
            Ognl.setValue(expr, contextTo, to, value);
        } catch (OgnlException e) {
            // ignore, this is OK
        }
    }
}

From source file:org.bibsonomy.util.ObjectUtils.java

/**
 * copies all property values (all properties that have a getter and setter) from the source to the target
 * /* ww w . j  ava2 s.co m*/
 * @param <T> 
 * @param source the source object
 * @param target the target object
 */
public static <T> void copyPropertyValues(final T source, final T target) {
    try {
        final BeanInfo biSource = Introspector.getBeanInfo(source.getClass());
        final BeanInfo biTarget = Introspector.getBeanInfo(target.getClass());

        /* 
         * source can be a subtype of target
         * to ensure that a setter of the subtype isn't called choose the array
         * that contains less properties
         */
        final PropertyDescriptor[] sourceProperties = biSource.getPropertyDescriptors();
        final PropertyDescriptor[] targetProperties = biTarget.getPropertyDescriptors();
        final PropertyDescriptor[] copyProperties = sourceProperties.length > targetProperties.length
                ? targetProperties
                : sourceProperties;

        /*
         * loop through all properties
         */
        for (final PropertyDescriptor d : copyProperties) {
            final Method getter = d.getReadMethod();
            final Method setter = d.getWriteMethod();

            if (present(getter) && present(setter)) {
                // get the value from the source
                final Object value = getter.invoke(source, (Object[]) null);
                // and set in in the target
                setter.invoke(target, value);
            }
        }
    } catch (Exception ex) {
        log.error("error while copying property values from an object " + source.getClass() + " to an object "
                + target.getClass(), ex);
    }
}

From source file:org.bibsonomy.layout.util.JabRefModelConverter.java

/**
 * Convert a JabRef BibtexEntry into a BibSonomy post
 * // ww  w .j a  v a2s . c  om
 * @param entry
 * @return
 */
public static Post<? extends Resource> convertEntry(final BibtexEntry entry) {

    try {
        final Post<BibTex> post = new Post<BibTex>();
        final BibTex bibtex = new BibTex();
        post.setResource(bibtex);

        final List<String> knownFields = new ArrayList<String>();

        final BeanInfo info = Introspector.getBeanInfo(bibtex.getClass());
        final PropertyDescriptor[] descriptors = info.getPropertyDescriptors();

        bibtex.setMisc("");

        // set all known properties of the BibTex
        for (final PropertyDescriptor pd : descriptors)
            if (present(entry.getField((pd.getName().toLowerCase())))
                    && !JabRefModelConverter.EXCLUDE_FIELDS.contains(pd.getName().toLowerCase())) {
                pd.getWriteMethod().invoke(bibtex, entry.getField(pd.getName().toLowerCase()));
                knownFields.add(pd.getName());
            }

        // Add not known Properties to misc
        for (final String field : entry.getAllFields())
            if (!knownFields.contains(field) && !JabRefModelConverter.EXCLUDE_FIELDS.contains(field))
                bibtex.addMiscField(field, entry.getField(field));

        bibtex.serializeMiscFields();

        // set the key
        bibtex.setBibtexKey(entry.getCiteKey());
        bibtex.setEntrytype(entry.getType().getName().toLowerCase());

        // set the date of the post
        final String timestamp = entry.getField("timestamp");
        if (present(timestamp))
            post.setDate(sdf.parse(timestamp));

        final String abstractt = entry.getField("abstract");
        if (present(abstractt))
            bibtex.setAbstract(abstractt);

        final String keywords = entry.getField("keywords");
        if (present(keywords))
            post.setTags(TagUtils.parse(keywords.replaceAll(jabRefKeywordSeparator, " ")));

        // Set the groups
        if (present(entry.getField("groups"))) {

            final String[] groupsArray = entry.getField("groups").split(" ");
            final Set<Group> groups = new HashSet<Group>();

            for (final String group : groupsArray)
                groups.add(new Group(group));

            post.setGroups(groups);
        }

        final String description = entry.getField("description");
        if (present(description))
            post.setDescription(description);

        final String comment = entry.getField("comment");
        if (present(comment))
            post.setDescription(comment);

        final String month = entry.getField("month");
        if (present(month))
            bibtex.setMonth(month);

        return post;

    } catch (final Exception e) {
        System.out.println(e.getStackTrace());
        log.debug("Could not convert JabRef entry into BibSonomy post.", e);
    }

    return null;
}

From source file:ReflectUtil.java

/**
 * Fetches the property descriptor for the named property of the supplied class. To
 * speed things up a cache is maintained of propertyName to PropertyDescriptor for
 * each class used with this method.  If there is no property with the specified name,
 * returns null.//from   w w w  . ja v  a 2 s. co m
 *
 * @param clazz the class who's properties to examine
 * @param property the String name of the property to look for
 * @return the PropertyDescriptor or null if none is found with a matching name
 */
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String property) {
    Map<String, PropertyDescriptor> pds = propertyDescriptors.get(clazz);
    if (pds == null) {
        try {
            BeanInfo info = Introspector.getBeanInfo(clazz);
            PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
            pds = new HashMap<String, PropertyDescriptor>();

            for (PropertyDescriptor descriptor : descriptors) {
                pds.put(descriptor.getName(), descriptor);
            }

            propertyDescriptors.put(clazz, pds);
        } catch (IntrospectionException ie) {
            throw new RuntimeException("Could not examine class '" + clazz.getName()
                    + "' using Introspector.getBeanInfo() to determine property information.", ie);
        }
    }

    return pds.get(property);
}

From source file:org.bibsonomy.plugin.jabref.util.JabRefModelConverter.java

protected static void copyStringProperties(BibtexEntry entry, BibTex bibtex) throws IntrospectionException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    /*// w w w  . j  a va2s.  co  m
     * we use introspection to get all fields ...
     */
    final BeanInfo info = Introspector.getBeanInfo(bibtex.getClass());
    final PropertyDescriptor[] descriptors = info.getPropertyDescriptors();

    /*
     * iterate over all properties
     */
    for (final PropertyDescriptor pd : descriptors) {

        final Method getter = pd.getReadMethod();

        // loop over all String attributes
        final Object o = getter.invoke(bibtex, (Object[]) null);

        if (String.class.equals(pd.getPropertyType()) && (o != null)
                && !JabRefModelConverter.EXCLUDE_FIELDS.contains(pd.getName())) {
            final String value = ((String) o);
            if (present(value)) {
                entry.setField(pd.getName().toLowerCase(), value);
            }
        }
    }
}

From source file:com.krm.dbaudit.common.utils.Collections3.java

/** 
 *  Map  JavaBean /*from w w w.  j  a v a 2  s .co  m*/
 * @param type ? 
 * @param map ? map 
 * @return ? JavaBean  
 * @throws IntrospectionException ? 
 * @throws IllegalAccessException  JavaBean  
 * @throws InstantiationException  JavaBean  
 * @throws InvocationTargetException  setter  
 */
public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException,
        InstantiationException, InvocationTargetException {
    BeanInfo beanInfo = Introspector.getBeanInfo(type); // ?   
    Object obj = type.newInstance(); //  JavaBean    

    //  JavaBean    
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    for (int i = 0; i < propertyDescriptors.length; i++) {
        PropertyDescriptor descriptor = propertyDescriptors[i];
        String propertyName = descriptor.getName();

        if (map.containsKey(propertyName)) {
            // ??? try ???   
            Object value = map.get(propertyName);
            if (propertyName.equals("id") || propertyName.equals("parentId")) {
                value = Long.valueOf(value.toString());
            }
            if (value instanceof Number) {
                value = Long.valueOf(value.toString());
            } else if (value instanceof Date) {
                value = (Date) value;
            }
            Object[] args = new Object[1];
            args[0] = value;
            descriptor.getWriteMethod().invoke(obj, args);
        }
    }
    return obj;
}