Example usage for java.beans PropertyDescriptor getReadMethod

List of usage examples for java.beans PropertyDescriptor getReadMethod

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor getReadMethod.

Prototype

public synchronized Method getReadMethod() 

Source Link

Document

Gets the method that should be used to read the property value.

Usage

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   ww  w .j  ava2s.  c  om*/

        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:com.ginema.api.reflection.ReflectionUtils.java

public static Method getGetterMethod2(Class clazz, String name) throws IntrospectionException {

    PropertyDescriptor desc[] = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
    for (PropertyDescriptor d : desc) {
        return d.getReadMethod();
    }//from w w w. j a va 2s.  c  o m
    return null;
}

From source file:org.apache.qpid.disttest.message.ParticipantAttributeExtractor.java

public static Method getPropertyReadMethod(Object targetObject, PropertyDescriptor propertyDescriptor) {
    final Method readMethod = propertyDescriptor.getReadMethod();

    if (readMethod == null) {
        throw new RuntimeException(
                "No read method for property " + propertyDescriptor.getName() + " on " + targetObject);
    }//from   w w  w .ja va  2s  .  co  m
    return readMethod;
}

From source file:net.eusashead.hateoas.header.impl.PropertyUtil.java

public static Object getValue(Object target, String name) {
    PropertyDescriptor property = BeanUtils.getPropertyDescriptor(target.getClass(), name);
    try {//from   www .  j  a va  2  s. c  o  m
        return property.getReadMethod().invoke(target);
    } catch (Exception e) {
        throw new RuntimeException("Unable to get value for target " + target.getClass());
    }
}

From source file:es.pode.soporte.auditoria.registrar.BeanDescripcion.java

/** 
 * Mtodo para recuperar informacin de un objeto a travs de reflexin 
 *   //from   w  w  w  .ja  v  a2  s .c o  m
 * @param objeto Objeto al cual se le realiza la reflexin 
 * @param atributo Valor que se recupera del objeto
 * @return valor Se devuelve el valor del atributo buscado
 */
public static String describe(Object objeto, String atributo) {

    if (objeto == null)
        return null;

    Object valor = null;

    Class clase = objeto.getClass();
    if (clase.isArray() || java.util.Collection.class.isAssignableFrom(clase))
        log.warn("El atributo es un array y debera ser un String");
    else {
        log("Reflexin del objeto: " + objeto);

        BeanWrapper wrapper = new BeanWrapperImpl(objeto);
        PropertyDescriptor descriptors[] = wrapper.getPropertyDescriptors();

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

            if (pd.getReadMethod() != null && pd.getWriteMethod() != null) {
                String name = pd.getName();

                /* Capturamos el valor del atributo que nos interesa */
                if (name.equals(atributo)) {
                    log("Nombre atributo: " + name);
                    valor = wrapper.getPropertyValue(name);

                    /* Si el valor es nulo registramos un "" */
                    if (valor == null) {
                        log("El valor del atributo interceptado es nulo");
                        return null;
                    } else
                        return valor.toString();
                }
            }
        }
    }

    return null;
}

From source file:org.jdal.text.FormatUtils.java

/**
 * Field for property name, if any/*from   w w  w  . j  a  v  a2  s.c o m*/
 * @param pd PropertyDescriptor
 * @return property field or null if none.
 */
public static Field getDeclaredField(PropertyDescriptor pd) {

    try {
        return pd.getReadMethod().getDeclaringClass().getDeclaredField(pd.getName());
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Cannot access to field: " + pd.getName());

        return null;
    }
}

From source file:de.micromata.genome.util.bean.PropertyDescriptorUtils.java

/**
 * Find anotation first on getter than from field.
 *
 * @param <T> the generic type// w  w w .j  a v a 2s.co  m
 * @param beanClazz the bean clazz
 * @param pd the pd
 * @param anotclass the anotclass
 * @return the t
 */
public static <T extends Annotation> T findAnotation(Class<?> beanClazz, PropertyDescriptor pd,
        Class<T> anotclass) {
    Method rm = pd.getReadMethod();
    if (rm != null) {
        T anot = rm.getAnnotation(anotclass);
        if (anot != null) {
            return anot;
        }
    }
    Field field = PrivateBeanUtils.findField(beanClazz, pd.getName());
    if (field == null) {
        return null;
    }
    T anot = field.getAnnotation(anotclass);
    return anot;
}

From source file:net.mojodna.searchable.SearchableBeanUtils.java

/**
 * Gets the name of the property containing the bean's id.
 * /* ww w  . j  av a  2s.  c o m*/
 * @param clazz Class to reflect on.
 * @return Name of the id property.
 */
public static String getIdPropertyName(final Class<? extends Searchable> clazz) {
    // look for Searchable.ID annotation
    final PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(clazz);
    for (final PropertyDescriptor descriptor : pds) {
        if (null != descriptor && null != descriptor.getReadMethod()
                && descriptor.getReadMethod().isAnnotationPresent(Searchable.ID.class)) {
            return descriptor.getName();
        }
    }

    return SearchableBeanUtils.ID_PROPERTY_NAME;
}

From source file:org.bibsonomy.util.ObjectUtils.java

/**
 * copies all property values (all properties that have a getter and setter) from the source to the target
 * //from www  .  jav  a2 s. co m
 * @param <T> 
 * @param source the source object
 * @param target the target object
 */
public static <T> void copyPropertyValues(final T source, final T target) {
    try {
        final BeanInfo biSource = Introspector.getBeanInfo(source.getClass());
        final BeanInfo biTarget = Introspector.getBeanInfo(target.getClass());

        /* 
         * source can be a subtype of target
         * to ensure that a setter of the subtype isn't called choose the array
         * that contains less properties
         */
        final PropertyDescriptor[] sourceProperties = biSource.getPropertyDescriptors();
        final PropertyDescriptor[] targetProperties = biTarget.getPropertyDescriptors();
        final PropertyDescriptor[] copyProperties = sourceProperties.length > targetProperties.length
                ? targetProperties
                : sourceProperties;

        /*
         * loop through all properties
         */
        for (final PropertyDescriptor d : copyProperties) {
            final Method getter = d.getReadMethod();
            final Method setter = d.getWriteMethod();

            if (present(getter) && present(setter)) {
                // get the value from the source
                final Object value = getter.invoke(source, (Object[]) null);
                // and set in in the target
                setter.invoke(target, value);
            }
        }
    } catch (Exception ex) {
        log.error("error while copying property values from an object " + source.getClass() + " to an object "
                + target.getClass(), ex);
    }
}

From source file:net.mojodna.searchable.SearchableBeanUtils.java

/**
 * Gets the name of the property that should be used to generate a search
 * excerpt.//from  w  w w  .  j  a v  a2  s. c om
 * 
 * @param clazz Class to reflect on.
 * @return Name of the excerptable property; null if none present.
 */
public static String getExcerptPropertyName(final Class<? extends Result> clazz) {
    final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz);
    for (final PropertyDescriptor d : descriptors) {
        if (AnnotationUtils.isAnnotationPresent(d.getReadMethod(), Searchable.Excerptable.class))
            return d.getName();
    }

    return null;
}