Beautiful Java: Reflection and the BeanCopier

When reading about reflection on the beautiful code website I thought about solving some problems with reflection and finding new solutions to old problems. One problem is boring code when writing a copy constructor. A copy constructor is a constructor which takes another object and copies it’s attributes. This is often useful when copying objects or moving objects to different system layers. One example would look like this:

There are some pitfalls, especially that you only create shallow copies. And there is always clone() and Serializable, which can also be used to create deep copies without such pitfalls. JBoss provides a fast implementation of this technique. But often copy constructors are quite useful if you’re careful and know what you do. I recently had a discussion on how to support domain objects in different parts of a system. One part might call an entity Employee, another part might call it Person. To make them talk together you can either create a composite, an adapter, a wrapper or use a copy constructor (or find a different solution). In such a case a shallow copy is all you want, because the new object should have the same “identity” as the first and not be a copy.

This should be easier to implement. Copying bean attributes for large objects isn’t much fun, leads to long constructors and unmaintanable code. What about changes in Person? New attributes? So I tinkered with Java reflection and ended with this:

I’ve written a BeanCopier which can – well you guessed it – copy beans. The syntax is easy to learn. A BeanCopier has a copy method which takes a source, a copy and the attributes which should be specially handled. By default all attributes which have the same name and type in source and copy are copied. But if they have different names, like name and firstName, you need to specify “name:firstName”. If a property shouldn’t be copied, you can specify “property:” without a target attribute.

The copy method looks like this (the generics are not needed but I was also tinkering with caching where it was quite handy):

and the copyAttribute method:

The code is rough but works. The type test can be improved to work with super types.BeanCopier can be used outside of constructors, often a wise choice. And be careful with “copying” other objects, you might only create a shallow copy.

Of course, I could have taken a look at commons BeanUtils, but isn’t it much more fun to tinker and find a nice solution yourself sometimes?

Thanks for listening.

188 thoughts on “Beautiful Java: Reflection and the BeanCopier”

  1. Pingback: best
  2. Pingback: fucoxanthin
  3. Pingback: geniux pills
  4. Pingback: intelligex
  5. Pingback: intellux
  6. Pingback: radian-c
  7. Pingback: supreme antler
  8. Pingback: genius reviews
  9. Pingback: testomax
  10. Pingback: alpha f1
  11. Pingback: alpha fuel
  12. Pingback: alpha man pro
  13. Pingback: alpha peak
  14. Pingback: alpha xtrm
  15. Pingback: caralluma extract
  16. Pingback: cognifocus
  17. Pingback: garcinia melt
  18. Pingback: max synapse
  19. Pingback: nerventrax
  20. Pingback: nutra forskolin
  21. Pingback: testorip
  22. Pingback: testoroar
  23. Pingback: zynev
  24. Pingback: forskolin slim
  25. Pingback: garcinia cleanse
  26. Pingback: garcinia xt
  27. Pingback: forskolin fuel
  28. Pingback: vitapulse
  29. Pingback: vitapulse reviews
  30. Pingback: vitapulse review
  31. Pingback: pure colon detox
  32. Pingback: somnapure
  33. Pingback: reviews vitapulse
  34. Pingback: vitapulse vitamins
  35. Pingback: vitapulse scam
  36. Pingback: vitapulse
  37. Pingback: vitapulse
  38. Pingback: probiotic america
  39. Pingback: taurus traits
  40. Pingback: Diabetes 60 System
  41. Pingback: Ecomaxx Review
  42. Pingback: Maxtropin Review
  43. Pingback: Tea tox
  44. Pingback: Tai Cheng
  45. Pingback: vita pulse
  46. Pingback: niagen
  47. Pingback: imp source
  48. Pingback: check here
  49. Pingback: navigate here
  50. Pingback: find more info
  51. Pingback: Vert Shock Reviews
  52. Pingback: Bonuses
  53. Pingback: click site
  54. Pingback: original site
  55. Pingback: check over here
  56. Pingback: have a peek here
  57. Pingback: news
  58. Pingback: click site
  59. Pingback: Visit Website
  60. Pingback: her latest blog
  61. Pingback: his explanation
  62. Pingback: find more
  63. Pingback: his explanation
  64. Pingback: my site
  65. Pingback: Read More Here
  66. Pingback: my site
  67. Pingback: original site
  68. Pingback: click
  69. Pingback: check it out
  70. Pingback: original site
  71. Pingback: anchor
  72. Pingback: my response
  73. Pingback: here
  74. Pingback: dig this
  75. Pingback: anchor
  76. Pingback: check my site
  77. Pingback: anchor
  78. Pingback: see this here
  79. Pingback: Read More Here
  80. Pingback: check my site
  81. Pingback: Visit Website
  82. Pingback: her latest blog
  83. Pingback: his explanation
  84. Pingback: 60 day fix reviews
  85. Pingback: 60 day fix review
  86. Pingback: click here
  87. Pingback: tires
  88. Pingback: rims
  89. Pingback: wheels
  90. Pingback: click here
  91. Pingback: click here
  92. Pingback: read more

Comments are closed.