I'm looking at serialization and API tooling that generates code instead of relying on runtime reflection. What do you gain, and what should you check before switching?
Is build-time code generation worth it to avoid reflection in Java?
Answers
Start by measuring where reflection actually costs you. Code generation moves work into the build and can give you direct calls at runtime, but it also adds generated sources and another build step to maintain. Compare startup time, throughput and build time for your own workload rather than assuming the generated option wins everywhere.
Also check what still happens at runtime. A generator may remove reflection from one part of a pipeline without eliminating dynamic loading or other runtime work from the whole application.
Reply to this answer
For serialization, behavior matters as much as speed. Check what happens when input contains fields your model doesn't know about. One project evaluating kotlinx.serialization returned to Jackson over its unknown-fields handling, so don't leave compatibility testing until after the migration.
Try representative JSON or other input, including extra fields, and configure each library explicitly before comparing the results.
Reply to this answer
API stubs are a good candidate when you already have a specification. Generators can work from OpenAPI, gRPC or GraphQL definitions and produce code without reflection-based dispatch. Avaje is another generator to look into if you want to explore the space.
The tradeoff is keeping the generated code and its inputs in step with your build. Decide when generation runs and how you'll catch stale output.
Reply to this answer
You might not need a full rewrite just to improve runtime behavior. Jackson has options such as Blackbird, while tools like Lombok, Manifold and jOOQ generate code for different jobs. Compare the specific part of the stack you want to change rather than treating all of these as interchangeable.
Reply to this answer
Add your answer
Answers are reviewed before publication.
_t54!bracketspool.7:
It's worth checking the particular generator's output and dependencies too. Generating stubs doesn't automatically mean every library they call is reflection-free.