123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
-
- package com.vaadin.data.util;
-
- import java.beans.BeanInfo;
- import java.beans.IntrospectionException;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * A wrapper class for adding the Item interface to any Java Bean.
- *
- * @author Vaadin Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- @SuppressWarnings("serial")
- public class BeanItem<BT> extends PropertysetItem {
-
- /**
- * The bean which this Item is based on.
- */
- private final BT bean;
-
- /**
- * <p>
- * Creates a new instance of <code>BeanItem</code> and adds all properties
- * of a Java Bean to it. The properties are identified by their respective
- * bean names.
- * </p>
- *
- * <p>
- * Note : This version only supports introspectable bean properties and
- * their getter and setter methods. Stand-alone <code>is</code> and
- * <code>are</code> methods are not supported.
- * </p>
- *
- * @param bean
- * the Java Bean to copy properties from.
- *
- */
- public BeanItem(BT bean) {
- this(bean, getPropertyDescriptors((Class<BT>) bean.getClass()));
- }
-
- /**
- * <p>
- * Creates a new instance of <code>BeanItem</code> using a pre-computed set
- * of properties. The properties are identified by their respective bean
- * names.
- * </p>
- *
- * @param bean
- * the Java Bean to copy properties from.
- * @param propertyDescriptors
- * pre-computed property descriptors
- */
- BeanItem(BT bean,
- Map<String, VaadinPropertyDescriptor<BT>> propertyDescriptors) {
-
- this.bean = bean;
-
- for (VaadinPropertyDescriptor<BT> pd : propertyDescriptors.values()) {
- addItemProperty(pd.getName(), pd.createProperty(bean));
- }
- }
-
- /**
- * <p>
- * Creates a new instance of <code>BeanItem</code> and adds all listed
- * properties of a Java Bean to it - in specified order. The properties are
- * identified by their respective bean names.
- * </p>
- *
- * <p>
- * Note : This version only supports introspectable bean properties and
- * their getter and setter methods. Stand-alone <code>is</code> and
- * <code>are</code> methods are not supported.
- * </p>
- *
- * @param bean
- * the Java Bean to copy properties from.
- * @param propertyIds
- * id of the property.
- */
- public BeanItem(BT bean, Collection<?> propertyIds) {
-
- this.bean = bean;
-
- // Create bean information
- LinkedHashMap<String, VaadinPropertyDescriptor<BT>> pds = getPropertyDescriptors((Class<BT>) bean
- .getClass());
-
- // Add all the bean properties as MethodProperties to this Item
- for (Object id : propertyIds) {
- VaadinPropertyDescriptor<BT> pd = pds.get(id);
- if (pd != null) {
- addItemProperty(pd.getName(), pd.createProperty(bean));
- }
- }
-
- }
-
- /**
- * <p>
- * Creates a new instance of <code>BeanItem</code> and adds all listed
- * properties of a Java Bean to it - in specified order. The properties are
- * identified by their respective bean names.
- * </p>
- *
- * <p>
- * Note : This version only supports introspectable bean properties and
- * their getter and setter methods. Stand-alone <code>is</code> and
- * <code>are</code> methods are not supported.
- * </p>
- *
- * @param bean
- * the Java Bean to copy properties from.
- * @param propertyIds
- * ids of the properties.
- */
- public BeanItem(BT bean, String[] propertyIds) {
- this(bean, Arrays.asList(propertyIds));
- }
-
- /**
- * <p>
- * Perform introspection on a Java Bean class to find its properties.
- * </p>
- *
- * <p>
- * Note : This version only supports introspectable bean properties and
- * their getter and setter methods. Stand-alone <code>is</code> and
- * <code>are</code> methods are not supported.
- * </p>
- *
- * @param beanClass
- * the Java Bean class to get properties for.
- * @return an ordered map from property names to property descriptors
- */
- static <BT> LinkedHashMap<String, VaadinPropertyDescriptor<BT>> getPropertyDescriptors(
- final Class<BT> beanClass) {
- final LinkedHashMap<String, VaadinPropertyDescriptor<BT>> pdMap = new LinkedHashMap<String, VaadinPropertyDescriptor<BT>>();
-
- // Try to introspect, if it fails, we just have an empty Item
- try {
- List<PropertyDescriptor> propertyDescriptors = getBeanPropertyDescriptor(beanClass);
-
- // Add all the bean properties as MethodProperties to this Item
- // later entries on the list overwrite earlier ones
- for (PropertyDescriptor pd : propertyDescriptors) {
- final Method getMethod = pd.getReadMethod();
- if ((getMethod != null)
- && getMethod.getDeclaringClass() != Object.class) {
- VaadinPropertyDescriptor<BT> vaadinPropertyDescriptor = new MethodPropertyDescriptor(
- pd.getName(), pd.getPropertyType(),
- pd.getReadMethod(), pd.getWriteMethod());
- pdMap.put(pd.getName(), vaadinPropertyDescriptor);
- }
- }
- } catch (final java.beans.IntrospectionException ignored) {
- }
-
- return pdMap;
- }
-
- /**
- * Returns the property descriptors of a class or an interface.
- *
- * For an interface, superinterfaces are also iterated as Introspector does
- * not take them into account (Oracle Java bug 4275879), but in that case,
- * both the setter and the getter for a property must be in the same
- * interface and should not be overridden in subinterfaces for the discovery
- * to work correctly.
- *
- * For interfaces, the iteration is depth first and the properties of
- * superinterfaces are returned before those of their subinterfaces.
- *
- * @param beanClass
- * @return
- * @throws IntrospectionException
- */
- private static List<PropertyDescriptor> getBeanPropertyDescriptor(
- final Class<?> beanClass) throws IntrospectionException {
- // Oracle bug 4275879: Introspector does not consider superinterfaces of
- // an interface
- if (beanClass.isInterface()) {
- List<PropertyDescriptor> propertyDescriptors = new ArrayList<PropertyDescriptor>();
-
- for (Class<?> cls : beanClass.getInterfaces()) {
- propertyDescriptors.addAll(getBeanPropertyDescriptor(cls));
- }
-
- BeanInfo info = Introspector.getBeanInfo(beanClass);
- propertyDescriptors.addAll(Arrays.asList(info
- .getPropertyDescriptors()));
-
- return propertyDescriptors;
- } else {
- BeanInfo info = Introspector.getBeanInfo(beanClass);
- return Arrays.asList(info.getPropertyDescriptors());
- }
- }
-
- /**
- * Gets the underlying JavaBean object.
- *
- * @return the bean object.
- */
- public BT getBean() {
- return bean;
- }
-
- }
|