public static void shallowCopyFieldState ( final Object src , final Object dest ) { if ( src == null ) { throw new IllegalArgumentException ( "Source for field copy cannot be null" ) ; } if ( dest == null ) { throw new IllegalArgumentException ( "Destination for field copy cannot be null" ) ; } if ( ! src . getClass ( ) . isAssignableFrom ( dest . getClass ( ) ) ) { throw new IllegalArgumentException ( "Destination class [" + dest . getClass ( ) . getName ( ) + "] must be same or subclass as source class [" + src . getClass ( ) . getName ( ) + "]" ) ; } doWithFields ( src . getClass ( ) , new FieldCallback ( ) { @ Override public void doWith ( Field field ) throws IllegalArgumentException , IllegalAccessException { makeAccessible ( field ) ; Object srcValue = field . get ( src ) ; field . set ( dest , srcValue ) ; } } , COPYABLE_FIELDS ) ; }
Given the source object and the destination which must be the same class or a subclass copy all fields including inherited fields . Designed to work on objects with public no - arg constructors .