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:Main.java

public static void main(String[] argv) throws Exception {

    BeanInfo bi = Introspector.getBeanInfo(MyBean.class);
    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    for (int i = 0; i < pds.length; i++) {

        String propName = pds[i].getDisplayName();
        System.out.println(propName);
    }/*from w w  w  . j  av a  2 s.  co m*/

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BeanInfo beanInfo = Introspector.getBeanInfo(Fruit.class);
    PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

    for (PropertyDescriptor pd : pds) {
        String propertyName = pd.getName();
        System.out.println("propertyName = " + propertyName);
    }//from w w  w. j a v a  2s  .co  m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BeanInfo bi = Introspector.getBeanInfo(BeanToXmlTransient.class);
    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor propertyDescriptor = pds[i];
        if (propertyDescriptor.getName().equals("itemQuantities")) {
            propertyDescriptor.setValue("transient", Boolean.TRUE);
        }/*w ww.j a v  a  2  s. c  o m*/
    }
    BeanToXmlTransient bean = new BeanToXmlTransient();
    bean.setId(new Long(1));
    bean.setItemName("Item");
    bean.setItemColour("Red");
    bean.setItemQuantities(new Integer(100));

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("BeanTransient.xml")));

    encoder.writeObject(bean);
    encoder.close();

}

From source file:IntrospectorDemo.java

public static void main(String args[]) {
    try {// www.  j  a  va 2 s  . c o m
        Class c = Class.forName("Colors");
        BeanInfo beanInfo = Introspector.getBeanInfo(c);

        System.out.println("Properties:");
        PropertyDescriptor propertyDescriptor[] = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptor.length; i++) {
            System.out.println("\t" + propertyDescriptor[i].getName());
        }

        System.out.println("Events:");
        EventSetDescriptor eventSetDescriptor[] = beanInfo.getEventSetDescriptors();
        for (int i = 0; i < eventSetDescriptor.length; i++) {
            System.out.println("\t" + eventSetDescriptor[i].getName());
        }
    } catch (Exception e) {
        System.out.println("Exception caught. " + e);
    }
}

From source file:generators.ExpressionMetaValidator.java

public static void main(String[] args) {
    ClassicEngineBoot.getInstance().start();
    int invalidExpressionsCounter = 0;
    int deprecatedExpressionsCounter = 0;
    final HashNMap expressionsByGroup = new HashNMap();

    ExpressionRegistry expressionRegistry = ExpressionRegistry.getInstance();
    final ExpressionMetaData[] allExpressions = expressionRegistry.getAllExpressionMetaDatas();
    for (int i = 0; i < allExpressions.length; i++) {
        final ExpressionMetaData expression = allExpressions[i];
        if (expression == null) {
            logger.warn("Null Expression encountered");
            continue;
        }/*from  w w w. ja  v a2  s . c o m*/

        missingProperties.clear();

        final Class type = expression.getExpressionType();
        if (type == null) {
            logger.warn("Expression class is null");
        }

        logger.debug("Processing " + type);

        final Class resultType = expression.getResultType();
        if (resultType == null) {
            logger.warn("Expression '" + expression.getExpressionType() + " is null");
        }

        try {
            final BeanInfo beanInfo = expression.getBeanDescriptor();
            if (beanInfo == null) {
                logger.warn(
                        "Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor: Null");
            }
        } catch (IntrospectionException e) {
            logger.warn("Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor", e);
        }

        final Locale locale = Locale.getDefault();
        final String displayName = expression.getDisplayName(locale);
        if (isValid(displayName, expression.getName()) == false) {
            logger.warn("Expression '" + expression.getExpressionType() + ": No valid display name");
        }
        if (expression.isDeprecated()) {
            deprecatedExpressionsCounter += 1;
            final String deprecateMessage = expression.getDeprecationMessage(locale);
            if (isValid(deprecateMessage, "Use a Formula instead") == false) {
                logger.warn("Expression '" + expression.getExpressionType() + ": No valid deprecate message");
            }
        }
        final String grouping = expression.getGrouping(locale);
        if (isValid(grouping, "Group") == false) {
            logger.warn("Expression '" + expression.getExpressionType() + ": No valid grouping message");
        }

        expressionsByGroup.add(grouping, expression);

        final ExpressionPropertyMetaData[] properties = expression.getPropertyDescriptions();
        for (int j = 0; j < properties.length; j++) {
            final ExpressionPropertyMetaData propertyMetaData = properties[j];
            final String name = propertyMetaData.getName();
            if (StringUtils.isEmpty(name)) {
                logger.warn("Expression '" + expression.getExpressionType() + ": Property without a name");
            }
            final String propertyDisplayName = propertyMetaData.getDisplayName(locale);
            if (isValid(propertyDisplayName, name) == false) {
                logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                        + propertyMetaData.getName() + ": No DisplayName");
            }

            final String propertyGrouping = propertyMetaData.getGrouping(locale);
            if (isValid(propertyGrouping, "Group") == false) {
                logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                        + propertyMetaData.getName() + ": Grouping is not valid");
            }
            final int groupingOrdinal = propertyMetaData.getGroupingOrdinal(locale);
            if (groupingOrdinal == Integer.MAX_VALUE) {
                if (propertyMetaData instanceof DefaultExpressionMetaData) {
                    final DefaultExpressionPropertyMetaData demd = (DefaultExpressionPropertyMetaData) propertyMetaData;
                    missingProperties.add(demd.getKeyPrefix() + "grouping.ordinal=1000");
                }
                logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                        + propertyMetaData.getName() + ": Grouping ordinal is not valid");
            }
            final int ordinal = propertyMetaData.getItemOrdinal(locale);
            if (groupingOrdinal == Integer.MAX_VALUE) {
                if (propertyMetaData instanceof DefaultExpressionMetaData) {
                    final DefaultExpressionPropertyMetaData demd = (DefaultExpressionPropertyMetaData) propertyMetaData;
                    missingProperties.add(demd.getKeyPrefix() + "ordinal=1000");
                }
                logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                        + propertyMetaData.getName() + ": Ordinal is not valid");
            }
            final String propertyDescription = propertyMetaData.getDescription(locale);
            if (isValid(propertyDescription, "") == false) {
                logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                        + propertyMetaData.getName() + ": Description is not valid");
            }
            final String propertyDeprecated = propertyMetaData.getDeprecationMessage(locale);
            if (isValid(propertyDeprecated, "") == false) {
                logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                        + propertyMetaData.getName() + ": Deprecation is not valid");
            }

            final String role = propertyMetaData.getPropertyRole();
            if (isValid(role, "Value") == false) {
                logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                        + propertyMetaData.getName() + ": Role is not valid");
            }
            final Class propertyType = propertyMetaData.getPropertyType();
            if (propertyType == null) {
                logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                        + propertyMetaData.getName() + ": Property Type is not valid");
            }

            // should not crash!
            final PropertyDescriptor propertyDescriptor = propertyMetaData.getBeanDescriptor();

            if (propertyMetaData.isDeprecated()) {
                final String deprecateMessage = propertyMetaData.getDeprecationMessage(locale);
                if (isValid(deprecateMessage, "Deprecated") == false) {
                    logger.warn("Expression '" + expression.getExpressionType() + ": Property "
                            + propertyMetaData.getName() + ": No valid deprecate message");
                }
            }

        }

        try {
            final BeanInfo beanInfo = Introspector.getBeanInfo(expression.getExpressionType());
            final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for (int propIdx = 0; propIdx < descriptors.length; propIdx++) {
                final PropertyDescriptor descriptor = descriptors[propIdx];
                final String key = descriptor.getName();

                if ("runtime".equals(key)) {
                    continue;
                }
                if ("active".equals(key)) {
                    continue;
                }
                if ("preserve".equals(key)) {
                    continue;
                }

                if (descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null) {
                    continue;
                }

                if (expression.getPropertyDescription(key) == null) {
                    logger.warn("Expression '" + expression.getExpressionType()
                            + ": No property definition for " + key);
                    missingPropertyDefs.add("    <property name=\"" + key
                            + "\" mandatory=\"false\" preferred=\"false\" value-role=\"Value\" expert=\"false\" hidden=\"false\"/>");
                }
            }

        } catch (Throwable e) {
            logger.warn("Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor", e);
        }

        System.err.flush();
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
        }

        for (int x = 0; x < missingProperties.size(); x++) {
            final String property = (String) missingProperties.get(x);
            System.out.println(property);
        }

        for (int j = 0; j < missingPropertyDefs.size(); j++) {
            final String def = (String) missingPropertyDefs.get(j);
            System.out.println(def);
        }

        if (missingProperties.isEmpty() == false || missingPropertyDefs.isEmpty() == false) {
            invalidExpressionsCounter += 1;
            missingProperties.clear();
            missingPropertyDefs.clear();
        }
        System.out.flush();
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
        }
    }

    logger.info("Validated " + allExpressions.length + " expressions. Invalid: " + invalidExpressionsCounter
            + " Deprecated: " + deprecatedExpressionsCounter);

    final Object[] keys = expressionsByGroup.keySet().toArray();
    Arrays.sort(keys);
    for (int i = 0; i < keys.length; i++) {
        final Object key = keys[i];
        logger.info("Group: '" + key + "' Size: " + expressionsByGroup.getValueCount(key));
        final Object[] objects = expressionsByGroup.toArray(key);
        for (int j = 0; j < objects.length; j++) {
            ExpressionMetaData metaData = (ExpressionMetaData) objects[j];
            logger.info("   " + metaData.getExpressionType());

        }
    }
}

From source file:Main.java

public static void makeTransient(Class<?> beanClass, String... pdNames) {
    try {//from ww w.  j av  a 2 s  .  c  o m
        BeanInfo info = Introspector.getBeanInfo(beanClass);
        PropertyDescriptor[] descs = info.getPropertyDescriptors();
        if (descs == null) {
            throw new RuntimeException("Cannot access property descriptor for class " + beanClass);
        }
        Map<String, PropertyDescriptor> mapping = new HashMap<String, PropertyDescriptor>();
        for (PropertyDescriptor desc : descs) {
            mapping.put(desc.getName(), desc);
        }
        for (String pdName : pdNames) {
            PropertyDescriptor desc = mapping.get(pdName);
            if (desc == null) {
                throw new RuntimeException("Property " + pdName + " does not exist in " + beanClass);
            }
            desc.setValue("transient", Boolean.TRUE);
        }
    } catch (IntrospectionException ie) {
        throw new RuntimeException(ie);
    }
}

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.j a  v  a  2 s  . c o m

    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:com.esofthead.mycollab.common.interceptor.aspect.AuditLogUtil.java

static public String getChangeSet(Object oldObj, Object newObj, List<String> excludeFields,
        boolean isSelective) {
    Class cl = oldObj.getClass();
    List<AuditChangeItem> changeItems = new ArrayList<>();

    try {/*w  ww  . ja  v a 2 s  .com*/
        BeanInfo beanInfo = Introspector.getBeanInfo(cl, Object.class);

        for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
            String fieldName = propertyDescriptor.getName();
            if (excludeFields.contains(fieldName)) {
                continue;
            }
            String oldProp = getValue(PropertyUtils.getProperty(oldObj, fieldName));

            Object newPropVal;
            try {
                newPropVal = PropertyUtils.getProperty(newObj, fieldName);
            } catch (Exception e) {
                continue;
            }
            String newProp = getValue(newPropVal);

            if (!oldProp.equals(newProp)) {
                if (isSelective && newProp.equals("")) {
                } else {
                    AuditChangeItem changeItem = new AuditChangeItem();
                    changeItem.setField(fieldName);
                    changeItem.setNewvalue(newProp);
                    changeItem.setOldvalue(oldProp);
                    changeItems.add(changeItem);
                }
            }
        }
    } catch (Exception e) {
        LOG.error("There is error when convert changeset", e);
        return null;
    }

    return (changeItems.size() > 0) ? JsonDeSerializer.toJson(changeItems) : null;
}

From source file:de.unentscheidbar.validation.internal.Beans.java

public static PropertyDescriptor property(Class<?> beanClass, String propertyName) {

    try {/* w w w . j a v a  2s. c o  m*/
        BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
        PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors();
        if (propDescriptors == null) {
            throw new IllegalArgumentException("Class " + beanClass.getName()
                    + " does not provide property descriptors in its bean info.");
        }
        for (PropertyDescriptor pd : propDescriptors) {
            if (pd.getName().equals(propertyName)) {
                return pd;
            }
        }
        return null;
    } catch (IntrospectionException e) {
        throw Throwables.propagate(e.getCause());
    }
}

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

private static PropertyDescriptor findDescriptor(final BeanInfo bi, final String propertyName) {
    for (final PropertyDescriptor pd : bi.getPropertyDescriptors()) {
        if (pd.getName().equals(propertyName)) {
            return pd;
        }//from  ww w . j  av a  2  s.  c  om
    }
    return null;
}