Example usage for java.beans Introspector getBeanInfo

List of usage examples for java.beans Introspector getBeanInfo

Introduction

In this page you can find the example usage for java.beans Introspector getBeanInfo.

Prototype

public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException 

Source Link

Document

Introspect on a Java Bean and learn about all its properties, exposed methods, and events.

Usage

From source file:org.jcurl.core.helpers.PropertyChangeSupport.java

/**
 * Creates a new instance of SafePropertyChangeSupport.
 * /*w  w  w.j  a  v  a 2  s .c o  m*/
 * @param producer
 *            This is the object that is producing the property change
 *            events.
 * @throws RuntimeException
 *             If there is an introspection problem.
 */
public PropertyChangeSupport(final Object producer) {
    try {
        final BeanInfo info = Introspector.getBeanInfo(producer.getClass());
        for (final PropertyDescriptor element : info.getPropertyDescriptors())
            listenerMap.put(element.getName(), new WeakHashSet());
        listenerMap.put(ALL_PROPERTIES, new WeakHashSet());
        this.producer = producer;
    } catch (final IntrospectionException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:no.sesat.search.datamodel.DataModelTest.java

private void collectProperties(final Class<?> cls, final Collection<Method> propertyMethods)
        throws IntrospectionException {

    final List<PropertyDescriptor> props = Arrays
            .asList(Introspector.getBeanInfo(cls).getPropertyDescriptors());
    //                = new ArrayList<PropertyDescriptor>();
    //        props.addAll(Arrays.asList(Introspector.getBeanInfo(cls, Introspector.IGNORE_ALL_BEANINFO).getPropertyDescriptors()));
    //        Introspector.flushFromCaches(cls);
    //        props.addAll(Arrays.asList(Introspector.getBeanInfo(cls, Introspector.USE_ALL_BEANINFO).getPropertyDescriptors()));
    //        Introspector.flushFromCaches(cls);

    for (PropertyDescriptor property : props) {

        LOG.info(" property --> " + property.getName());

        handleProperty(propertyMethods, property);
        if (property instanceof MappedPropertyDescriptor) {
            handleMappedProperty(propertyMethods, (MappedPropertyDescriptor) property);
        }//from w  w w . j ava2 s  .c o m
    }

    // repeat again on all implemented interfaces
    for (Class<?> c : cls.getInterfaces()) {
        collectProperties(c, propertyMethods);
    }
}

From source file:com.manydesigns.elements.reflection.JavaClassAccessor.java

protected List<PropertyAccessor> setupPropertyAccessors() {
    List<PropertyAccessor> accessorList = new ArrayList<PropertyAccessor>();

    // handle properties through introspection
    try {//from  w  w w.  java  2 s.com
        BeanInfo beanInfo = Introspector.getBeanInfo(javaClass);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor current : propertyDescriptors) {
            accessorList.add(new JavaPropertyAccessor(current));
        }
    } catch (IntrospectionException e) {
        logger.error(e.getMessage(), e);
    }

    // handle public fields
    for (Field field : javaClass.getFields()) {
        if (isPropertyPresent(accessorList, field.getName())) {
            continue;
        }
        accessorList.add(new JavaFieldAccessor(field));
    }

    return accessorList;
}

From source file:org.jcurl.core.api.PropertyChangeSupport.java

/**
 * Creates a new instance of SafePropertyChangeSupport.
 * //from   w  w  w  . j a  v  a 2 s . c o  m
 * @param producer
 *            This is the object that is producing the property change
 *            events.
 * @throws RuntimeException
 *             If there is an introspection problem.
 */
public PropertyChangeSupport(final Object producer) {
    try {
        final BeanInfo info = Introspector.getBeanInfo(producer.getClass());
        for (final PropertyDescriptor element : info.getPropertyDescriptors())
            listenerMap.put(element.getName(), new WeakHashSet<PropertyChangeListener>());
        listenerMap.put(ALL_PROPERTIES, new WeakHashSet<PropertyChangeListener>());
        this.producer = producer;
    } catch (final IntrospectionException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.swingtech.commons.util.ClassUtil.java

/**
 * @param pClass// w  w  w.ja  v a  2 s.co  m
 * @param filterRegEx
 * @return
 */
public static List getPropertyNamesFromClass(final Class pClass, final String filterRegEx) {
    BeanInfo vBeanInfo = null;
    PropertyDescriptor vPropDescList[] = null;
    PropertyDescriptor vPropDesc = null;
    String vPropName = null;
    final Vector vPropertyNameList = new Vector();

    try {
        vBeanInfo = Introspector.getBeanInfo(pClass);
    } catch (final IntrospectionException e) {
        logger.warn("Utility.getPropertyNamesFromClass.  Error trying to use Introspector", e);
        return null;
    }

    vPropDescList = vBeanInfo.getPropertyDescriptors();

    for (int i = 0; i < vPropDescList.length; i++) {
        vPropDesc = vPropDescList[i];

        vPropName = vPropDesc.getName();

        if (Utility.isNullOrEmpty(filterRegEx) || vPropName.matches(filterRegEx)) {
            vPropertyNameList.add(vPropName);
        }
    }

    if (vPropertyNameList.isEmpty()) {
        return null;
    }

    return vPropertyNameList;
}

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

private void saveSettings(ConfigurationBean bean) {

    if (_path == null)
        return;/*w ww . j a  va2  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:com.kangdainfo.common.util.BeanUtil.java

/**
 *
 * @param bean/*w w  w . jav  a2 s.c o m*/
 * @return
 * @throws IntrospectionException
 */
public static PropertyDescriptor[] getPropertyDescriptors(Object bean) throws IntrospectionException {

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

    return descriptors;

}

From source file:ch.ifocusit.plantuml.utils.ClassUtils.java

public static boolean isGetter(Method method) {
    try {//w  w  w .java2 s .c om
        return Stream.of(Introspector.getBeanInfo(method.getDeclaringClass()).getPropertyDescriptors())
                .map(desc -> desc.getReadMethod()).filter(Objects::nonNull)
                .anyMatch(getter -> getter.equals(method));
    } catch (IntrospectionException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.onebusaway.siri.core.filters.ElementPathModuleDeliveryFilter.java

/**
 * We support lazy initialization of the {@link PropertyDescriptor}
 * descriptors necessary to read the and write to our object tree. Why?
 * /*ww  w . java 2  s.c  o m*/
 * We can't do introspection ahead of time because we support iteration over
 * Collection types. Specifically, if a path expression navigates over a
 * collection, we'll iterate over the values of the collection, applying the
 * next level of property path to the collection value, as opposed to the
 * collection object itself. Because we can't introspect the element type of a
 * Collection from the parent class, we have to wait to do introspection at
 * runtime on the values of the Collection itself.
 * 
 * @param value
 * @param depth
 * @return
 */
private PropertyDescriptor getPropertyDescriptorForDepth(Object value, int depth) {

    PropertyDescriptor property = _properties[depth];

    if (property == null) {

        synchronized (this) {

            if (property == null) {

                String propertyName = _propertyNames[depth];

                try {

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

                    for (PropertyDescriptor propertyDesc : beanInfo.getPropertyDescriptors()) {
                        if (propertyDesc.getName().equals(propertyName)) {
                            property = propertyDesc;
                            break;
                        }
                    }

                } catch (Throwable ex) {
                    throw new SiriException("error in introspection of class " + value.getClass()
                            + " and property \"" + propertyName + "\"");
                }

                if (property == null) {
                    throw new SiriException(
                            "class " + value.getClass() + " does not have property \"" + propertyName + "\"");
                }

                PropertyDescriptor[] properties = new PropertyDescriptor[_properties.length];
                System.arraycopy(_properties, 0, properties, 0, properties.length);
                properties[depth] = property;
                _properties = properties;
            }
        }
    }

    return property;
}

From source file:org.pentaho.reporting.engine.classic.core.util.beans.BeanUtility.java

public BeanUtility(final Object o) throws IntrospectionException {
    beanInfo = Introspector.getBeanInfo(o.getClass());
    bean = o;/*from   w w w . j a  va 2  s. c  o  m*/
    properties = new HashMap<String, PropertyDescriptor>();

    final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    for (int i = 0; i < propertyDescriptors.length; i++) {
        properties.put(propertyDescriptors[i].getName(), propertyDescriptors[i]);
    }
}