Question

Today I was wondering why frameworks like `Hibernate use reflection instead of code generation (for example using libraries like BCEL or ASM) during compilation/application startup.

Is it because of historical reasons (when Hibernate was being written there was no such library available that would allow byte code generation on the fly) and now everybody uses this approach?

I would assume that the approach with generated code would be faster then the one that uses reflection.

Was it helpful?

Solution

Right, Hibernate could likely benefit from code generation, though the profit might not be as big as you suppose.

  1. First of all, Reflection uses bytecode generation under the hood and it is not too slow.
  2. You can't do some sort of things using bytecode generation only. E.g. reflection allows you to access private fields and to invoke private methods, while it is not possible with bytecode generation (unless you use certain non-portable hacks).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top