6

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.

2
  • 1
    Reflection is not that slow, especially in the context of I/O - database/file access will be several orders of magnitude slower that the performance hit due to reflection. Commented Apr 7, 2014 at 7:11
  • Yeah, you are probably right. I just asked this question because I was curious about the decision to use reflection. After so many years of JVM I believe there are many optimizations that speed up the reflection compared to first release of Java. Commented Apr 7, 2014 at 8:38

1 Answer 1

8

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).
Sign up to request clarification or add additional context in comments.

3 Comments

Ad. 1 - do you have any source on this (code generation I mean)? I never really read how reflection works under the hood. I would be glad if you could point me to some article. Ad. 2 - true, point for reflection
Have a look at Reflection sources in OpenJDK, MethodAccessorGenerator.java in particular. In short, java.lang.Method has an accessor to which invocations are delegated. First 15 calls of Method.invoke() are executed through a native call by JVM (see NativeMethodAccessorImpl.java). Then, as soon as sun.reflect.inflationThreshold is reached (15 by default), bytecode generation runs and NativeMethodAccessorImpl is replaced with dynamically generated Java accessor.
Thanks for linking the relevant sources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.