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.gameye.psp.image.utils.TPropertyUtilsBean.java

/**
 * add by Majian/*w w w. j  a v a 2  s . c  o m*/
 * ??
 * **/
public Class getDestType(Object bean, String name) {

    // Validate method parameters
    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }

    // Return the requested result
    if (bean instanceof DynaBean) {
        // All DynaBean properties are writeable
        return null;
    } else {
        try {
            PropertyDescriptor desc = getPropertyDescriptor(bean, name);
            if (desc != null) {
                Class c = desc.getPropertyType();
                return c;
            } else {
                return null;
            }
        } catch (IllegalAccessException e) {
            return null;
        } catch (InvocationTargetException e) {
            return null;
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

}

From source file:org.codehaus.enunciate.modules.xfire_client.IntrospectingTypeCreator.java

/**
 * Looks up the type for the property.//  www.  ja v a 2  s  .c  om
 *
 * @param property The property.
 * @return The type.
 */
public Type createType(PropertyDescriptor property) {
    return createType(property.getPropertyType());
}

From source file:org.dozer.util.ReflectionUtils.java

public static DeepHierarchyElement[] getDeepFieldHierarchy(Class<?> parentClass, String field,
        HintContainer deepIndexHintContainer) {
    if (!MappingUtils.isDeepMapping(field)) {
        MappingUtils.throwMappingException("Field does not contain deep field delimitor");
    }/* w  ww  .j a v a 2  s  .  c  o m*/

    StringTokenizer toks = new StringTokenizer(field, DozerConstants.DEEP_FIELD_DELIMITER);
    Class<?> latestClass = parentClass;
    DeepHierarchyElement[] hierarchy = new DeepHierarchyElement[toks.countTokens()];
    int index = 0;
    int hintIndex = 0;
    while (toks.hasMoreTokens()) {
        String aFieldName = toks.nextToken();
        String theFieldName = aFieldName;
        int collectionIndex = -1;

        if (aFieldName.contains("[")) {
            theFieldName = aFieldName.substring(0, aFieldName.indexOf("["));
            collectionIndex = Integer
                    .parseInt(aFieldName.substring(aFieldName.indexOf("[") + 1, aFieldName.indexOf("]")));
        }

        PropertyDescriptor propDescriptor = findPropertyDescriptor(latestClass, theFieldName,
                deepIndexHintContainer);
        DeepHierarchyElement r = new DeepHierarchyElement(propDescriptor, collectionIndex);

        if (propDescriptor == null) {
            MappingUtils
                    .throwMappingException("Exception occurred determining deep field hierarchy for Class --> "
                            + parentClass.getName() + ", Field --> " + field
                            + ".  Unable to determine property descriptor for Class --> "
                            + latestClass.getName() + ", Field Name: " + aFieldName);
        }

        latestClass = propDescriptor.getPropertyType();
        if (toks.hasMoreTokens()) {
            if (latestClass.isArray()) {
                latestClass = latestClass.getComponentType();
            } else if (Collection.class.isAssignableFrom(latestClass)) {
                Class<?> genericType = determineGenericsType(parentClass.getClass(), propDescriptor);

                if (genericType == null && deepIndexHintContainer == null) {
                    MappingUtils.throwMappingException(
                            "Hint(s) or Generics not specified.  Hint(s) or Generics must be specified for deep mapping with indexed field(s). Exception occurred determining deep field hierarchy for Class --> "
                                    + parentClass.getName() + ", Field --> " + field
                                    + ".  Unable to determine property descriptor for Class --> "
                                    + latestClass.getName() + ", Field Name: " + aFieldName);
                }
                if (genericType != null) {
                    latestClass = genericType;
                } else {
                    latestClass = deepIndexHintContainer.getHint(hintIndex);
                    hintIndex += 1;
                }
            }
        }
        hierarchy[index++] = r;
    }

    return hierarchy;
}

From source file:com.thesoftwarefactory.vertx.web.model.Form.java

/**
 * Build a new Form instance from the specified class
 * /*w w  w .ja  v  a  2 s.  c om*/
 * @param cls
 * @param fieldPrefix: if not null, the prefix is prepended to each field name 
 * @return
 */
public final static <T> Form<T> fromClass(Class<T> cls, String fieldPrefix) {
    Objects.requireNonNull(cls);

    Form<T> result = new Form<T>();
    if (fieldPrefix != null) {
        result.fieldPrefix = fieldPrefix;
    }

    // discover properties
    for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(cls)) {
        if (property.getReadMethod() != null && property.getWriteMethod() != null) {
            // the property has a getter and setter
            String fieldName = fieldPrefix != null ? fieldPrefix + property.getName() : property.getName();
            result.addField(result.new Field(fieldName, Form.fieldTypefromClass(property.getPropertyType())));
        }
    }

    return result;
}

From source file:com.subakva.formicid.converters.FileSetConverter.java

public FileSet convert(Class type, Object value) {
    if (value == null || value.equals(Undefined.instance)) {
        throw new ConversionException("No value specified");
    }/*from w ww  .ja  v  a 2s . com*/
    if (value instanceof FileSet) {
        return (FileSet) value;
    } else if (value instanceof Scriptable) {
        FileSet fileSet = (FileSet) container.getProject().createDataType("fileset");
        Scriptable options = (Scriptable) value;
        Object[] ids = options.getIds();
        for (int i = 0; i < ids.length; i++) {
            String id = (String) ids[i];
            Object val = options.get(id, options);
            try {
                PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(fileSet, id);
                Class<?> propertyType = descriptor.getPropertyType();
                Converter converter = container.getConverter(propertyType);
                Object converted = converter.convert(propertyType, val);
                PropertyUtils.setProperty(fileSet, id, converted);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        return fileSet;
    } else if (value instanceof String) {
        FileSet fileSet = (FileSet) container.getProject().createDataType("fileset");
        fileSet.setDir(new File("."));
        FilenameSelector selector = new FilenameSelector();
        selector.setName((String) value);
        fileSet.add(selector);
        return fileSet;
    } else {
        throw new ConversionException("" + value);
    }
}

From source file:it.geosolutions.unredd.onlinestats.ppio.JAXBStatisticConfigurationPPIO.java

private Object populate(Map m, Class c) throws InstantiationException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException {
    Object o = c.newInstance();//from  ww w  .  java2  s .c om
    for (Object eo : m.entrySet()) {
        Entry e = (Entry) eo;
        Object v = e.getValue();
        Class<?> ec = v.getClass();
        if (Map.class.isAssignableFrom(ec)) {
            PropertyUtilsBean pub = new PropertyUtilsBean();
            String ek = (String) e.getKey();
            PropertyDescriptor pd = pub.getPropertyDescriptor(o, ek);
            Class pt = pd.getPropertyType();
            if (List.class.isAssignableFrom(pt)) {
                Class k = Object.class;
                if (ek.equals("classifications")) {
                    k = ClassificationLayer.class;
                    //} else if (ek.equals("topics")) {
                    //   k = String.class;
                    //} else if (ek.equals("stats")) {
                    //   k = StatsType.class;
                }
                List l = new ArrayList();
                // This is the point where I give up.
                Object ojeto = populate((Map) e.getValue(), k);
                l.add(ojeto);
                m.put(ek, l);
            } else {
                Object p = populate((Map) e.getValue(), pt);
                m.put(ek, p);
            }
        }
    }
    BeanUtils.populate(o, m);
    return o;
}

From source file:com.mycollab.db.persistence.service.DefaultService.java

@Override
public void removeByCriteria(S criteria, Integer accountId) {
    boolean isValid = false;
    try {/*ww w  . j  a  v a 2s  .  co m*/
        PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(criteria);

        for (PropertyDescriptor descriptor : propertyDescriptors) {
            String propName = descriptor.getName();
            if ((descriptor.getPropertyType().getGenericSuperclass() == SearchField.class)
                    && (PropertyUtils.getProperty(criteria, propName) != null)) {
                isValid = true;
                break;
            }

        }
    } catch (Exception e) {
        LOG.debug("Error while validating criteria", e);
    }
    if (isValid) {
        getSearchMapper().removeByCriteria(criteria);
    }

}

From source file:com.esofthead.mycollab.core.persistence.service.DefaultService.java

@Override
public void removeByCriteria(S criteria, int accountId) {
    boolean isValid = false;
    try {/*from  ww  w .  j  av a2  s .c  o m*/
        PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(criteria);

        for (PropertyDescriptor descriptor : propertyDescriptors) {
            String propName = descriptor.getName();
            if ((descriptor.getPropertyType().getGenericSuperclass() == SearchField.class)
                    && (PropertyUtils.getProperty(criteria, propName) != null)) {
                isValid = true;
                break;
            }

        }
    } catch (Exception e) {
        LOG.debug("Error while validating criteria", e);
    }
    if (isValid) {
        getSearchMapper().removeByCriteria(criteria);
    }

}

From source file:org.openmrs.api.handler.OpenmrsObjectSaveHandler.java

/**
 * This sets the uuid property on the given OpenmrsObject if it is non-null.
 *
 * @see org.openmrs.api.handler.RequiredDataHandler#handle(org.openmrs.OpenmrsObject,
 *      org.openmrs.User, java.util.Date, java.lang.String)
 * @should set empty string properties to null
 * @should not set empty string properties to null for AllowEmptyStrings annotation
 * @should not trim empty strings for AllowLeadingOrTrailingWhitespace annotation
 * @should trim strings without AllowLeadingOrTrailingWhitespace annotation
 * @should trim empty strings for AllowEmptyStrings annotation
 *//* ww  w  . j a v  a 2  s  .co  m*/
public void handle(OpenmrsObject openmrsObject, User creator, Date dateCreated, String reason) {
    if (openmrsObject.getUuid() == null) {
        openmrsObject.setUuid(UUID.randomUUID().toString());
    }

    //Set all empty string properties, that do not have the AllowEmptyStrings annotation, to null.
    //And also trim leading and trailing white space for properties that do not have the
    //AllowLeadingOrTrailingWhitespace annotation.
    PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(openmrsObject);
    for (PropertyDescriptor property : properties) {

        if (property.getPropertyType() == null) {
            continue;
        }

        // Ignore properties that don't have a getter (e.g. GlobalProperty.valueReferenceInternal) or
        // don't have a setter (e.g. Patient.familyName)
        if (property.getWriteMethod() == null || property.getReadMethod() == null) {
            continue;
        }

        // Ignore properties that have a deprecated getter or setter
        if (property.getWriteMethod().getAnnotation(Deprecated.class) != null
                || property.getReadMethod().getAnnotation(Deprecated.class) != null) {
            continue;
        }

        //We are dealing with only strings
        if (!property.getPropertyType().equals(String.class)) {
            continue;
        }

        try {
            Object value = PropertyUtils.getProperty(openmrsObject, property.getName());
            if (value == null) {
                continue;
            }

            Object valueBeforeTrim = value;
            if (property.getWriteMethod().getAnnotation(AllowLeadingOrTrailingWhitespace.class) == null) {
                value = ((String) value).trim();

                //If we have actually trimmed any space, set the trimmed value.
                if (!valueBeforeTrim.equals(value)) {
                    PropertyUtils.setProperty(openmrsObject, property.getName(), value);
                }
            }

            //Check if user is interested in setting empty strings to null
            if (property.getWriteMethod().getAnnotation(AllowEmptyStrings.class) != null) {
                continue;
            }

            if ("".equals(value)
                    && !(openmrsObject instanceof Voidable && ((Voidable) openmrsObject).isVoided())) {
                //Set to null only if object is not already voided
                PropertyUtils.setProperty(openmrsObject, property.getName(), null);
            }
        } catch (UnsupportedOperationException ex) {
            // there is no need to log this. These should be (mostly) silently skipped over 
            if (log.isInfoEnabled()) {
                log.info(
                        "The property " + property.getName() + " is no longer supported and should be ignored.",
                        ex);
            }
        } catch (InvocationTargetException ex) {
            if (log.isWarnEnabled()) {
                log.warn("Failed to access property " + property.getName() + "; accessor threw exception.", ex);
            }
        } catch (Exception ex) {
            throw new APIException("failed.change.property.value", new Object[] { property.getName() }, ex);
        }
    }
}

From source file:com.easyget.commons.csv.bean.CsvToBean.java

private boolean trimmableProperty(PropertyDescriptor prop) {
    return !prop.getPropertyType().getName().contains("String");
}