vendredi 5 juin 2015

Why use reflection when MethodHandles are an option?

With the release of Java 7 came the MethodHandle, which allows a user to invoke a method as if using its underlying bytecode. In particular, the MethodHandles.Lookup class provides factory methods to create method handles to class members:

The factory methods on a Lookup object correspond to all major use cases for methods, constructors, and fields. Each method handle created by a factory method is the functional equivalent of a particular bytecode behavior.

Functionally, this is more or less equivalent to using reflection to access these same class members, yet method handles are faster than reflection.

So, is there any reason to still use reflection functionalities like Field#get(..)/Method.invoke(..) or are these effectively rendered obsolete by the introduction of method handles?





Aucun commentaire:

Enregistrer un commentaire