So I know this topic has been done before, e.g. Java Reflection Performance, but my particular question is, it seems that many popular libraries are implemented via annotations and reflection (Gson, Jackson, Jaxb implementations, hibernate search, for example). Many (if not all) libraries provide good (or great) performance even though they use reflection. My question is, how do they do it? Is there some "tricks" to know or do they simply use straight reflection, and the performances worries are overblown?
So for example, when we write:
MyObject obj = new Gson().fromJson(someInputStream, MyObject.class);
I can understand how the library might internally cache the Field objects, but it seems to me that it needs to reflectively instantiate the object each time, and it needs to call the setter on each field (reflectively) based on the parsed value from the json.
Or is there some way to pay (all) the cost of reflection only at startup?
I've definitely noticed that Gson/Jackson etc. have relatively large startup cost and are super fast after that. So obviously I'm wondering, if I write a library that does something vaguely similar, are there tricks I need to know about? Because it appears you can't get away from some amount of reflection, on each call.