Example usage for java.beans PropertyDescriptor getPropertyType

List of usage examples for java.beans PropertyDescriptor getPropertyType

Introduction

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

Prototype

public synchronized Class<?> getPropertyType() 

Source Link

Document

Returns the Java type info for the property.

Usage

From source file:org.grails.datastore.mapping.model.AbstractPersistentEntity.java

public boolean hasProperty(String name, Class type) {
    final PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(getJavaClass(), name);
    return pd != null && pd.getPropertyType().equals(type);
}

From source file:com.clican.pluto.common.support.spring.BeanPropertyRowMapper.java

/**
 * Retrieve a JDBC object value for the specified column.
 * <p>/*from   w  ww.  ja  va  2 s.c o m*/
 * The default implementation calls
 * {@link JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)}.
 * Subclasses may override this to check specific value types upfront, or to
 * post-process values return from <code>getResultSetValue</code>.
 * 
 * @param rs
 *            is the ResultSet holding the data
 * @param index
 *            is the column index
 * @param pd
 *            the bean property that each result object is expected to match
 *            (or <code>null</code> if none specified)
 * @return the Object value
 * @throws SQLException
 *             in case of extraction failure
 * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue(java.sql.ResultSet,
 *      int, Class)
 */
protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException {
    Object obj = JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());
    if (obj instanceof Date) {
        return new Date(((Date) obj).getTime());
    } else {
        return obj;
    }
}

From source file:com.ebay.jetstream.management.HtmlResourceFormatter.java

protected void formatProperty(Object bean, PropertyDescriptor pd) throws Exception {
    PrintWriter pw = getWriter();
    Method getter = pd.getReadMethod();
    Class<?> pclass = pd.getPropertyType();
    ManagedAttribute attr = getter.getAnnotation(ManagedAttribute.class);
    String text = attr != null ? attr.description() : null;
    if (CommonUtils.isEmptyTrimmed(text)) {
        text = pd.getDisplayName();//w  w w.ja va  2s  .  co m
    } else {
        text = pd.getDisplayName() + " (" + text + ")";
    }
    pw.print(text + ": " + pclass.getName() + " = ");
    getter.setAccessible(true);
    Object value = getter.invoke(bean);
    Method setter = pd.getWriteMethod();
    attr = setter == null ? null : setter.getAnnotation(ManagedAttribute.class);
    boolean isComplex = !(String.class.isAssignableFrom(pclass) || ClassUtils.isPrimitiveOrWrapper(pclass));
    if (isComplex) {
        value = StringEscapeUtils.escapeXml(getSerializer().getXMLStringRepresentation(value));
    }
    if (attr == null) {
        if (isComplex) {
            pushElement("code", null);
        }
        pw.println(value);
        if (isComplex) {
            popElement();
        }
    } else {
        pw.println(attr.description());
        pushElement("form",
                "action=" + makePath(getPrefix(), getPath(), isComplex ? "?form" : "?" + pd.getName())
                        + " method=" + (isComplex ? "POST" : "GET"));
        if (isComplex) {
            pw.print("<TEXTAREA name=" + pd.getName() + " rows=4 cols=32>" + value + "</TEXTAREA>");
        } else {
            pw.print("<input type=text name=" + pd.getName() + " value=\"" + value + "\"/>");
        }
        pw.println("<input type=submit Value=\"Go\"/>");
        popElement();
    }
    pw.println("<P/>");
}

From source file:org.kuali.rice.krad.util.BeanPropertyComparator.java

/**
 * Compare two JavaBeans by the properties given to the constructor.
 * /*from   ww w.  j  av a  2s .  c  om*/
 * @param o1 Object The first bean to get data from to compare against
 * @param o2 Object The second bean to get data from to compare
 * @return int negative or positive based on order
 */
public int compare(Object o1, Object o2) {
    int compared = 0;

    try {
        for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) {
            String currentProperty = i.next().toString();

            // choose appropriate comparator
            Comparator currentComparator = null;
            try {
                PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1,
                        currentProperty);
                Class propertyClass = propertyDescriptor.getPropertyType();
                if (propertyClass.equals(String.class)) {
                    currentComparator = this.stringComparator;
                } else if (TypeUtils.isBooleanClass(propertyClass)) {
                    currentComparator = this.booleanComparator;
                } else {
                    currentComparator = this.genericComparator;
                }
            } catch (NullPointerException e) {
                throw new BeanComparisonException(
                        "unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e);
            }

            // compare the values
            Object value1 = PropertyUtils.getProperty(o1, currentProperty);
            Object value2 = PropertyUtils.getProperty(o2, currentProperty);
            /* Fix for KULRICE-5170 : BeanPropertyComparator throws exception when a null value is found in sortable non-string data type column */
            if (value1 == null && value2 == null)
                return 0;
            else if (value1 == null)
                return -1;
            else if (value2 == null)
                return 1;
            /* End KULRICE-5170 Fix*/
            compared = currentComparator.compare(value1, value2);
        }
    } catch (IllegalAccessException e) {
        throw new BeanComparisonException("unable to compare property values", e);
    } catch (NoSuchMethodException e) {
        throw new BeanComparisonException("unable to compare property values", e);
    } catch (InvocationTargetException e) {
        throw new BeanComparisonException("unable to compare property values", e);
    }

    return compared;
}

From source file:com.qccr.livtrip.web.template.BaseDirective.java

/**
 * ?// w w w . j  a  va2s.  co m
 * 
 * @param params
 *            ?
 * @param type
 *            ?
 * @param ignoreProperties
 *            
 * @return 
 */
protected List<Filter> getFilters(Map<String, TemplateModel> params, Class<?> type, String... ignoreProperties)
        throws TemplateModelException {
    List<Filter> filters = new ArrayList<Filter>();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(type);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        String propertyName = propertyDescriptor.getName();
        Class<?> propertyType = propertyDescriptor.getPropertyType();
        if (!ArrayUtils.contains(ignoreProperties, propertyName) && params.containsKey(propertyName)) {
            Object value = FreeMarkerUtils.getParameter(propertyName, propertyType, params);
            filters.add(Filter.eq(propertyName, value));
        }
    }
    return filters;
}

From source file:org.opoo.oqs.core.mapper.BeanPropertyMapper.java

/**
 * Map<String, ?> in JDK1.5/*  w  ww. j a v a  2 s .  c om*/
 *
 * @param rs ResultSet
 * @param rowNum int
 * @param type Class
 * @return Map
 * @throws SQLException
 */
private Map getValues(ResultSet rs, int rowNum, Class type) throws SQLException {
    Map map = new HashMap();
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(type);
    for (int i = 0; i < mappers.length; i++) {
        PropertyMapper pm = mappers[i];
        if (PropertyTypeAware.class.isInstance(pm)) { //(pm instanceof PropertyTypeAware)
            PropertyDescriptor pd = findPropertyDescriptor(pds, mappers[i].getName());
            if (pd != null) {
                ((PropertyTypeAware) pm).setPropertyType(pd.getPropertyType());
            }
        }
        Object value = pm.map(rs, rowNum);
        map.put(pm.getName(), value);
        //setProperty(target, mappers[i].getName(), value);
    }
    return map;
}

From source file:org.kuali.rice.krad.datadictionary.DataDictionary.java

/**
 * @param targetClass/*w w w  .j  a  v  a  2  s.com*/
 * @param propertyName
 * @return true if the given propertyName names a Collection property of the given class
 * @throws CompletionException if there is a problem accessing the named property on the given class
 */
public static boolean isCollectionPropertyOf(Class targetClass, String propertyName) {
    boolean isCollectionPropertyOf = false;

    PropertyDescriptor propertyDescriptor = buildReadDescriptor(targetClass, propertyName);
    if (propertyDescriptor != null) {
        Class clazz = propertyDescriptor.getPropertyType();

        if ((clazz != null) && Collection.class.isAssignableFrom(clazz)) {
            isCollectionPropertyOf = true;
        }
    }

    return isCollectionPropertyOf;
}

From source file:org.openmobster.core.mobileObject.TestBeanSyntax.java

private Object initializeIndexedProperty(Object parentObject, String property,
        PropertyDescriptor propertyMetaData) throws Exception {
    Object element = null;/*from  w  w w  .jav a  2s . c o m*/

    //Find the Class of the elementType
    Class elementType = null;

    if (!propertyMetaData.getPropertyType().isArray()) {
        ParameterizedType returnType = (ParameterizedType) propertyMetaData.getReadMethod()
                .getGenericReturnType();
        Type[] actualTypes = returnType.getActualTypeArguments();
        for (Type actualType : actualTypes) {
            elementType = (Class) actualType;
        }
    } else {
        elementType = propertyMetaData.getPropertyType().getComponentType();
    }

    //An IndexedProperty
    Object indexedProperty = PropertyUtils.getProperty(parentObject, propertyMetaData.getName());

    //Initialize the IndexedProperty (An Array or Collection)
    if (indexedProperty == null) {
        if (propertyMetaData.getPropertyType().isArray()) {
            //TODO: Remove hardcoded array size                     
            PropertyUtils.setProperty(parentObject, propertyMetaData.getName(),
                    Array.newInstance(elementType, 1));
        } else {
            //Handle Collection Construction
            PropertyUtils.setProperty(parentObject, propertyMetaData.getName(), new ArrayList());
        }
        indexedProperty = PropertyUtils.getProperty(parentObject, propertyMetaData.getName());
    }

    //Check to see if the index specified by the field requires creation of new
    //element
    try {
        element = PropertyUtils.getIndexedProperty(parentObject, property);
    } catch (IndexOutOfBoundsException iae) {
        Object newlyInitialized = elementType.newInstance();

        if (!propertyMetaData.getPropertyType().isArray()) {
            ((Collection) indexedProperty).add(newlyInitialized);
        } else {
            //TODO: Remove hardcoded array index
            Array.set(indexedProperty, 0, newlyInitialized);
        }

        element = newlyInitialized;
    }

    return element;
}

From source file:com.dp2345.template.directive.BaseDirective.java

/**
 * ?/*ww w  . jav  a  2 s. c o  m*/
 * 
 * @param params
 *            ?
 * @param type
 *            ?
 * @param ignoreProperties
 *            
 * @return 
 */
protected List<Filter> getFilters(Map<String, TemplateModel> params, Class<?> type, String... ignoreProperties)
        throws TemplateModelException {
    List<Filter> filters = new ArrayList<Filter>();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(type);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        String propertyName = propertyDescriptor.getName();
        Class<?> propertyType = propertyDescriptor.getPropertyType();
        if (!ArrayUtils.contains(ignoreProperties, propertyName) && params.containsKey(propertyName)) {
            Object value = FreemarkerUtils.getParameter(propertyName, propertyType, params);
            filters.add(Filter.eq(propertyName, value));
        }
    }
    return filters;
}

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"/* w  w  w .j  ava  2s. c  om*/
 * @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;
}