Example usage for java.beans PropertyDescriptor getPropertyType

List of usage examples for java.beans PropertyDescriptor getPropertyType

Introduction

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

Prototype

public synchronized Class<?> getPropertyType() 

Source Link

Document

Returns the Java type info for the property.

Usage

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Gets the value type of the given collection, as declared by the collection property type.
 *
 * @param beanType     bean class/*from   w  ww  .  java 2s.  c  o  m*/
 * @param beanProperty name of property, which must be a collection
 * @return generic type declared for collection members
 */
public static Class getCollectionValueType(Class beanType, String beanProperty) {
    PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(beanType, beanProperty);
    Class propertyType = descriptor.getPropertyType();
    Assert.PROGRAMMING.isTrue(Collection.class.isAssignableFrom(propertyType),
            "Bean property not a collection type: " + beanType + "." + beanProperty);

    Type genericPropertyType = descriptor.getReadMethod().getGenericReturnType();
    Class collectionValueType = null;
    if (genericPropertyType != null && genericPropertyType instanceof ParameterizedType) {
        Type[] typeArgs = ((ParameterizedType) genericPropertyType).getActualTypeArguments();
        if (typeArgs != null && typeArgs.length > 0) {
            if (typeArgs.length == 1) {
                collectionValueType = (Class) typeArgs[0];
            } else if (typeArgs.length == 2) {
                collectionValueType = (Class) typeArgs[1];
            } else {
                Assert.PROGRAMMING.fail("Collection type has more than two generic arguments");
            }
        }
    }

    return collectionValueType;
}

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  w w. j  a  v a  2s .  c  om
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);
        }
    }
}

From source file:Main.java

public static <T> T loadBean(Node node, Class<T> beanClass)
        throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        DOMException, InvocationTargetException {
    T store = beanClass.newInstance();/*from  w ww.ja v a2s.c  om*/

    Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
        properties.put(property.getName(), property);
    }

    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        PropertyDescriptor property = properties.get(attribute.getNodeName());
        if (property != null && property.getPropertyType().equals(String.class)
                && property.getWriteMethod() != null) {
            property.getWriteMethod().invoke(store, attribute.getNodeValue());
        }
    }

    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        PropertyDescriptor property = properties.get(child.getNodeName());
        if (property != null && property.getPropertyType().equals(String.class)
                && property.getWriteMethod() != null) {
            property.getWriteMethod().invoke(store, child.getTextContent());
        }
    }

    return store;
}

From source file:nl.strohalm.cyclos.utils.PropertyHelper.java

/**
 * Copies all possible properties from source to dest, ignoring the given properties list. Exceptions are ignored
 *//*w ww.j av  a 2s.co  m*/
public static void copyProperties(final Object source, final Object dest, final String... ignored) {
    if (source == null || dest == null) {
        return;
    }
    final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(source);
    for (final PropertyDescriptor sourceDescriptor : propertyDescriptors) {
        try {
            final String name = sourceDescriptor.getName();
            // Check for ignored properties
            if (ArrayUtils.contains(ignored, name)) {
                continue;
            }
            final PropertyDescriptor destProperty = PropertyUtils.getPropertyDescriptor(dest, name);
            if (destProperty.getWriteMethod() == null) {
                // Ignore read-only properties
                continue;
            }
            final Object value = CoercionHelper.coerce(destProperty.getPropertyType(), get(source, name));
            set(dest, name, value);
        } catch (final Exception e) {
            // Ignore this property
        }
    }
}

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

private static void addDescriptors(List<PropertyDescriptor> descriptors, PropertyDescriptor[] newDescriptors) {
    int count = descriptors.size();
    if (count > 0) {
        // filter equal properties
        for (int i = 0; i < newDescriptors.length; i++) {
            PropertyDescriptor newDescriptor = newDescriptors[i];
            if (newDescriptor.getPropertyType() == null) {
                continue;
            }//ww w  .j a  v a 2  s  .  co  m
            //
            String name = newDescriptor.getName();
            boolean addDescriptor = true;
            //
            for (int j = 0; j < count; j++) {
                PropertyDescriptor descriptor = descriptors.get(j);
                if (name.equals(descriptor.getName())) {
                    addDescriptor = false;
                    break;
                }
            }
            if (addDescriptor) {
                descriptors.add(newDescriptor);
            }
        }
    } else {
        // add all properties
        for (PropertyDescriptor descriptor : newDescriptors) {
            if (descriptor.getPropertyType() != null) {
                descriptors.add(descriptor);
            }
        }
    }
}

From source file:org.eclipse.scada.da.core.VariantBeanHelper.java

private static boolean applyValueAsObject(final Object target, final PropertyDescriptor pd, final Object value)
        throws IllegalAccessException, InvocationTargetException, OperationException {
    if (value == null) {
        pd.getWriteMethod().invoke(target, new Object[] { null });
        return true;
    }//from   w w  w.j  av a2s. co  m

    final Object targetType = converter.convert(value, pd.getPropertyType());

    if (targetType == null) {
        throw new OperationException(
                String.format("Unable to convert '%s' to '%s'", value.getClass(), pd.getPropertyType()));
    }

    pd.getWriteMethod().invoke(target, targetType);
    return true;
}

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

/**
 * @param bibtex//from  w w w.j  a v a 2 s  . com
 *            target object
 * @param entry
 *            source object
 * @return list of all copied property names
 */
public static List<String> copyStringPropertiesToBibsonomyModel(final BibTex bibtex, final BibtexEntry entry) {
    final List<String> knownFields = new ArrayList<String>(50);

    final BeanInfo info;
    try {
        info = Introspector.getBeanInfo(bibtex.getClass());
    } catch (IntrospectionException e) {
        ExceptionUtils.logErrorAndThrowRuntimeException(log, e, "could not introspect");
        return knownFields;
    }
    final PropertyDescriptor[] descriptors = info.getPropertyDescriptors();

    // set all known properties of the BibTex
    for (PropertyDescriptor pd : descriptors) {
        if (String.class.equals(pd.getPropertyType()) == false) {
            continue;
        }
        if (present(entry.getField((pd.getName().toLowerCase())))
                && !JabRefModelConverter.EXCLUDE_FIELDS.contains(pd.getName().toLowerCase())) {
            final Object value = entry.getField(pd.getName().toLowerCase());
            try {
                pd.getWriteMethod().invoke(bibtex, value);
            } catch (Exception e) {
                ExceptionUtils.logErrorAndThrowRuntimeException(log, e,
                        "could not convert property " + pd.getName());
                return knownFields;
            }
            knownFields.add(pd.getName());
        }
    }
    return knownFields;
}

From source file:org.eclipse.wb.internal.rcp.databinding.ui.contentproviders.TreeContentLabelProvidersUiContentProvider.java

/**
 * Helper method that filter given <code>properties</code> include only boolean properties.
 *//*from w  ww  .j  a v a  2 s  . c o m*/
private static List<PropertyDescriptor> filterBooleanProperties(List<PropertyDescriptor> properties) {
    List<PropertyDescriptor> newProperties = Lists.newArrayList();
    for (PropertyDescriptor property : properties) {
        Class<?> type = property.getPropertyType();
        if (type == boolean.class || type == Boolean.class) {
            newProperties.add(property);
        }
    }
    return newProperties;
}

From source file:org.eclipse.wb.internal.rcp.databinding.ui.contentproviders.TreeContentLabelProvidersUiContentProvider.java

/**
 * Helper method that filter given <code>properties</code> include only {@link Collection} and
 * array properties./*from w  w w.j  a va2 s  .  co  m*/
 */
private static List<PropertyDescriptor> filterCollectionProperties(List<PropertyDescriptor> properties) {
    List<PropertyDescriptor> newProperties = Lists.newArrayList();
    for (PropertyDescriptor property : properties) {
        Class<?> type = property.getPropertyType();
        if (type != null && (type.isArray() || Collection.class.isAssignableFrom(type))) {
            newProperties.add(property);
        }
    }
    return newProperties;
}

From source file:org.eclipse.wb.internal.rcp.databinding.ui.contentproviders.TreeContentLabelProvidersUiContentProvider.java

/**
 * Helper method that filter given <code>properties</code> include only {@link Class}
 * <code>testType</code> properties.
 *///  www.j av  a  2s. co  m
private static List<PropertyDescriptor> filterProperties(List<PropertyDescriptor> properties,
        Class<?> testType) {
    List<PropertyDescriptor> newProperties = Lists.newArrayList();
    for (PropertyDescriptor property : properties) {
        Class<?> type = property.getPropertyType();
        if (type != null && (testType == type || testType.isAssignableFrom(type))) {
            newProperties.add(property);
        }
    }
    return newProperties;
}