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.rhq.scripting.javascript.JavascriptCompletor.java

private Map<String, List<Object>> findJavaBeanContextMatches(Object baseObject, Class<?> baseObjectClass,
        String start) throws IntrospectionException {

    Map<String, List<Object>> found = new HashMap<String, List<Object>>();

    BeanInfo info = null;//  w w  w  .  j a v a  2s  . c  o  m
    if (baseObjectClass.isInterface() || baseObjectClass.equals(Object.class)) {
        info = Introspector.getBeanInfo(baseObjectClass);
    } else {
        info = Introspector.getBeanInfo(baseObjectClass, Object.class);
    }

    Set<Method> methodsCovered = new HashSet<Method>();

    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
    for (PropertyDescriptor desc : descriptors) {
        if (desc.getName().startsWith(start) && (!IGNORED_METHODS.contains(desc.getName()))) {

            List<Object> list = found.get(desc.getName());
            if (list == null) {
                list = new ArrayList<Object>();
                found.put(desc.getName(), list);
            }
            list.add(desc);

            methodsCovered.add(desc.getReadMethod());
            methodsCovered.add(desc.getWriteMethod());
        }
    }

    MethodDescriptor[] methods = info.getMethodDescriptors();
    for (MethodDescriptor desc : methods) {
        if (desc.getName().startsWith(start) && !methodsCovered.contains(desc.getMethod())
                && !desc.getName().startsWith("_d") && !IGNORED_METHODS.contains(desc.getName())) {

            Method m = desc.getMethod();

            List<Object> list = found.get(desc.getName());
            if (list == null) {
                list = new ArrayList<Object>();
                found.put(desc.getName(), list);
            }
            list.add(m);
        }
    }

    return found;
}

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

private Method findSupport(final String propertyName, final boolean setter) throws IntrospectionException {

    // If there's a support instance, use it first.

    Method m = null;//from   ww  w  .  j  av a  2 s  .c o m
    if (null != support) {

        final PropertyDescriptor[] propDescriptors = Introspector
                .getBeanInfo(support.getClass().getInterfaces()[0]).getPropertyDescriptors();

        for (PropertyDescriptor pd : propDescriptors) {

            if (propertyName.equalsIgnoreCase(pd.getName())) {
                if (pd instanceof MappedPropertyDescriptor) {

                    final MappedPropertyDescriptor mpd = (MappedPropertyDescriptor) pd;
                    m = setter ? mpd.getMappedWriteMethod() : mpd.getMappedReadMethod();

                } else {
                    m = setter ? pd.getWriteMethod() : pd.getReadMethod();
                }
                break;
            }
        }
    }
    return m;
}

From source file:org.apache.cocoon.transformation.TagTransformer.java

private static Map getWriteMethodMap(Class beanClass) throws IntrospectionException {
    Map map = (Map) TAG_PROPERTIES_MAP.get(beanClass);
    if (map != null) {
        return map;
    }//from   w  ww  .  jav  a 2 s . com

    BeanInfo info = Introspector.getBeanInfo(beanClass);
    if (info != null) {
        PropertyDescriptor pds[] = info.getPropertyDescriptors();
        map = new HashMap(pds.length * 4 / 3, 1);
        for (int i = 0; i < pds.length; i++) {
            PropertyDescriptor pd = pds[i];
            String name = pd.getName();
            Method method = pd.getWriteMethod();
            Class type = pd.getPropertyType();
            if (type != String.class) // only String properties
                continue;
            map.put(name, method);
        }
    }
    TAG_PROPERTIES_MAP.put(beanClass, map);
    return map;
}

From source file:com.temenos.interaction.media.hal.HALProvider.java

/** populate a Map from a java bean
 *  TODO implement nested structures and collections
 *//*from  w w  w  .  java2  s  .c  o m*/
protected void buildFromBean(Map<String, Object> map, Object bean, String entityName) {
    EntityMetadata entityMetadata = metadata.getEntityMetadata(entityName);
    if (entityMetadata == null)
        throw new IllegalStateException("Entity metadata could not be found [" + entityName + "]");

    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
        for (PropertyDescriptor propertyDesc : beanInfo.getPropertyDescriptors()) {
            String propertyName = propertyDesc.getName();
            if (entityMetadata.getPropertyVocabulary(propertyName) != null) {
                Object value = propertyDesc.getReadMethod().invoke(bean);
                map.put(propertyName, value);
            }
        }
    } catch (IllegalArgumentException e) {
        logger.error("Error accessing bean property", e);
    } catch (IntrospectionException e) {
        logger.error("Error accessing bean property", e);
    } catch (IllegalAccessException e) {
        logger.error("Error accessing bean property", e);
    } catch (InvocationTargetException e) {
        logger.error("Error accessing bean property", e);
    }
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

private void initializePropertyDescriptors() {
    @SuppressWarnings("unchecked")
    List<PropertyDescriptor> propertyDescriptors = Collections.EMPTY_LIST;
    try {/*from   www  .  j  av a 2  s  . c  o  m*/
        BeanInfo info = Introspector.getBeanInfo(clazz);
        propertyDescriptors = Collections.unmodifiableList(Arrays.stream(info.getPropertyDescriptors())
                .filter(prop -> !prop.getName().equals("class")).collect(toList()));
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
    for (int i = 0; i < propertyDescriptors.size(); i++) {
        addPropertyInfo(new PropertyInfo(propertyDescriptors.get(i), i));
    }
}

From source file:net.jolm.JolmLdapTemplate.java

private List<? extends LdapEntity> filterAttributes(List<LdapEntity> entities, String[] attributes) {
    if (entities == null || entities.size() == 0) {
        return entities;
    }// w w  w. j a va 2  s . c  om
    List<String> attributesAsList = Arrays.asList(attributes);
    for (LdapEntity entity : entities) {
        BeanInfo beanInfo;
        try {
            beanInfo = Introspector.getBeanInfo(entity.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor pd : propertyDescriptors) {
                if (isAttributeFiltered(pd.getName(), attributesAsList)) {
                    Method writeMethod = pd.getWriteMethod();
                    writeMethod.invoke(entity, new Object[] { null });
                }
            }
        } catch (Exception e) {
            //Should never happen
            throw new RuntimeException(e);
        }
    }
    return entities;
}

From source file:com.hiperf.common.ui.server.storage.impl.PersistenceHelper.java

private static Object deproxyObject(Class<?> targetClass, Object proxy)
        throws InstantiationException, IllegalAccessException, IntrospectionException,
        InvocationTargetException, PersistenceException, ClassNotFoundException {
    Object target = targetClass.newInstance();
    PropertyDescriptor[] targetPds = Introspector.getBeanInfo(targetClass).getPropertyDescriptors();
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getReadMethod() != null && targetPd.getWriteMethod() != null) {
            Object o = targetPd.getReadMethod().invoke(proxy, new Object[0]);
            if (o != null) {
                Class<?> propertyType = targetPd.getPropertyType();
                String className = propertyType.getName();
                if (!propertyType.isPrimitive() && !o.getClass().isPrimitive() && !(o instanceof Date)
                        && isProxy(o, className)) {
                    if (Set.class.isAssignableFrom(propertyType)) {
                        o = new LazySet();
                    } else if (List.class.isAssignableFrom(propertyType)) {
                        o = new LazyList();
                    } else
                        o = newLazyObject(propertyType);
                }//  www .jav a 2s.com
                targetPd.getWriteMethod().invoke(target, o);
            }
        }
    }

    return target;
}

From source file:com.zhumeng.dream.orm.hibernate.HibernateDao.java

/**
 * ?,hibernate???select count(o) from Xxx o?BUG,
 * hibernatejpql??sqlselect count(field1,field2,...),count()
 * ??HQL?:"select count(*) from Object"//from  w w w  . ja v  a  2  s .c  o m
 * @author: lizhong
 * @param <E>
 * @param clazz
 * @return
 */
protected static <E> String getCountField(Class<E> clazz) {
    String out = "o";
    try {
        PropertyDescriptor[] propertys = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
        for (PropertyDescriptor property : propertys) {
            Method method = property.getReadMethod();
            if (method != null && method.isAnnotationPresent(EmbeddedId.class)) {
                PropertyDescriptor[] props = Introspector.getBeanInfo(property.getPropertyType())
                        .getPropertyDescriptors();
                out = "o." + property.getName() + "."
                        + (!props[1].getName().equals("class") ? props[1].getName() : props[0].getName());
                break;
            }
        }
    } catch (IntrospectionException e) {
        e.printStackTrace();
    }
    return out;
}

From source file:ResultSetIterator.java

/**
 * Returns a PropertyDescriptor[] for the given Class.
 *
 * @param c The Class to retrieve PropertyDescriptors for.
 * @return A PropertyDescriptor[] describing the Class.
 * @throws SQLException if introspection failed.
 *///from   w  w w  .j av a 2  s  . c om
private PropertyDescriptor[] propertyDescriptors(Class c) throws SQLException {
    // Introspector caches BeanInfo classes for better performance
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(c);

    } catch (IntrospectionException e) {
        throw new SQLException("Bean introspection failed: " + e.getMessage());
    }

    return beanInfo.getPropertyDescriptors();
}