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
?
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.
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).
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.
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.
If you use SpringFramework, "BeanWrapperImpl" its the answer you are looking for:
BeanWrapperImpl wrapper = new BeanWrapperImpl(sourceObject);
Object attributeValue = wrapper.getPropertyValue("attribute");
BeanUtils is very powerful because it supports nested properties. E.G "bean.prop1.prop2", handle Map
s 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With