1

I use java 1.5. I am to create a common function which takes 2 objects (source class and target class) which call all getters and get all values from source object and, call setters of target object . My intention is to get all values from source object and set those values to target object . Both classes of parsing objects are different but all encapsulations are equal each other (variables, getters and setter methods are similar).

Here I am to use reflection and then, call getters and setter. As I know, using reflection arises performance and memory issues. Is there any way to come up this with only java without any other third party jars ? Any implementation, methodology to do this ? Any source, sample codes are highly appreciated.

Débora
  • 5,816
  • 28
  • 99
  • 171
  • 1
    Note : if you only want to use getters/setters, use `Introspection`. It uses reflection to expose properties and read/write methods. – Arnaud Denoyelle Apr 25 '14 at 08:38
  • @ArnaudDenoyelle . I thanks you for your quick reponse. Please let me know any performance or memory issues will occure because of Introspection ? – Débora Apr 25 '14 at 08:40
  • 4
    Have you tried using reflection and observed these issues, or are you just guessing that it will be a problem for you? – Keppil Apr 25 '14 at 08:42
  • @Keppil. Thanks for your attention.I have to stick with Java 1.5 and everywhere in Internet, it says with statics that Reflection has performance issues. (I don't want to make an argument, but this http://stackoverflow.com/questions/435553/java-reflection-performance ) My proposed function will be a frequent caller, and therefore I have to consider the performance. – Débora Apr 25 '14 at 08:45
  • 1
    To quote another answer to the same question (http://stackoverflow.com/a/547999/1343161) _Premature optimization is the root of all evil_. Try it first, and then consider alternative approaches if it doesn't work. Performance issues is not an absolute term, it doesn't mean that everyone will have problems. – Keppil Apr 25 '14 at 08:50
  • I think reflection must be used only when there is no alternative solution. Calling getters and setters dynamically needs Reflection. You are comparing performance of reflection against what?. – TheLostMind Apr 25 '14 at 08:54
  • @WhoAmI, Actually I have nothing to compare with, The only alternative can be writing seperate lines of codes that do my job which consume many lines. That s why I am looking one function for this. :) – Débora Apr 25 '14 at 08:57
  • Thats a tradeoff decision that you'll have to make. If you have many, many getters and setters, then yes, maybe using reflection makes sense. If there but a handful of them, you could do better without reflection. Reflection will not be an efficient solution in the latter case. And yes, @Keppil is right. Try it first. – TheLostMind Apr 25 '14 at 09:03

1 Answers1

1

I'd really go with instrumentation (or other kind of code generation tooling). As what instrumentation does is to generate byte code that behaves just like handewritten code performance wise. However, you should be aware that the effort is much higher than using reflection directly. You can't see it, you can't debug it. That's why source code generation is an real alternative, but it bytes, too! You need support by the build process. Some support by the IDE would be nice, too.

Whatever your solution is, think very thorough whether it might not be an option to avoid copying the data. I mean, how valueable can be two data structures, that are that similar, that automatic copying is possible at all? Is it really really worth to put 3 or 4 months of effort here?

Bernd Ebertz
  • 1,317
  • 8
  • 10