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.ajax4jsf.builder.config.ComponentBaseBean.java

/**
 * @param superClass/*from   w w  w  .ja v a 2  s  .  c o m*/
 */
private void checkPropertiesForClass(Class<?> superClass) {
    getLog().debug("Check properties for class " + superClass.getName());
    // get all property descriptors
    PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(superClass);
    // for all properties, add it to component. If property have not abstract getter/setter ,
    // add it with exist = true . If property in list of hidden names, set hidden = true.
    PropertyBean property;
    for (int i = 0; i < properties.length; i++) {
        PropertyDescriptor descriptor = properties[i];
        if (!containProperty(descriptor.getName())) {
            if (isIgnorableProperty(superClass, descriptor.getName())) {
                continue;
            }
            Class<?> type = descriptor.getPropertyType();
            getLog().debug("Register property  " + descriptor.getName() + " with type name "
                    + type.getCanonicalName());
            property = new PropertyBean();
            property.setName(descriptor.getName());
            property.setDescription(descriptor.getShortDescription());
            property.setDisplayname(descriptor.getDisplayName());
            property.setClassname(descriptor.getPropertyType().getCanonicalName());
            property.setExist(true);
            addProperty(property);
        } else {
            // Load and check property.
            getLog().debug("Check  property  " + descriptor.getName());
            property = (PropertyBean) this.properties.get(descriptor.getName());
            if (property.getClassname() == null) {
                property.setClassname(descriptor.getPropertyType().getCanonicalName());
            } else {
                if (!property.getClassname().equals(descriptor.getPropertyType().getCanonicalName())) {
                    String message = "Class " + property.getClassname() + " for property " + property.getName()
                            + " not equals with real bean property type: "
                            + descriptor.getPropertyType().getCanonicalName();
                    getLog().error(message);
                    //throw new IllegalArgumentException(message);
                }
            }
            if (property.getDescription() == null) {
                property.setDescription(descriptor.getShortDescription());
            }
            if (property.getDisplayname() == null) {
                property.setDisplayname(descriptor.getDisplayName());
            }
            property.setExist(true);
        }
        Method getter = descriptor.getReadMethod();
        Method setter = descriptor.getWriteMethod();
        // Abstract methods
        if (null != setter && null != getter) {
            if ((Modifier.isAbstract(getter.getModifiers()) && Modifier.isAbstract(setter.getModifiers()))
                    || superClass.isInterface()) {
                getLog().debug("Detect as abstract property  " + descriptor.getName());
                property.setExist(false);
            }
        }

        if (null == setter || (!Modifier.isPublic(setter.getModifiers()))) {
            getLog().debug("Detect as hidden property  " + descriptor.getName());
            property.setHidden(true);
        }
        if (isAttachedProperty(property)) {
            property.setAttachedstate(true);
        }
        if (property.isInstanceof("javax.faces.el.MethodBinding")
                || property.isInstanceof("javax.faces.el.ValueBinding")) {
            property.setElonly(true);
        }

    }
}

From source file:ca.sqlpower.architect.swingui.TestPlayPen.java

/**
  * Returns true if an instance of the given property type is of a mutable class.
  * Throws an exception if it lacks a case for the given type.
  * //  ww w .j  ava 2 s.  co  m
  * @param property The property that should be checked for mutability.
  */
private boolean isPropertyInstanceMutable(PropertyDescriptor property) {
    if (property.getPropertyType() == String.class) {
        return false;
    } else if (Enum.class.isAssignableFrom(property.getPropertyType())) {
        return false;
    } else if (property.getPropertyType() == Boolean.class || property.getPropertyType() == Boolean.TYPE) {
        return false;
    } else if (property.getPropertyType() == Double.class || property.getPropertyType() == Double.TYPE) {
        return false;
    } else if (property.getPropertyType() == Integer.class || property.getPropertyType() == Integer.TYPE) {
        return false;
    } else if (property.getPropertyType() == Color.class) {
        return false;
    } else if (property.getPropertyType() == Font.class) {
        return false;
    } else if (property.getPropertyType() == Set.class) {
        return true;
    } else if (property.getPropertyType() == List.class) {
        return true;
    } else if (property.getPropertyType() == Point.class) {
        return true;
    } else {
        throw new RuntimeException("This test case lacks a value for " + property.getName() + " (type "
                + property.getPropertyType().getName() + ") in isPropertyInstanceMutable()");
    }
}

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

/**
 * This method determines the Class of the elements in the collectionName passed in.
 *
 * @param boClass Class that the collectionName collection exists in.
 * @param collectionName the name of the collection you want the element class for
 * @return collection element type/*from  ww  w .  j  a  va  2s.c  o  m*/
 */
public static Class getCollectionElementClass(Class boClass, String collectionName) {
    if (boClass == null) {
        throw new IllegalArgumentException("invalid (null) boClass");
    }
    if (StringUtils.isBlank(collectionName)) {
        throw new IllegalArgumentException("invalid (blank) collectionName");
    }

    PropertyDescriptor propertyDescriptor = null;

    String[] intermediateProperties = collectionName.split("\\.");
    Class currentClass = boClass;

    for (int i = 0; i < intermediateProperties.length; ++i) {

        String currentPropertyName = intermediateProperties[i];
        propertyDescriptor = buildSimpleReadDescriptor(currentClass, currentPropertyName);

        if (propertyDescriptor != null) {

            Class type = propertyDescriptor.getPropertyType();
            if (Collection.class.isAssignableFrom(type)) {
                currentClass = getLegacyDataAdapter().determineCollectionObjectType(currentClass,
                        currentPropertyName);
            } else {
                currentClass = propertyDescriptor.getPropertyType();
            }
        }
    }

    return currentClass;
}

From source file:org.oddjob.framework.WrapDynaClass.java

/**
 * Introspect our bean class to identify the supported properties.
 *//*from   w w  w.  ja va 2s.c o m*/
protected void introspect(Class<?> beanClass) {
    Set<String> mismatched = new HashSet<String>();

    // first find simple and indexed properties via usual means.
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    for (int i = 0; i < descriptors.length; ++i) {
        PropertyDescriptor descriptor = descriptors[i];

        String propertyName = descriptor.getName();

        DynaProperty dynaProperty;
        // indexed property?
        if (descriptor instanceof IndexedPropertyDescriptor) {
            dynaProperty = new DynaProperty(propertyName, descriptor.getPropertyType(),
                    ((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType());
        }
        // else a simple property.
        else {
            dynaProperty = new DynaProperty(propertyName, descriptor.getPropertyType());
        }

        propertiesMap.put(propertyName, dynaProperty);

        // update readable writable
        if (MethodUtils.getAccessibleMethod(descriptor.getReadMethod()) != null) {
            readableProperties.add(propertyName);
        }
        if (MethodUtils.getAccessibleMethod(descriptor.getWriteMethod()) != null) {
            writableProperties.add(propertyName);
        }
    }

    // now find mapped properties.
    Method[] methods = beanClass.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        Method method = methods[i];

        // methods beginning with get could be properties.          
        if (!method.getName().startsWith("get") && !method.getName().startsWith("set")) {
            continue;
        }

        String propertyName = method.getName().substring(3);
        // get on it's own is not a property
        if (propertyName.length() == 0) {
            continue;
        }

        // lowercase first letter
        propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);

        Class<?>[] args = method.getParameterTypes();

        DynaProperty dynaProperty = null;

        boolean readable = false;
        boolean writable = false;

        // is mapped property?
        if (method.getName().startsWith("get") && Void.TYPE != method.getReturnType() && args.length == 1
                && args[0] == String.class) {
            DynaProperty existing = (DynaProperty) propertiesMap.get(propertyName);
            if (existing != null && !existing.isMapped()) {
                mismatched.add(propertyName);
                continue;
            }
            dynaProperty = new DynaProperty(propertyName, Map.class, method.getReturnType());
            readable = true;
        } else if (args.length == 2 && args[0] == String.class && Void.TYPE == method.getReturnType()) {
            DynaProperty existing = (DynaProperty) propertiesMap.get(propertyName);
            if (existing != null && !existing.isMapped()) {
                mismatched.add(propertyName);
                continue;
            }
            dynaProperty = new DynaProperty(propertyName, Map.class, args[1]);
            writable = true;
        } else {
            continue;
        }
        propertiesMap.put(propertyName, dynaProperty);

        // update readable writable
        if (readable) {
            readableProperties.add(propertyName);
        }
        if (writable) {
            writableProperties.add(propertyName);
        }
    }

    for (String element : mismatched) {
        propertiesMap.remove(element);
        readableProperties.remove(element);
        writableProperties.remove(element);
    }

    properties = (DynaProperty[]) propertiesMap.values().toArray(new DynaProperty[0]);

}

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

/**
 * Extract the values for all columns in the current row.
 * <p>//from  w  w w .j  a va2s .co  m
 * Utilizes public setters and result set metadata.
 * 
 * @see java.sql.ResultSetMetaData
 */
public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
    Assert.state(this.mappedClass != null, "Mapped class was not specified");
    Object mappedObject = BeanUtils.instantiateClass(this.mappedClass);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
    initBeanWrapper(bw);

    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null);

    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase();
        PropertyDescriptor pd = (PropertyDescriptor) this.mappedFields.get(column);
        if (pd != null) {
            try {
                Object value = getColumnValue(rs, index, pd);
                if (logger.isDebugEnabled() && rowNumber == 0) {
                    logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type "
                            + pd.getPropertyType());
                }
                bw.setPropertyValue(pd.getName(), value);
                if (populatedProperties != null) {
                    populatedProperties.add(pd.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException(
                        "Unable to map column " + column + " to property " + pd.getName(), ex);
            }
        }
    }

    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields "
                + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
    }

    return mappedObject;
}

From source file:org.mule.modules.zuora.ZuoraModule.java

@MetaDataRetriever
public MetaData getMetadata(MetaDataKey key) throws Exception {

    Class<?> cls = getClassForType(key.getId());

    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(cls);

    Map<String, MetaDataModel> fieldMap = new HashMap<String, MetaDataModel>();
    for (PropertyDescriptor pd : pds) {
        if (pd.getReadMethod() != null && pd.getWriteMethod() != null) {
            fieldMap.put(pd.getName(), getFieldMetadata(pd.getPropertyType()));
        }//  w  w  w.java 2 s  . co  m
    }

    DefaultDefinedMapMetaDataModel mapModel = new DefaultDefinedMapMetaDataModel(fieldMap, key.getId());

    return new DefaultMetaData(mapModel);
}

From source file:edu.ku.brc.af.ui.forms.DataSetterForObj.java

public void setFieldValue(Object dataObj, String fieldName, Object data) {
    //log.debug("fieldName["+fieldName+"] dataObj["+dataObj+"] data ["+data+"]");
    try {// w ww. j  a va 2 s  .com
        PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(dataObj, fieldName.trim());
        if (descr != null) {
            // Check to see if the class of the data we have is different than the one we are trying to set into
            // This typically happens when we have a TextField with a number and it needs to be converted from a 
            // String representation of the number to the actually numeric type like from String to Integer or Short
            Object dataVal = data;
            Class<?> fieldClass = descr.getPropertyType();
            if (data != null) {

                if (dataVal.getClass() != fieldClass && !fieldClass.isAssignableFrom(dataVal.getClass())) {
                    dataVal = UIHelper.convertDataFromString(dataVal.toString(), fieldClass);
                }
            } /*else // Data is Null
              {
              if (fieldClass.isAssignableFrom(Collection.class) || fieldClass.isAssignableFrom(Set.class))
              {
                  return;
              }
              }*/
            if (dataVal instanceof String && ((String) dataVal).isEmpty()) {
                dataVal = null;
            }
            Method setter = PropertyUtils.getWriteMethod(descr);
            if (setter != null && dataObj != null) {
                args[0] = dataVal;
                //log.debug("fieldname["+fieldName+"] dataObj["+dataObj+"] data ["+dataVal+"] ("+(dataVal != null ? dataVal.getClass().getSimpleName() : "")+")");
                setter.invoke(dataObj, dataVal);
            } else {
                log.error("dataObj was null - Trouble setting value field named["
                        + (fieldName != null ? fieldName.trim() : "null") + "] in data object ["
                        + (dataObj != null ? dataObj.getClass().toString() : "null") + "]");
            }
        } else {
            log.error("We could not find a field named[" + fieldName.trim() + "] in data object ["
                    + dataObj.getClass().toString() + "]");
        }
    } catch (java.lang.reflect.InvocationTargetException ex) {
        log.error("Trouble setting value field named[" + (fieldName != null ? fieldName.trim() : "null")
                + "] in data object [" + (dataObj != null ? dataObj.getClass().toString() : "null") + "]");
        log.error(ex);
        ex.printStackTrace();
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataSetterForObj.class, ex);

    } catch (Exception ex) {
        log.error("Trouble setting value field named[" + (fieldName != null ? fieldName.trim() : "null")
                + "] in data object [" + (dataObj != null ? dataObj.getClass().toString() : "null") + "]");
        log.error(ex);
        ex.printStackTrace();
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataSetterForObj.class, ex);
    }
}

From source file:org.mypsycho.beans.DefaultInvoker.java

public boolean isCollection(PropertyDescriptor prop) {
    if (prop instanceof MappedPropertyDescriptor) {
        return true;
    }//from   w  w  w  .j  a v  a2s . c  o m
    if (prop instanceof IndexedPropertyDescriptor) {
        return true;
    }
    return isCollection(prop.getPropertyType());
}

From source file:org.kuali.rice.krad.web.bind.UifViewBeanWrapper.java

/**
 * {@inheritDoc}// w w w .  ja v a  2s  . c o m
 */
@Override
public Class<?> getPropertyType(String propertyName) throws BeansException {
    try {
        PropertyDescriptor pd = getPropertyDescriptorInternal(propertyName);
        if (pd != null) {
            return pd.getPropertyType();
        }

        // Maybe an indexed/mapped property...
        Object value = super.getPropertyValue(propertyName);
        if (value != null) {
            return value.getClass();
        }

        // Check to see if there is a custom editor,
        // which might give an indication on the desired target type.
        Class<?> editorType = guessPropertyTypeFromEditors(propertyName);
        if (editorType != null) {
            return editorType;
        }
    } catch (InvalidPropertyException ex) {
        // Consider as not determinable.
    }

    return null;
}