Why use reflection to access class members when MethodHandle is faster?

Reflection and method handles serve different purposes, and exist at different levels of abstraction. You should use the one that is right for the problem you are solving.

Reflection is a general-purpose introspection mechanism, which includes many features that the method handle mechanism lacks, such as enumerating the members of a class (Class.getMethods()), inspecting the characteristics of a member such as its accessibility flags, inspecting generic signatures of members, etc.

Additionally, reflective objects can be freely shared without granting access to the sharee, because the access checks are made at each invocation. On the other hand, sharing method handles confers to the sharee the capability to invoke. So they also have different security implications.

Method handles are a low-level mechanism for finding, adapting, and invoking methods. While invocation through method handles is faster than through reflection (though to date, direct bytecode invocation is still generally faster than method handle invocation), method handles are also significantly harder to use, as they do not automatically perform the adaptations Java users would expect (such as converting a String argument to Object), resulting in linkage errors.

The reflection library is aimed at mainstream Java users; the method handle layer is aimed more at compiler and language runtime writers. Pick the tool designed for the job.

Leave a Comment