Created
February 27, 2015 21:48
-
-
Save esarbanis/f234912f9b328043b6ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PropertiesUtis { | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment