Put the code up first, using the same 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 difference between the invoke and the Invokeexact method, in terms of name, is clearly the latter being more accurate, or more demanding. The Invokeexact method requires strict type matching at invocation, and the return value type of the method is also considered, as commented out in the above code. If you putObject result2 = (String) mh.invokeExact(str,
1
,
3
);
中的(String)去掉类型转换的话,在调用的时候该方法会认为返回值是Object类型而不是String类型,然后系统报错。
The basic rule for type matching is to compare the return value type and the type of each parameter to match each other. Assuming that the source type is s and the target type is T, the basic rules are as follows: 1, can be done by the Java type conversion, generally from the subclass to the parent class, such as from a string to the object type, 2, can be completed by the basic type of conversion, you can only expand the scope of the type, such as an int tangent Change to long, 3, can be done by the basic type of automatic boxing and unpacking mechanism, such as from int to Integer, 4, if S has a return value type, and T's return value type is void, then the return value of S is discarded. 5, if the return value of S is void, the return value of T is a reference type, the return value of T is null, 6, if the return value of S is void, and the return value of T is the base type, the return value of T is 0; 1th, 2, 3 well understood, 4th, 5, 6 The sense principle is the same, I use a new example Description
Public classMethodhandletest { PublicMethodhandle gethandler () {Methodhandle MH=NULL; Methodtype MT= Methodtype.methodtype (void.class); Methodhandles.lookup LK=Methodhandles.lookup (); Try{MH= Lk.findvirtual (methodhandletest.class, "Print", MT); } Catch(Throwable e) {e.printstacktrace (); } returnMH; } Public voidprint () {System.out.println ("Print"); } Public Static voidMain (string[] args)throwsthrowable {methodhandletest mht=Newmethodhandletest (); Methodhandle MH=Mht.gethandler (); intRESULT1 = (int) Mh.invoke (MHT); Object result2=Mh.invoke (MHT); System.out.println ("Result 1:" +RESULT1); System.out.println ("Result 2:" +result2); }}
The output of the program is: Printprintresult 1:0result 2:null Reference: Java Programmer's Way of cultivation, deep understanding of Java7 core technology and best practices
Methodhandle (method handle) series three: the difference between invoke and Invokeexact