Example usage for org.apache.commons.beanutils PropertyUtils getPropertyType

List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyType

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getPropertyType.

Prototype

public static Class getPropertyType(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the Java Class representing the property type of the specified property, or null if there is no such property for the specified bean.

For more details see PropertyUtilsBean.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Recording recording = new Recording();
    recording.setTitle("Magical Mystery Tour");

    Class type = PropertyUtils.getPropertyType(recording, "title");
    System.out.println("type = " + type.getName());

    String value = (String) PropertyUtils.getProperty(recording, "title");
    System.out.println("value = " + value);
}

From source file:com.aosa.main.utils.tools.AOSACopyUtil.java

/**
 * Description<code>Copy properties of orig to dest Exception the Entity and Collection Type</code>      <br>
 * By mutou at 2011-8-30 ?11:35:29   <br>
 * Object      <br>/*from   w ww  .  j  a  v a2  s .  com*/
 * @param dest
 * @param orig
 * @return the dest bean
 * @throws
 */
@SuppressWarnings("rawtypes")
public static Object copyProperties(Object dest, Object orig) {
    if (dest == null || orig == null) {
        return dest;
    }
    PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
    try {
        for (int i = 0; i < destDesc.length; i++) {
            Class destType = destDesc[i].getPropertyType();
            Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());
            if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) {
                if (!Collection.class.isAssignableFrom(origType)) {
                    try {
                        Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
                        PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
                    } catch (Exception ex) {
                    }
                }
            }
        }
        return dest;
    } catch (Exception ex) {
        throw new AOSARuntimeException(ex);
    }
}

From source file:com.autentia.tnt.faces.components.property.BigDecimalPropertyTest.java

@Test
public void BigDecimalPropTest() {

    try {/*  w w  w.  jav a2  s.c om*/
        System.out.println(PropertyUtils.getPropertyType(BookMock.class, "price"));
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    final Property[] properties = PropertyFactory.build(BookMock.class, false, "price");

    Assert.assertEquals("com.autentia.tnt.faces.components.property.BigDecimalProperty",
            properties[0].getClass().getName());

}

From source file:com.hengyi.japp.sap.convert.impl.FieldCopyStructure.java

protected void initOther(Object bean, JCoRecord record) throws Exception {
    beanPropertyType = PropertyUtils.getPropertyType(bean, beanPropertyName);
}

From source file:com.zauberlabs.commons.mom.style.impl.JaxbStyle.java

/**Answers if the type of a property is list, given a target object and propertyName **/
private boolean isPropertyTypeList(final Object destination, final String propertyName)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Class clazz = PropertyUtils.getPropertyType(destination, propertyName);
    if (clazz == null) {
        return List.class
                .isAssignableFrom(NaiveProperties.getter(destination.getClass(), propertyName).getReturnType());
    }/*from  w w w.  j ava  2  s  .c  o  m*/
    return List.class.isAssignableFrom(clazz);
}

From source file:com.opengamma.component.factory.engine.FunctionBlacklistComponentFactory.java

@Override
public void init(final ComponentRepository repo, final LinkedHashMap<String, String> configuration) {
    String classifier = "default";
    final Iterator<Map.Entry<String, String>> itr = configuration.entrySet().iterator();
    while (itr.hasNext()) {
        final Map.Entry<String, String> conf = itr.next();
        if ("classifier".equals(conf.getKey())) {
            classifier = conf.getValue();
        } else {//from  w  w w. ja  va2  s. c o  m
            try {
                if (conf.getValue().startsWith("::")) {
                    final Class<?> property = PropertyUtils.getPropertyType(_bean, conf.getKey());
                    final ComponentInfo info = repo.findInfo(property, conf.getValue().substring(2));
                    if (info != null) {
                        BeanUtils.setProperty(_bean, conf.getKey(), repo.getInstance(info));
                    } else {
                        BeanUtils.setProperty(_bean, conf.getKey(), conf.getValue());
                    }
                } else {
                    BeanUtils.setProperty(_bean, conf.getKey(), conf.getValue());
                }
            } catch (Exception e) {
                throw new OpenGammaRuntimeException("invalid property '" + conf.getKey() + "' on " + _bean, e);
            }
        }
        itr.remove();
    }
    final FunctionBlacklist blacklist = _bean.getObjectCreating();

    ComponentInfo infoRO = new ComponentInfo(FunctionBlacklist.class, classifier);
    infoRO.addAttribute(ComponentInfoAttributes.LEVEL, 1);
    infoRO.addAttribute(ComponentInfoAttributes.REMOTE_CLIENT_JAVA, RemoteFunctionBlacklist.class);
    repo.registerComponent(infoRO, blacklist);

    if (blacklist instanceof ManageableFunctionBlacklist) {
        ComponentInfo infoMng = new ComponentInfo(ManageableFunctionBlacklist.class, classifier);
        infoMng.addAttribute(ComponentInfoAttributes.LEVEL, 1);
        infoMng.addAttribute(ComponentInfoAttributes.REMOTE_CLIENT_JAVA,
                RemoteManageableFunctionBlacklist.class);
        repo.registerComponent(infoMng, blacklist);
    }
}

From source file:com.aosa.main.utils.tools.AOSACopyUtil.java

/**
 * Description<code>Copy properties of orig to dest Exception the Entity and Collection Type</code>      <br>
 * By mutou at 2011-8-30 ?11:36:11   <br>
 * Object      <br>/*from  w ww . java  2  s  .  co m*/
 * @param dest
 * @param orig
 * @param ignores
 * @return the dest bean
 * @throws
 */
@SuppressWarnings("rawtypes")
public static Object copyProperties(Object dest, Object orig, String[] ignores) {
    if (dest == null || orig == null) {
        return dest;
    }

    PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
    try {
        for (int i = 0; i < destDesc.length; i++) {
            if (contains(ignores, destDesc[i].getName())) {
                continue;
            }
            Class destType = destDesc[i].getPropertyType();
            Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());
            if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) {
                if (!Collection.class.isAssignableFrom(origType)) {
                    Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
                    PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
                }
            }
        }
        return dest;
    } catch (Exception ex) {
        throw new AOSARuntimeException(ex);
    }
}

From source file:edu.umn.msi.tropix.common.test.BeanTest.java

public static void testBeanProperties(final Object testBean, final HashMultimap<Class<?>, Object> typeObjects) {
    try {/*from   w ww  .  j  a  v a2s .c om*/
        @SuppressWarnings("unchecked")
        final Map<String, ?> propertyMap = PropertyUtils.describe(testBean);
        for (final String propertyName : propertyMap.keySet()) {
            if (propertyName.equals("class") || !PropertyUtils.isWriteable(testBean, propertyName)) {
                continue;
            }
            final Class<?> type = PropertyUtils.getPropertyType(testBean, propertyName);
            Collection<?> objects = null;
            for (final Class<?> typeQuery : typeObjects.keySet()) {
                if (typeQuery.isAssignableFrom(type)) {
                    objects = typeObjects.get(typeQuery);
                }
            }
            boolean useEquals = true;
            if (objects == null) {
                useEquals = false;
                try {
                    objects = Lists.<Object>newArrayList(EasyMock.createMock(type));
                } catch (final Exception e) {
                    // Cannot instantiate mock of this type
                    continue;
                }
            }
            for (final Object expectedObject : objects) {
                PropertyUtils.setProperty(testBean, propertyName, expectedObject);
                final Object object = PropertyUtils.getProperty(testBean, propertyName);
                if (useEquals) {
                    assert object.equals(expectedObject) : "Expected " + expectedObject + " obtained " + object;
                } else {
                    assert object == expectedObject : "Expected " + expectedObject + " obtained " + object;
                }
            }
        }
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.safetys.framework.jmesa.util.ItemUtils.java

/**
 * Get the Class for the property./*from   w  ww.  j  av  a 2s . c  o  m*/
 * 
 * @param items The Collection of Beans or Maps.
 * @param property The Bean attribute or Map key.
 * @return The Class for the property.
 */
public static Class<?> getPropertyClassType(Collection<?> items, String property) throws Exception {

    Object item = items.iterator().next();

    if (item instanceof Map) {
        for (Object object : items) {
            Map<?, ?> map = (Map<?, ?>) object;
            Object val = map.get(property);

            if (val == null) {
                continue;
            }

            return val.getClass();
        }
    }

    Class<?> type = null;
    try {
        type = PropertyUtils.getPropertyType(item, property);
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Had problems getting property type by object, trying reflection...");
        }
        type = BeanUtils.getPropertyType(item, property);
    }
    return type;
}

From source file:de.boksa.rt.rest.response.parser.processor.FieldProcessorRegistry.java

public FieldProcessor getTicketFieldProcessor(RTTicket ticket, String ticketFieldName) {
    FieldProcessor fieldProcessor = null;

    Matcher m = RTCustomField.PATTERN_CUSTOM_FIELD_NAME.matcher(ticketFieldName);

    if (m.matches()) {
        fieldProcessor = customFieldProcessors.get(m.group(1));
        if (fieldProcessor == null) {
            fieldProcessor = DefaultCustomFieldProcessor.getInstance();
        }/*from   w w w . j  av a  2s.com*/
    } else {
        fieldProcessor = standardFieldProcessors.get(ticketFieldName);
        if (fieldProcessor == null) {
            try {
                fieldProcessor = typeFieldProcessors
                        .get(PropertyUtils.getPropertyType(ticket, StringUtils.uncapitalize(ticketFieldName)));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    return (fieldProcessor != null) ? fieldProcessor : DefaultFieldProcessor.getInstance();
}