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:storybook.model.EntityUtil.java

public static void printBeanProperties(AbstractEntity entity) {
    SbApp.trace("EntityUtil.printBeanProperties(" + entity.getClass().getName() + ")");
    try {/*from   ww w.j  a  v a  2s  .  co m*/
        BeanInfo bi = Introspector.getBeanInfo(entity.getClass());
        for (PropertyDescriptor propDescr : bi.getPropertyDescriptors()) {
            String name = propDescr.getName();
            Object val = propDescr.getReadMethod().invoke(entity);
            String isNull = "not null";
            if (val == null) {
                isNull = "is null";
            }
            System.out.println("EntityUtil.printProperties(): " + name + ": '" + val + "' "
                    + (val == null ? "isNull" : "not null"));
        }
    } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        System.err.println("EntityUtil.printBeanProperties(" + entity + ") Exception : " + e.getMessage());
    }
}

From source file:org.codehaus.enunciate.modules.xfire.EnunciatedJAXWSOperationBinding.java

/**
 * Loads the property descriptors for the ordered properties of the specified class.
 *
 * @param wrapperClass The wrapper class.
 * @return The ordered property descriptors.
 *///w  w  w .  ja v  a2s.c o  m
protected PropertyDescriptor[] loadOrderedProperties(Class wrapperClass) throws XFireFault {
    XmlType typeInfo = (XmlType) wrapperClass.getAnnotation(XmlType.class);
    if ((typeInfo == null) || (typeInfo.propOrder() == null)
            || ((typeInfo.propOrder().length == 1) && "".equals(typeInfo.propOrder()[0]))) {
        throw new XFireFault(
                "Unable use use " + wrapperClass.getName() + " as a wrapper class: no propOrder specified.",
                XFireFault.RECEIVER);
    }

    String[] propOrder = typeInfo.propOrder();
    BeanInfo beanInfo;
    try {
        beanInfo = Introspector.getBeanInfo(wrapperClass, Object.class);
    } catch (IntrospectionException e) {
        throw new XFireFault("Unable to introspect " + wrapperClass.getName(), e, XFireFault.RECEIVER);
    }

    PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
    PropertyDescriptor[] props = new PropertyDescriptor[propOrder.length];
    RESPONSE_PROPERTY_LOOP: for (int i = 0; i < propOrder.length; i++) {
        String property = propOrder[i];
        if ((property.length() > 1) && (!Character.isLowerCase(property.charAt(1)))) {
            //if the second letter is uppercase, javabean spec says the first character of the property is also to be kept uppercase.
            property = capitalize(property);
        }

        for (PropertyDescriptor descriptor : pds) {
            if (descriptor.getName().equals(property)) {
                props[i] = descriptor;
                continue RESPONSE_PROPERTY_LOOP;
            }
        }

        throw new XFireFault("Unknown property " + property + " on wrapper " + wrapperClass.getName(),
                XFireFault.RECEIVER);
    }

    return props;
}

From source file:org.liveSense.api.beanprocessors.DbStandardBeanProcessor.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 ww w . j  a va 2  s. c o m
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());
    }

    //        PropertyDescriptor[] pd = beanInfo.getPropertyDescriptors();
    return beanInfo.getPropertyDescriptors();
}

From source file:com.gdcn.modules.db.jdbc.processor.CamelBeanProcessor.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.
 *///w w  w .j a  va 2 s. co m
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());
    }
    List<PropertyDescriptor> propList = Lists.newArrayList();
    PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor prop : props) {
        String propName = prop.getName();
        try {
            Field field = ReflectionUtils.findField(c, propName);
            if (field != null && !field.isAnnotationPresent(Transient.class)) {//1.field=null 2.Transient??
                propList.add(prop);
            }
        } catch (SecurityException e) {
            throw new SQLException("Bean Get Field failed: " + e.getMessage());
        }
    }
    return propList.toArray(new PropertyDescriptor[propList.size()]);
}

From source file:org.apache.tapestry.enhance.ComponentClassFactory.java

private void buildBeanProperties() {
    BeanInfo info = null;

    try {/*from   www. j ava  2  s  .com*/
        info = Introspector.getBeanInfo(_componentClass);

    } catch (IntrospectionException ex) {
        throw new ApplicationRuntimeException(
                Tapestry.format("ComponentClassFactory.unable-to-introspect-class", _componentClass.getName()),
                ex);
    }

    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();

    for (int i = 0; i < descriptors.length; i++) {
        _beanProperties.put(descriptors[i].getName(), descriptors[i]);
    }
}

From source file:com.gmail.sretof.db.jdbc.processor.CamelBeanProcessor.java

/**
 * Returns a PropertyDescriptor[] for the given Class.
 * /*from  w w w  .  j a  va2 s .c o m*/
 * @param c
 *            The Class to retrieve PropertyDescriptors for.
 * @return A PropertyDescriptor[] describing the Class.
 * @throws SQLException
 *             if introspection failed.
 */
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());
    }
    List<PropertyDescriptor> propList = Lists.newArrayList();
    PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor prop : props) {
        String propName = prop.getName();
        try {
            Field field = ReflectionUtils.findField(c, propName);
            if (field != null && !field.isAnnotationPresent(Transient.class)) {// 1.field=null
                // 2.Transient??
                propList.add(prop);
            }
        } catch (SecurityException e) {
            throw new SQLException("Bean Get Field failed: " + e.getMessage());
        }
    }
    return propList.toArray(new PropertyDescriptor[propList.size()]);
}

From source file:org.rhq.cassandra.DeploymentOptions.java

public TokenReplacingProperties toMap() {
    try {/*w w w .j  a v a2  s.  c om*/
        BeanInfo beanInfo = Introspector.getBeanInfo(DeploymentOptions.class);
        Map<String, String> properties = new TreeMap<String, String>();

        for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
            if (pd.getReadMethod() == null) {
                throw new RuntimeException("The [" + pd.getName() + "] property must define a getter method");
            }
            Method method = pd.getReadMethod();
            DeploymentProperty deploymentProperty = method.getAnnotation(DeploymentProperty.class);
            if (deploymentProperty != null) {
                Object value = method.invoke(this, null);
                if (value != null) {
                    properties.put(deploymentProperty.name(), value.toString());
                }
            }
        }
        return new TokenReplacingProperties(properties);
    } catch (Exception e) {
        throw new RuntimeException("Failed to convert " + DeploymentOptions.class.getName() + " to a map", e);
    }
}

From source file:com.googlecode.jsonplugin.JSONWriter.java

/**
 * Instrospect bean and serialize its properties
 *//* w ww  .  ja va 2 s . c o m*/
private void bean(Object object) throws JSONException {
    this.add("{");

    BeanInfo info;

    try {
        Class clazz = object.getClass();

        info = ((object == this.root) && this.ignoreHierarchy)
                ? Introspector.getBeanInfo(clazz, clazz.getSuperclass())
                : Introspector.getBeanInfo(clazz);

        PropertyDescriptor[] props = info.getPropertyDescriptors();

        boolean hasData = false;
        for (int i = 0; i < props.length; ++i) {
            PropertyDescriptor prop = props[i];
            String name = prop.getName();
            Method accessor = prop.getReadMethod();
            Method baseAccessor = null;
            if (clazz.getName().indexOf("$$EnhancerByCGLIB$$") > -1) {
                try {
                    baseAccessor = Class.forName(clazz.getName().substring(0, clazz.getName().indexOf("$$")))
                            .getMethod(accessor.getName(), accessor.getParameterTypes());
                } catch (Exception ex) {
                    log.debug(ex.getMessage(), ex);
                }
            } else
                baseAccessor = accessor;

            if (baseAccessor != null) {

                JSON json = baseAccessor.getAnnotation(JSON.class);
                if (json != null) {
                    if (!json.serialize())
                        continue;
                    else if (json.name().length() > 0)
                        name = json.name();
                }

                //ignore "class" and others
                if (this.shouldExcludeProperty(clazz, prop)) {
                    continue;
                }
                String expr = null;
                if (this.buildExpr) {
                    expr = this.expandExpr(name);
                    if (this.shouldExcludeProperty(expr)) {
                        continue;
                    }
                    expr = this.setExprStack(expr);
                }

                Object value = accessor.invoke(object, new Object[0]);
                boolean propertyPrinted = this.add(name, value, accessor, hasData);
                hasData = hasData || propertyPrinted;
                if (this.buildExpr) {
                    this.setExprStack(expr);
                }
            }
        }

        // special-case handling for an Enumeration - include the name() as a property */
        if (object instanceof Enum) {
            Object value = ((Enum) object).name();
            this.add("_name", value, object.getClass().getMethod("name"), hasData);
        }
    } catch (Exception e) {
        throw new JSONException(e);
    }

    this.add("}");
}

From source file:de.xwic.appkit.core.transport.xml.XmlBeanSerializer.java

/**
 * Deserializes an element into a bean./*from   w w  w  . ja  v a2  s  .  co  m*/
 * 
 * @param elm
 * @param context
 * @return
 * @throws TransportException
 */
public void deserializeBean(Object bean, Element elm, Map<EntityKey, Integer> context,
        boolean forceLoadCollection) throws TransportException {
    // now read the properties
    try {
        BeanInfo bi = Introspector.getBeanInfo(bean.getClass());
        for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
            Method mWrite = pd.getWriteMethod();
            if (!"class".equals(pd.getName()) && mWrite != null && !skipPropertyNames.contains(pd.getName())) {
                Element elmProp = elm.element(pd.getName());
                if (elmProp != null) {
                    Object value = readValue(context, elmProp, pd, forceLoadCollection);
                    mWrite.invoke(bean, new Object[] { value });
                } else {
                    log.warn("The property " + pd.getName()
                            + " is not specified in the xml document. The bean may not be restored completly");
                }
            }
        }
    } catch (Exception e) {
        throw new TransportException("Error deserializing bean: " + e, e);
    }
}

From source file:de.xwic.appkit.core.transport.xml.XmlBeanSerializer.java

/**
 * @param doc//  ww  w  .  ja  v a 2  s  .c om
 * @param string
 * @param query
 * @throws IntrospectionException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
public void serializeBean(Element elm, String elementName, Object bean) throws TransportException {

    Element root = elm.addElement(elementName);

    root.addAttribute("type", bean.getClass().getName());

    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
        for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {

            if (!"class".equals(pd.getName())) {
                Element pElm = root.addElement(pd.getName());
                Method mRead = pd.getReadMethod();
                if (mRead == null) {
                    log.debug(String.format("No '%s' property on '%s'.", pd.getName(), bean.getClass()));
                    continue;
                }
                Object value = mRead.invoke(bean, NO_ARGS);

                boolean addType = true;
                addType = value != null && !value.getClass().equals(pd.getPropertyType())
                        && !pd.getPropertyType().isPrimitive();
                addValue(pElm, value, addType);
            }

        }
    } catch (Exception e) {
        throw new TransportException("Error serializing bean: " + e, e);
    }

}