Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java reflection vs code generation

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.

like image 582
Andna Avatar asked Apr 07 '14 06:04

Andna


1 Answers

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).
like image 62
apangin Avatar answered Sep 23 '22 11:09

apangin