0

I am facing issue in copying properties from one bean to another.

I am aware about the copyProperties() which can be used here to copy from source bean to destination bean probably if both the beans are of same type. My issue here is that I want to copy first 50 properties on the first call and next 50 properties on the second call.

Is there any way in which I can copy first 50 configs only?

Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
Bhaskar
  • 337
  • 6
  • 21

2 Answers2

1

You can do it only by using reflection.

@Zhuinden made a good point to start with.

To make the approach usefull for arbitrary classes, create a Map name -> field:

Map<String,Field> asMap( Field[] fields ){
  Map<String,Field> m = new HashMap<String,Field>();
  for( Field f : fields ){
    f.setAccessible( true );
    m.put( f.getName(), f );
  }
  return m;
}

then use it like:

Map<String,Field> trg = asMap( target.getClass().getDeclaredFields() );

int counter = 50;

for( Field f : Source.getClass().getDeclaredFields() ){
  f.setAccessible( true );
  Field fieldTarget = trg.get( f.getName() );
  if( null != fieldTarget ){
    fieldTarget.set(target, f.get(source));
    counter--;
  }
  if( 0 == counter ) break;
}
injecteer
  • 20,038
  • 4
  • 45
  • 89
0

Something like this would work using reflection (added an error check at the start as per https://stackoverflow.com/a/3738954/2413303 ):

if(source.getClass() != target.getClass())
{
    throw new RuntimeException("The target and source classes don't match!");
}

Field[] fieldSources = source.getClass().getDeclaredFields();
Arrays.sort(fieldSources);
Field[] fieldTargets = target.getClass().getDeclaredFields();
Arrays.sort(fieldTargets);
//source and target class is the same so their fields length is the same

for(int i = 50*n; i < fieldSources.length && i < 50*n+50; i++)
{
    Field fieldSource = fieldSources[i];
    Field fieldTarget = fieldTargets[i];
    fieldSource.setAccessible(true);
    fieldTarget.setAccessible(true);
    fieldTarget.set(target, fieldSource.get(source));
}
Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • main problem with this approach: it would work ONLY with the same classes or classes with exactly the same properties – injecteer Aug 05 '14 at 13:09
  • @injecteer Indeed, but he said `if both the beans are of same type.`, so I went with that assumption. – EpicPandaForce Aug 05 '14 at 13:12
  • A better question is whether you actually need to get the Fields twice, or if once is sufficient because they're the same type to begin with. – EpicPandaForce Aug 05 '14 at 13:16
  • @Zhuinden.. when I am trying to get the declaredfield using array.sort() as above, I am get a compile time error- "Type mismatch: cannot convert from void to Field[]" – Bhaskar Aug 06 '14 at 09:57
  • However, I am able to achieve it using copyProperties. But, my problem is that I want to copy properties in the batches of 50. With each call I will copy 50 properties. I am just trying to figure out an appropriate logic for copying 50 properties. – Bhaskar Aug 06 '14 at 10:01
  • `for(int i = 50*n; i < fieldSources.length && i < 50*n+50; i++)` – EpicPandaForce Aug 06 '14 at 11:18
  • fixed the compile time error, I assumed `Arrays.sort()` returns the array that was being sorted but apparently not. – EpicPandaForce Aug 06 '14 at 11:19