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.jdal.vaadin.ui.form.ComboBoxFieldBuilder.java

/**
 * Fill the ComboBox with items from PersistentService
 * @param combo ComboBox to fill//from w  ww.  j  a  va 2s . co m
 * @param clazz Class of Bean containing property name
 * @param name property name
 */
protected void fillComboBox(ComboBox combo, Class<?> clazz, String name) {
    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, name);
    Dao<?, Serializable> service = persistentServiceFactory.createPersistentService(pd.getPropertyType());
    // fill combo
    Iterator<?> iter = service.getAll().iterator();
    while (iter.hasNext())
        combo.addItem(iter.next());
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

/**
 * ???????????//from www.  j  ava2s . c  o m
 * @param propertyName ??
 * @param target 
 * @return ????
 */
public static Class<?> detectDeclaredPropertyType(String propertyName, Class<?> target) {
    Args.checkNotBlank(propertyName);
    Args.checkNotNull(target);
    String[] properties = propertyName.split("\\.");
    Class<?> result = target;
    for (String property : properties) {
        PropertyDescriptor pd = findPropertyDescriptorFor(result, property);
        if (pd == null) {
            if (L.isDebugEnabled()) {
                Map<String, Object> replace = new HashMap<String, Object>(1);
                replace.put("property", propertyName);
                replace.put("class", target.getClass().getName());
                L.debug(Strings.substitute(R.getString("D-UTIL#0015"), replace));
            }
            return null;
        }
        result = pd.getPropertyType();
    }
    return result;
}

From source file:org.beangle.spring.bind.AutoConfigProcessor.java

protected void autowire(String beanName, BeanDefinition mbd) {
    Map<String, PropertyDescriptor> properties = unsatisfiedNonSimpleProperties(mbd);
    for (Map.Entry<String, PropertyDescriptor> entry : properties.entrySet()) {
        String propertyName = entry.getKey();
        PropertyDescriptor pd = entry.getValue();
        if (Object.class.equals(pd.getPropertyType()))
            continue;
        MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
        List<String> beanNames = bindRegistry.getBeanNames(methodParam.getParameterType());
        boolean binded = false;
        if (beanNames.size() == 1) {
            mbd.getPropertyValues().add(propertyName, new RuntimeBeanReference(beanNames.get(0)));
            binded = true;//from w w w  .  j av a2  s  . c o  m
        } else if (beanNames.size() > 1) {
            for (String name : beanNames) {
                if (name.equals(propertyName)) {
                    mbd.getPropertyValues().add(propertyName, new RuntimeBeanReference(propertyName));
                    binded = true;
                    break;
                }
            }
        }
        if (!binded) {
            if (beanNames.isEmpty()) {
                logger.debug(beanName + "'s " + propertyName + "  cannot  found candidate bean");
            } else {
                logger.warn(beanName + "'s " + propertyName + " expected single bean but found {} : {}",
                        beanNames.size(), beanNames);
            }
        }
    }
}

From source file:org.openlegacy.web.DefaultHtmlTableWriter.java

@Override
public void writeTable(List<? extends Object> records, TableDefinition<ColumnDefinition> tableDefinition,
        OutputStream outputStream) {
    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

    Document doc;//  w  w w. ja v a  2 s.com
    try {
        doc = dfactory.newDocumentBuilder().newDocument();

        Element tableTag = (Element) doc.appendChild(doc.createElement(HtmlConstants.TABLE));

        if (records.size() == 0) {
            return;
        }

        Object firstRecord = records.get(0);
        PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(firstRecord);

        // render headers with display name of the fields
        Element rowTag = createTag(tableTag, HtmlConstants.TR);
        for (PropertyDescriptor propertyDescriptor : descriptors) {
            if (TypesUtil.isPrimitive(propertyDescriptor.getPropertyType())) {
                String displayName = "";
                if (tableDefinition == null) {
                    displayName = StringUtil.toDisplayName(propertyDescriptor.getName());
                } else {
                    ColumnDefinition columnDefinition = tableDefinition
                            .getColumnDefinition(propertyDescriptor.getName());
                    if (columnDefinition == null) {
                        continue;
                    }
                    displayName = columnDefinition.getDisplayName();
                }
                Element headerTag = createTag(rowTag, HtmlConstants.TH);
                setCellValue(headerTag, displayName);
            }

        }

        for (Object object : records) {
            rowTag = createTag(tableTag, HtmlConstants.TR);
            for (PropertyDescriptor propertyDescriptor : descriptors) {
                if (TypesUtil.isPrimitive(propertyDescriptor.getPropertyType())) {
                    if (tableDefinition != null) {
                        ColumnDefinition columnDefinition = tableDefinition
                                .getColumnDefinition(propertyDescriptor.getName());
                        if (columnDefinition == null) {
                            continue;
                        }
                    }

                    Object value = propertyDescriptor.getReadMethod().invoke(object);
                    Element cellTag = createTag(rowTag, HtmlConstants.TD);

                    if (value == null) {
                        value = "";
                    }
                    setCellValue(cellTag, String.valueOf(value));
                }
            }
        }

        DomUtils.render(doc, outputStream);
    } catch (Exception e) {
        throw (new GenerationException(e));
    }

}

From source file:com.iorga.webappwatcher.util.BasicParameterHandler.java

@SuppressWarnings("unchecked")
public BasicParameterHandler(final Class<T> ownerClass, final String fieldName) {
    this.ownerClass = ownerClass;
    this.fieldName = fieldName;
    final PropertyDescriptor fieldPropertyDescriptor = findFieldPropertyDescriptor(fieldName);
    this.fieldClass = (Class<V>) fieldPropertyDescriptor.getPropertyType();

    initAccessorMethods(fieldPropertyDescriptor);
}

From source file:org.force66.beantester.tests.AccessorMutatorTest.java

protected void testProperty(Object bean, PropertyDescriptor descriptor) {

    try {/*from   ww w. j  a v  a  2 s  . co m*/
        performNullTest(bean, descriptor);
        for (Object value : generateValues(descriptor.getPropertyType())) {
            performValueTest(bean, descriptor, value);
        }
    } catch (TestFailureException are) {
        throw are;
    } catch (BeanTesterException are) {
        throw are.addContextValue("bean type", bean.getClass().getName()).addContextValue("field",
                descriptor.getName());
    } catch (Exception e) {
        throw new BeanTesterException(e).addContextValue("bean type", bean.getClass().getName())
                .addContextValue("field", descriptor.getName());
    }
}

From source file:org.mule.module.netsuite.api.model.expression.filter.FilterExpressionBuilder.java

private Object parseOperation(String operationName, PropertyDescriptor descriptor) {
    try {//from   ww w .  j  ava  2 s .  c o  m
        return invokeExactStaticMethod(descriptor.getPropertyType(), "fromValue", operationName);
    } catch (Exception e) {
        throw new IllegalArgumentException(String.format("Unsupported operation %s for operator type %s",
                operationName, descriptor.getPropertyType().getSimpleName()));
    }
}

From source file:com.github.dozermapper.core.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");
    }/*from   ww w  .jav  a  2 s  . co  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:gov.nih.nci.caarray.external.v1_0.AbstractCaArrayEntityTest.java

private void setSomeProperty(AbstractCaArrayEntity a) throws Exception {
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(a);
    for (PropertyDescriptor p : pds) {
        if (p.getName().equals("id"))
            continue;
        if (p.getPropertyType() == String.class) {
            p.getWriteMethod().invoke(a, "some String");
            return;
        }//  ww  w  . j  a  v  a 2  s .  c om

        if (p.getPropertyType() == Person.class) {
            p.getWriteMethod().invoke(a, new Person());
            return;
        }

        if (p.getPropertyType() == FileMetadata.class) {
            p.getWriteMethod().invoke(a, new FileMetadata());
            return;
        }

    }

    throw new UnsupportedOperationException("did know how to set a property on " + a.getClass());
}

From source file:org.mule.module.netsuite.api.model.expression.filter.FilterExpressionBuilder.java

public void setTarget(SearchRecordType targetRecordType) {
    target = targetRecordType.newSearchInstance();
    try {/*  w  ww  . j  a va2s  .c om*/
        PropertyDescriptor descriptor = newDescriptor("basic", target);
        basic = (SearchRecord) descriptor.getPropertyType().newInstance();
        descriptor.getWriteMethod().invoke(target, basic);
    } catch (Exception e) {
        throw soften(e);
    }
}