Apart, on the code
/** * * * @author liuyefeng<[email protected]> * @date April 8, 2015 PM 10:41:13 * @CopyRight TopView INC * @versio N V1.0*/ Public classMethodhandletest { PublicMethodhandle gethandler () {Methodhandle MH=NULL; Methodtype MT= Methodtype.methodtype (String.class,int.class,int.class); Methodhandles.lookup LK=Methodhandles.lookup (); Try{MH= Lk.findvirtual (String.class,"substring", MT); } Catch(Throwable e) {e.printstacktrace (); } returnMH; } Public Static voidMain (string[] args) throws Throwable {Methodhandle MH=Newmethodhandletest (). GetHandler (); String Str="Hello World"; Object RESULT1= Mh.invoke (str,1,3); Object result2= (String) mh.invokeexact (str,1,3);//Object result2 = mh.invokeexact (str, new Integer (1), 3); /** * The above method executes the Times error, because the method type is String.class, Int.class, Int.class * and the returned type is object, and the declaration is a string that does not conform to * Two parameter type is integer, and declaration of int does not match, then type adaptation does not conform, system error. */System. out. println ("result 1:"+RESULT1); System. out. println ("result 1:"+result2); }}The result of the code output is El. Now let's talk about the invocation procedure of the method handle, first, before getting the method handle, through the static factory method of Methodtype, which is a method type that contains the method parameter type, the method return type, that is
MethodType mt = MethodType.methodType(String.
class
,
int
.
class
,
int
.
class
)。
Second, get the method handle to use the Lookup object, such as the code in theMethodhandles.lookup LK, this object can provide a handle to the method of any visible method in its environment. We can liken it to a container of method members, methods that contain a class object, bylk.findVirtual(String.class, "substring", mt);具体锁定String类型中的某个方法,作为方法句柄返回。要从lookup对象中得到方法句柄,需要给出持有所需方法的类,方法的名称,以及跟方法相匹配的方法类型。
Finally, after getting to the method handle, we can invoke the underlying method through the method handle, similar to the method call in reflection. The method handle provides two methods for invoking the underlying method, invoke and Invokeexact methods. The Invokeexact method is the same as calling the underlying method directly, such as in Code, where Mh.invoke (str, 1, 2) is the equivalent of calling str.substring (1, 3) directly, and the difference between two methods is described below. Here we first parse the invocation of these two methods, the method's invocation parameters are basically the same, the first parameter is the method's accepted object, that is, which object executes this method, and the next parameter is the parameters required to execute the method. Of course, the first parameter, which is the accepted object of the method, can be bound by the BindTo dynamic parameter binding method of the method handle, so that the invocation of the method handle is no different from the normal method call, and the dynamic parameter binding will be researched later.
&NBSP;
&NBSP;
参考资料:《java程序员修炼之道》、《深入理解java7核心技术与最佳实践》
Methodhandle (method handle) series two: Simple use of method handles