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.jkcsoft.java.util.Beans.java

public static PropertyDescriptor[] getPropertyDescriptors(Class clazz) {
    PropertyDescriptor[] pds = null;
    try {//from w ww .  ja v  a  2  s  .  c om
        BeanInfo bi = Introspector.getBeanInfo(clazz);
        pds = bi.getPropertyDescriptors();
    } catch (Exception ex) {
        log.error(ex);
    }
    return pds;
}

From source file:com.cloudera.csd.validation.references.components.ReflectionHelper.java

/**
 * Return the set of getter methods for the class.
 *
 * @param clazz the class.//ww w .  ja v a 2s .c  o  m
 * @return the set of getter methods.
 */
public static Set<Method> getterMethods(Class<?> clazz) {
    Preconditions.checkNotNull(clazz);
    try {
        ImmutableSet.Builder<Method> builder = ImmutableSet.builder();
        BeanInfo info = Introspector.getBeanInfo(clazz);
        for (PropertyDescriptor p : info.getPropertyDescriptors()) {
            Method getter = p.getReadMethod();
            if (getter != null) {
                // Don't want to include any of the methods inherited from object.
                if (!getter.getDeclaringClass().equals(Object.class)) {
                    builder.add(getter);
                }
            }
        }
        return builder.build();
    } catch (IntrospectionException e) {
        throw new IllegalStateException("Could not introspect on " + clazz, e);
    }
}

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

/**
 * Extract the property data as string/variant map
 * @param source the source object//from ww w  .ja  va 2 s . c om
 * @return the map with bean data
 * @throws IntrospectionException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static Map<String, Variant> extract(final Object source) throws IntrospectionException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    final Map<String, Variant> result = new HashMap<String, Variant>();

    final BeanInfo bi = Introspector.getBeanInfo(source.getClass());

    for (final PropertyDescriptor pd : bi.getPropertyDescriptors()) {
        final Method m = pd.getReadMethod();
        if (m != null) {
            result.put(pd.getName(), Variant.valueOf(m.invoke(source)));
        }
    }
    return result;
}

From source file:kelly.util.BeanUtils.java

/**
 * Retrieve the JavaBeans {@code PropertyDescriptor}s of a given class.
 * @param clazz the Class to retrieve the PropertyDescriptors for
 * @return an array of {@code PropertyDescriptors} for the given class
 *//*from   w  w w  .  java  2s  . c  om*/
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) {
    Validate.notNull(clazz, "Class must not be null");
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
        return beanInfo.getPropertyDescriptors();
    } catch (IntrospectionException e) {
        throw new BeanException(e.getMessage(), e);
    }
}

From source file:com.mawujun.utils.bean.BeanUtils.java

private static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws IntrospectionException {
    PropertyDescriptor[] pds = beanPropertyCache.get(clazz.getName());
    if (pds != null) {
        //return pds;
    } else {//from w  ww  .  ja  va 2s. c  o  m
        BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
        pds = beanInfo.getPropertyDescriptors();
        beanPropertyCache.put(clazz.getName(), pds);
    }
    return pds;
}

From source file:org.jkcsoft.java.util.Beans.java

public static PropertyDescriptor getPropertyDescriptor(Object bean, String propName)
        throws IntrospectionException {
    BeanInfo bi = Introspector.getBeanInfo(bean.getClass());
    PropertyDescriptor pds[] = bi.getPropertyDescriptors();
    PropertyDescriptor pd = null;
    for (int i = 0; i < pds.length; i++) {
        if (propName.equalsIgnoreCase(pds[i].getName())) {
            pd = pds[i];//from   www.  j av  a2  s .  c o m
            break;
        }
    }
    return pd;
}

From source file:org.zht.framework.util.ZBeanUtil.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map convertBeanToMap(Object bean) {
    Map returnMap = null;//  ww  w.ja va2 s.c om
    try {
        Class<?> type = bean.getClass();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        //           MethodAccess access = MethodAccess.get(type.getClass());
        returnMap = new HashMap();
        for (PropertyDescriptor property : propertyDescriptors) {
            String properName = property.getName();
            Method getter = property.getReadMethod();
            Object value = getter.invoke(bean);
            returnMap.put(properName, value);
            //
            //            Object value=access.invoke(bean,"get" + ZStrUtil.toUpCaseFirst(properName));
            //            if (value != null){
            //               returnMap.put(properName, value);
            //            }else{
            //               returnMap.put(properName, null);
            //            }

        }
    } catch (Exception e) {
        e.printStackTrace();
        return returnMap;
    }
    return returnMap;
}

From source file:org.jkcsoft.java.util.Beans.java

/**
 * Doesn't work yet!/*ww  w  . j  a  v a  2s. com*/
 *
 * @param bean
 */
public static void cleanBean(Object bean) {
    if (bean == null)
        return;

    try {
        BeanInfo bi = Introspector.getBeanInfo(bean.getClass());
        PropertyDescriptor pds[] = bi.getPropertyDescriptors();
        PropertyDescriptor pd = null;
        for (int i = 0; i < pds.length; i++) {
            pd = pds[i];
            //Method getter = pd.getReadMethod();
            Method setter = pd.getWriteMethod();
            if (pd.getPropertyType() == Integer.class) {
                setter.invoke(bean, new Object[] { new Integer(0) });
            } else if (pd.getPropertyType() == Double.class) {
                setter.invoke(bean, new Object[] { new Double(0) });
            } else {
                try {
                    setter.invoke(bean, new Object[] { null });
                } catch (Throwable e) {
                    log.warn("cleanBean()", e);
                }
            }
        }
    } catch (Throwable e) {
        log.warn("cleanBean()", e);
    }
}

From source file:com.github.pfmiles.minvelocity.TemplateUtil.java

private static void putAllPojoVals(Object ctxPojo, Context ctx) {
    ctx.put("ParseUtil", ParseUtil.class);
    if (ctxPojo == null)
        return;/* w  w w  .j  av a  2s  .  c  om*/
    if (ctxPojo instanceof Map) {
        for (Map.Entry<?, ?> e : ((Map<?, ?>) ctxPojo).entrySet()) {
            ctx.put(e.getKey().toString(), e.getValue());
        }
    } else {
        BeanInfo bi;
        try {
            bi = Introspector.getBeanInfo(ctxPojo.getClass());
            for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
                if ("class".equals(pd.getName()))
                    continue;
                Method rm = pd.getReadMethod();
                if (rm != null)
                    ctx.put(pd.getName(), rm.invoke(ctxPojo));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:ubic.gemma.util.PrettyPrinter.java

/**
 * The only class that does any real work. Recursively print an object and all its associated objects.
 * /*from   w ww .  j  a  v a 2 s  . c  om*/
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws IntrospectionException
 * @param buf
 * @param gemmaObj
 * @param level Used to track indents.
 */
private static void print(StringBuffer buf, Object bean, int level) throws IntrospectionException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    if (bean == null)
        return;
    Class<?> gemmaClass = bean.getClass();

    if (bean instanceof Collection) {
        print(buf, (Collection<?>) bean, ++level);
        return;
    }

    if (!gemmaClass.getName().startsWith("ubic.gemma"))
        return;

    BeanInfo bif = Introspector.getBeanInfo(gemmaClass);
    PropertyDescriptor[] props = bif.getPropertyDescriptors();

    StringBuffer indent = new StringBuffer();
    for (int i = 0; i < level; i++)
        indent.append("   ");

    boolean first = true;
    level++;

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

        Object o = prop.getReadMethod().invoke(bean, new Object[] {});

        if (prop.getDisplayName().equals("class"))
            continue; // everybody has it.
        if (prop.getDisplayName().equals("mutable"))
            continue; // shows up in the enums, just clutter.

        // generate a 'heading' for this object.
        if (first)
            buf.append(indent + bean.getClass().getSimpleName() + " Properties:\n");

        first = false;
        buf.append(indent + "   " + bean.getClass().getSimpleName() + "." + prop.getName() + ": "
                + (o == null ? "---" : o) + "\n");
        print(buf, o, level);
    }
}