@@ -8,10 +8,10 @@ category_order: 1
88layout : learn/tutorial.html
99subheader_select : tutorials
1010main_css_id : learn
11- description : " What is Method Handle mechanism, how is it different from Reflection API, and what tooling does it provide."
11+ description : " What is method handle mechanism, how is it different from Reflection API, and what tooling does it provide."
1212author : ["NataliiaDziubenko"]
1313toc :
14- - What are Method Handles {intro}
14+ - What are method handles {intro}
1515 - Access checking {access}
1616 - Method handle lookup {lookup}
1717 - Method type {methodtype}
@@ -27,15 +27,15 @@ last_update: 2024-05-04
2727---
2828
2929<a id =" intro " >  ; </a >
30- ## What are Method Handles
31- Method Handles are a low level mechanism used for method lookup and invocation. It is often compared to reflection,
32- because both the Reflection API and Method Handles provide means to invoke methods and constructors and access fields.
30+ ## What are method handles
31+ Method handles are a low level mechanism used for method lookup and invocation. It is often compared to reflection,
32+ because both the Reflection API and method handles provide means to invoke methods and constructors and access fields.
3333
34- What exactly is a Method Handle ? It's a direct reference to a method, constructor, or field, which can be invoked.
34+ What exactly is a method handle ? It's a direct reference to a method, constructor, or field, which can be invoked.
3535The Method Handle API allows manipulations on top of simple pointer to the method, that allow us to insert or reorder the
3636arguments, or transform the return values, for example.
3737
38- Let's take a closer look at what Method Handle mechanism can provide and how we can effectively use it.
38+ Let's take a closer look at what method handle mechanism can provide and how we can effectively use it.
3939
4040<a id =" access " >  ; </a >
4141## Access checking
@@ -88,17 +88,17 @@ use [`Lookup.findVirtual`](javadoc:MethodHandles.Lookup.findVirtual(Class,String
8888following arguments: a ` Class ` , where the method is located, a method name represented as a ` String ` , and a ` MethodType `
8989instance.
9090
91- In the example below, we are looking up an instance method [ ` String.replace ` ] ( javadoc:String.replace(char,char) ) , which
92- accepts two ` char ` arguments and returns a ` String ` :
91+ In the example below, we are using ` Lookup.findVirtual ` method to look up an instance method
92+ [ ` String.replace ` ] ( javadoc:String.replace(char,char) ) , which accepts two ` char ` arguments and returns a ` String ` :
9393
9494``` java
9595MethodHandles . Lookup lookup = MethodHandles . lookup();
9696MethodType replaceMethodType = MethodType . methodType(String . class, char . class, char . class);
9797MethodHandle replaceMethodHandle = lookup. findVirtual(String . class, " replace" , replaceMethodType);
9898```
9999
100- In the next example, we are looking up a static method [ ` String.valueOf ` ] ( javadoc:String.valueOf(Object) ) , which accepts
101- an ` Object ` and returns a ` String ` :
100+ In the next example, we are using ` Lookup.findStatic ` to look up a static method
101+ [ ` String.valueOf ` ] ( javadoc:String.valueOf(Object) ) , which accepts an ` Object ` and returns a ` String ` :
102102
103103``` java
104104MethodType valueOfMethodType = MethodType . methodType(String . class, Object . class);
@@ -257,10 +257,10 @@ several examples.
257257The [ ` MethodHandles.catchException ` ] ( javadoc:MethodHandles.catchException(MethodHandle,Class,MethodHandle) ) method can
258258wrap a given method handle inside a provided exception handler method handle.
259259
260- Say, we have a method ` problematicMethod ` that does some job , and a method ` exceptionHandler ` that handles a particular
261- exception [ ` IllegalArgumentException ` ] ( javadoc:IllegalArgumentException ) . The exception handler method must return the
262- same type as the original method. The first argument it accepts is a ` Throwable ` that we're interested in, after which
263- follow the rest of the arguments that we've originally accepted:
260+ Say, we have a method ` problematicMethod ` that performs some business logic , and a method ` exceptionHandler ` that handles
261+ a particular exception [ ` IllegalArgumentException ` ] ( javadoc:IllegalArgumentException ) . The exception handler method must
262+ return the same type as the original method. The first argument it accepts is a ` Throwable ` that we're interested in,
263+ after which follow the rest of the arguments that we've originally accepted:
264264
265265``` java
266266public static int problematicMethod(String argument) throws IllegalArgumentException {
@@ -549,9 +549,9 @@ System.out.println(getSomeUppercaseString.invoke()); // outputs: "MUMMY"
549549
550550<a id =" vsreflection " >  ; </a >
551551## Method Handles vs Reflection API
552- Method Handles were introduced in [ JDK7] ( https://docs.oracle.com/javase/7/docs/index.html ) as a tool to assist
552+ Method handles were introduced in [ JDK7] ( https://docs.oracle.com/javase/7/docs/index.html ) as a tool to assist
553553compiler and language runtime developers. They were never meant to replace reflection. The Reflection API offers something
554- that Method Handles cannot, which is listing the class members and inspecting their properties. Method Handles , on the
554+ that method handles cannot, which is listing the class members and inspecting their properties. Method handles , on the
555555other hand, can be transformed and manipulated in a way that is not possible with Reflection API.
556556
557557When it comes to method invocation, there are differences related to access checking and security considerations. The
@@ -612,7 +612,7 @@ Field field = MethodHandles.reflectAs(Field.class, getterMethodHandle); // same
612612
613613<a id =" conclusion " >  ; </a >
614614## Conclusion
615- In this tutorial, we have looked into the Method Handle mechanism and learned how to efficiently use it. We now know,
615+ In this tutorial, we have looked into the method handle mechanism and learned how to efficiently use it. We now know,
616616that method handles provide means for efficient method invocation, but this mechanism is not meant to replace the
617617Reflection API.
618618
0 commit comments