[ACCEPTED]-How to copy properties from one Java bean to another?-javabeans
I guess if you look at the source code of 7 BeanUtils, it will show you how to do this 6 without actually using BeanUtils.
If you 5 simply want to create a copy of a POJO (not 4 quite the same thing as copying the properties 3 from one POJO to another), you could change 2 the source bean to implement the clone() method 1 and the Cloneable interface.
I had the same problem when developing an 5 app for Google App Engine, where I couldn't 4 use BeanUtils due to commons Logging restrictions. Anyway, I 3 came up with this solution and worked just 2 fine for me.
public static void copyProperties(Object fromObj, Object toObj) {
Class<? extends Object> fromClass = fromObj.getClass();
Class<? extends Object> toClass = toObj.getClass();
try {
BeanInfo fromBean = Introspector.getBeanInfo(fromClass);
BeanInfo toBean = Introspector.getBeanInfo(toClass);
PropertyDescriptor[] toPd = toBean.getPropertyDescriptors();
List<PropertyDescriptor> fromPd = Arrays.asList(fromBean
.getPropertyDescriptors());
for (PropertyDescriptor propertyDescriptor : toPd) {
propertyDescriptor.getDisplayName();
PropertyDescriptor pd = fromPd.get(fromPd
.indexOf(propertyDescriptor));
if (pd.getDisplayName().equals(
propertyDescriptor.getDisplayName())
&& !pd.getDisplayName().equals("class")) {
if(propertyDescriptor.getWriteMethod() != null)
propertyDescriptor.getWriteMethod().invoke(toObj, pd.getReadMethod().invoke(fromObj, null));
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Any enhancements or recomendations 1 are really welcome.
Another alternative is MapStruct which generates 4 mapping code at build time, resulting in 3 type-safe mappings which don't require any 2 dependencies at runtime (Disclaimer: I'm 1 the author of MapStruct).
Hey friends just use my created ReflectionUtil 11 class for copy one bean values to another 10 similar bean.
This class will also copy 9 Collections object.
https://github.com/vijayshegokar/Java/blob/master/Utility/src/common/util/reflection/ReflectionUtil.java
Note: This bean must have 8 similar variables name with type and have 7 getter and setters for them.
Now more functionalities 6 are added. You can also copy one entity 5 data to its bean. If one entity has another 4 entity in it then you can pass map option 3 for runtime change of inner entity to its 2 related bean.
Eg.
ParentEntity parentEntityObject = getParentDataFromDB();
Map<Class<?>, Class<?>> map = new HashMap<Class<?>, Class<?>>();
map.put(InnerBean1.class, InnerEntity1.class);
map.put(InnerBean2.class, InnerEntity2.class);
ParentBean parent = ReflectionUtil.copy(ParentBean.class, parentEntityObject, map);
This case is very useful 1 when your Entities contains relationship.
Have a look at the JavaBeans API, in particular the Introspector class. You 4 can use the BeanInfo metadata to get and set properties. It 3 is a good idea to read up on the JavaBeans specification if you 2 haven't already. It also helps to have a 1 passing familiarity with the reflection API.
There is no simple way to do it. Introspector 7 and the Java beans libraries are monolithic 6 - BeanUtils is a simple wrapper around this 5 and works well. Not having libraries just 4 to not have libraries is a bad idea in general 3 - there's a reason it's commons to begin 2 with - common functionality that should 1 exist with Java, but doesn't.
I ran into some problems with Introspector.getBeanInfo
not returning 2 all the properties, so I ended up implementing 1 a field copy instead of property copy.
public static <T> void copyFields(T target, T source) throws Exception{
Class<?> clazz = source.getClass();
for (Field field : clazz.getFields()) {
Object value = field.get(source);
field.set(target, value);
}
}
You can achieve it using Java Reflection API.
public static <T> void copy(T target, T source) throws Exception {
Class<?> clazz = source.getClass();
for (Field field : clazz.getDeclaredFields()) {
if (Modifier.isPrivate(field.getModifiers()))
field.setAccessible(true);
Object value = field.get(source);
field.set(target, value);
}
}
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.