Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate to BeanUtils.getProperty()

I am looking for alternate to BeanUtils.getProperty().only reason i want to have alternate is to avoid end user having one more dependency.

I am working on a custom constraints and this is piece of code i have

final Object firstObj = BeanUtils.getProperty(value, this.firstFieldName);
final Object secondObj = BeanUtils.getProperty(value, this.secondFieldName);

Since i need to get these two properties out of object. Is there any alternate for this without any third party system or i need to copy this piece of code from BeanUtilsBean?

like image 889
Umesh Awasthi Avatar asked Oct 16 '13 11:10

Umesh Awasthi


People also ask

Why we use BeanUtils copyProperties?

Copying properties of one object to another object is often tedious and error-prone for developers. BeanUtils class provides a copyProperties method that copies the properties of source object to target object where the property name is same in both objects.

How do I copy one bean to another bean?

Using copyProperties(Object source, Object target, Class<?> editable) This method copies the property values of the given source bean into the given target bean, only setting properties defined in the given "editable" class (or interface).

What is Commons BeanUtils used for?

Commons BeanUtils is a collection of utilities that makes working with beans and bean properties much easier. This project contains utilities that allow one to retrieve a bean property by name, sort beans by a property, translate beans to maps, and more.

Does BeanUtils use reflection?

As they both ultimately use Reflection you aren't likely to notice much difference, unless the higher-level API is doing things you don't need done. See also java. beans.


2 Answers

If you use SpringFramework, "BeanWrapperImpl" its the answer you are looking for:

BeanWrapperImpl wrapper = new BeanWrapperImpl(sourceObject);

Object attributeValue = wrapper.getPropertyValue("attribute");
like image 193
nekperu15739 Avatar answered Sep 18 '22 12:09

nekperu15739


BeanUtils is very powerful because it supports nested properties. E.G "bean.prop1.prop2", handle Maps as beans and DynaBeans.

For example:

 HashMap<String, Object> hashMap = new HashMap<String, Object>();
 JTextArea value = new JTextArea();
 value.setText("jArea text");
 hashMap.put("jarea", value);

 String property = BeanUtils.getProperty(hashMap, "jarea.text");
 System.out.println(property);

Thus in your case I would just write a private method that uses the java.beans.Introspector.

private Object getPropertyValue(Object bean, String property)
        throws IntrospectionException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException {
    Class<?> beanClass = bean.getClass();
    PropertyDescriptor propertyDescriptor = getPropertyDescriptor(
            beanClass, property);
    if (propertyDescriptor == null) {
        throw new IllegalArgumentException("No such property " + property
                + " for " + beanClass + " exists");
    }

    Method readMethod = propertyDescriptor.getReadMethod();
    if (readMethod == null) {
        throw new IllegalStateException("No getter available for property "
                + property + " on " + beanClass);
    }
    return readMethod.invoke(bean);
}

private PropertyDescriptor getPropertyDescriptor(Class<?> beanClass,
        String propertyname) throws IntrospectionException {
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    PropertyDescriptor[] propertyDescriptors = beanInfo
            .getPropertyDescriptors();
    PropertyDescriptor propertyDescriptor = null;
    for (int i = 0; i < propertyDescriptors.length; i++) {
        PropertyDescriptor currentPropertyDescriptor = propertyDescriptors[i];
        if (currentPropertyDescriptor.getName().equals(propertyname)) {
            propertyDescriptor = currentPropertyDescriptor;
        }

    }
    return propertyDescriptor;
}
like image 45
René Link Avatar answered Sep 18 '22 12:09

René Link