@@ -8,7 +8,7 @@ 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 are method handles , how are they different from the Reflection API, and what tooling do they provide? "
1212author : ["NataliiaDziubenko"]
1313toc :
1414 - What are method handles {intro}
@@ -28,14 +28,14 @@ last_update: 2024-05-04
2828
2929<a id =" intro " >  ; </a >
3030## 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, constructors and access fields.
31+ Method handles are a low- level mechanism used for method lookup and invocation. They are often compared to reflection,
32+ because both the Reflection API and method handles provide a means to invoke methods, constructors, and access fields.
3333
3434What exactly is a method handle? It's a directly invocable reference to an underlying method, constructor, or field.
35- The Method Handle API allows manipulations on top of simple pointer to the method, that allow us to insert or reorder the
36- arguments, or transform the return values, for example .
35+ The Method Handle API allows manipulations on top of a simple pointer to the method that allows us to insert or reorder the
36+ arguments, transform the return values, etc .
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 handles can provide and how we can effectively use them .
3939
4040<a id =" access " >  ; </a >
4141## Access checking
@@ -55,8 +55,8 @@ To create a method handle we first need to create a [`Lookup`](javadoc:Lookup) o
5555creating method handles. Depending on how the lookup object itself or the method handles are going to be used, we can
5656decide whether we should limit its access level.
5757
58- For example, if we create a method handle pointing to a private method and that method handle is accessible from outside,
59- so is the private method. Normally we would like to avoid that. One way is to make the lookup object and method handle
58+ For example, if we create a method handle pointing to a private method and that method handle is accessible from the outside,
59+ then the private method is as well . Normally we would like to avoid that. One way is to make the lookup object and method handle
6060` private ` too. Another option is to create the lookup object using the [ ` MethodHandles.publicLookup ` ] ( javadoc:MethodHandles.publicLookup() )
6161method, so it will only be able to search for public members in public classes within packages that are exported unconditionally:
6262
@@ -138,7 +138,7 @@ String result = (String) replaceMethodHandle.invoke((Object)"dummy", (Object)'d'
138138```
139139
140140One other alternative to invoke a method handle is to use [ ` MethodHandle.invokeWithArguments ` ] ( javadoc:MethodHandle.invokeWithArguments(Object...) ) .
141- The result of this method invocation is equivalent to ` invoke ` , with the only difference that all the arguments can be
141+ The result of this method invocation is equivalent to ` invoke ` , with the only difference being that all the arguments can be
142142provided as an array or list of objects.
143143
144144One interesting feature of this method is that if the number of provided arguments exceeds the expected number, all the
@@ -147,8 +147,8 @@ leftover arguments will be squashed into the last argument, which will be treate
147147<a id =" fields " >  ; </a >
148148## Accessing fields
149149It is possible to create method handles that have read or write access to fields. For instance fields, this is
150- facilitated by [ ` findGetter ` ] ( javadoc:MethodHandles.Lookup.findGetter(Class,String,Class) ) and
151- [ ` findSetter ` ] ( javadoc:MethodHandles.Lookup.findSetter(Class,String,Class) ) methods, and for static fields, by
150+ facilitated by the [ ` findGetter ` ] ( javadoc:MethodHandles.Lookup.findGetter(Class,String,Class) ) and
151+ [ ` findSetter ` ] ( javadoc:MethodHandles.Lookup.findSetter(Class,String,Class) ) methods, and for static fields, by the
152152[ ` findStaticGetter ` ] ( javadoc:MethodHandles.Lookup.findStaticGetter(Class,String,Class) ) and
153153[ ` findStaticSetter ` ] ( javadoc:MethodHandles.Lookup.findStaticSetter(Class,String,Class) ) methods. We don't need to provide
154154a ` MethodType ` instance; instead, we should provide a single type, which is the type of the field.
@@ -534,7 +534,7 @@ private static String resultTransform(String value) {
534534}
535535```
536536
537- Here is the method handle for our transformator method:
537+ Here is the method handle for our transformer method:
538538
539539``` java
540540MethodHandle resultTransform = lookup. findStatic(Example . class, " resultTransform" , MethodType . methodType(String . class, String . class));
@@ -614,7 +614,7 @@ Field field = MethodHandles.reflectAs(Field.class, getterMethodHandle); // same
614614<a id =" conclusion " >  ; </a >
615615## Conclusion
616616In this tutorial, we have looked into the method handle mechanism and learned how to efficiently use it. We now know,
617- that method handles provide means for efficient method invocation, but this mechanism is not meant to replace the
617+ that method handles provide a means for efficient method invocation, but this mechanism is not meant to replace the
618618Reflection API.
619619
620620Method handles offer a performance advantage for method invocation due to a different access checking approach. However,
0 commit comments