public static void copyProperties ( Object dest , Object orig ) { try { if ( orig != null && dest != null ) { BeanUtils . copyProperties ( dest , orig ) ; PropertyUtils putils = new PropertyUtils ( ) ; PropertyDescriptor origDescriptors [ ] = putils . getPropertyDescriptors ( orig ) ; for ( PropertyDescriptor origDescriptor : origDescriptors ) { String name = origDescriptor . getName ( ) ; if ( "class" . equals ( name ) ) { continue ; // No point in trying to set an object's class } Class propertyType = origDescriptor . getPropertyType ( ) ; if ( ! Boolean . class . equals ( propertyType ) && ! ( Boolean . class . equals ( propertyType ) ) ) continue ; if ( ! putils . isReadable ( orig , name ) ) { //because of bad convention Method m = orig . getClass ( ) . getMethod ( "is" + name . substring ( 0 , 1 ) . toUpperCase ( ) + name . substring ( 1 ) , ( Class < ? > [ ] ) null ) ; Object value = m . invoke ( orig , ( Object [ ] ) null ) ; if ( putils . isWriteable ( dest , name ) ) { BeanUtilsBean . getInstance ( ) . copyProperty ( dest , name , value ) ; } } } } } catch ( Exception e ) { throw new DJException ( "Could not copy properties for shared object: " + orig + ", message: " + e . getMessage ( ) , e ) ; } }
This takes into account objects that breaks the JavaBean convention and have as getter for Boolean objects an isXXX method .