[JVM] 2. About jdk7's MethodHandle class, jdk7methodhandle

Source: Internet
Author: User

[JVM] 2. About jdk7's MethodHandle class, jdk7methodhandle

About the MethodHandle class, this class is added after jdk1.7. The function of this class is similar to the function pointer.

 

This class has a method

 

 

 

Here, my jdk has a problem, that is, when I perform the MethodHandle operation, we will find that our method can only set the desired return value and parameters, however, we cannot operate these methods in the corresponding method. If the int type can be operated by the parameter, an error is reported, and char also reports an error.

 

But there is no error in double type.

 

1 package ch08.MethodHandle; 2 3 import java. lang. invoke. methodHandle; 4 import java. lang. invoke. methodType; 5 6 import static java. lang. invoke. methodHandles. lookup; 7 8/** 9*10 * function: test the use of MethodHandleTest class 11 * Time: 9:49:16 am 12 * file: MethodHandleTest. java13 * 14 * @ author Administrator15 * 16 */17 public class MethodHandleTest18 {19 static class ClassA20 {21 public void println (String s) 22 {23 Sys Tem. out. println (s); 24} 25 26 public void xixi (char g) 27 {28 System. out. println ("zheli shi gg" + g); 29 30 char a = 'a'; 31 a = g; 32 33 // an error is returned when the following part is added !!!! 34 // int a = 1; 35 // a = a + g; 36 // char s = (char) ('A' + g); 37 // return s; 38} 39} 40 41 public static void main (String [] args) throws Throwable42 {43 Object obj = System. currentTimeMillis () % 2 = 0? System. out: new ClassA (); 44 45 // The result of this method call is that no matter the actual type is the one, the corresponding operation 46 // getPrintlnMH (obj) can be implemented ). invokeExact ("aksdjadj"); 47 48 getPrintXixi (obj ). invokeExact (3.25); 49} 50 51 private static MethodHandle getPrintXixi (Object clazz) throws NoSuchMethodException, IllegalAccessException52 {53 MethodType mt = MethodType. methodType (void. class, char. class); 54 55 return lookup (). findVirtual (clazz. getClass (), "xixi", mt ). bindTo (clazz); 56} 57 58 private static MethodHandle getPrintlnMH (Object reveiver) throws Throwable59 {60 // This is the method type, the first parameter is the return type, the second parameter is our parameter type, followed by other types 61 MethodType mt = MethodType. methodType (void. class, String. class); 62 63 // In this lookup method, find is used to find the println class and the corresponding method type contained in the class we formulated, then our bindto is to return the MethodHandle handle 64 return lookup () of this method (). findVirtual (reveiver. getClass (), "println", mt ). bindTo (reveiver); 65} 66}

 

The error message varies depending on the number of executions !!!!

 

 

Let's take a look at this.

 

1 package ch08.MethodHandle; 2 3 import java. lang. invoke. methodHandle; 4 import java. lang. invoke. methodType; 5 6 import static java. lang. invoke. methodHandles. lookup; 7 8/** 9*10 * function: test the use of MethodHandleTest class 11 * Time: 9:49:16 am 12 * file: MethodHandleTest. java13 * 14 * @ author Administrator15 * 16 */17 public class MethodHandleTest18 {19 static class ClassA20 {21 public void println (String s) 22 {23 Sys Tem. out. println (s); 24} 25 26 public void xixi (char g) 27 {28 System. out. println ("zheli shi gg" + g); 29 30 char a = 'a'; 31 a = g; 32 33 // an error is returned when the following part is added !!!! 34 // int a = 1; 35 // a = a + g; 36 // char s = (char) ('A' + g); 37 // return s; 38} 39 40 public void xixi (double g) 41 {42 43 double a = 2.565; 44 45 a + = g; 46 47 // OK, if there is no process output in the above sentence, it will be rough in the next sentence and cannot be output !!!! 48 System. out. println ("zheli shi gg" + g); 49 System. out. println ("zheli shi gg" + a); 50 51 // an error will be reported when the following part is added !!!! 52 // int a = 1; 53 // a = a + g; 54 // char s = (char) ('A' + g); 55 // return s; 56} 57} 58 59 public static void main (String [] args) throws Throwable60 {61 Object obj = System. currentTimeMillis () % 2 = 0? System. out: new ClassA (); 62 63 // The result of this method call is that the corresponding operation 64 // getPrintlnMH (obj) can be implemented regardless of the actual type ). invokeExact ("aksdjadj"); 65 66 getPrintXixi (obj ). invokeExact (3.25); 67} 68 69 private static MethodHandle getPrintXixi (Object clazz) throws NoSuchMethodException, IllegalAccessException70 {71 // MethodType mt = MethodType. methodType (void. class, char. class); 72 MethodType mt = MethodType. methodType (void. class, double. class); 73 74 return lookup (). findVirtual (clazz. getClass (), "xixi", mt ). bindTo (clazz); 75} 76 77 private static MethodHandle getPrintlnMH (Object reveiver) throws Throwable78 {79 // This is the method type, the first parameter is the return type, the second parameter is our parameter type, followed by other types 80 MethodType mt = MethodType. methodType (void. class, String. class); 81 82 // In this lookup method, find is used to find the println class and the corresponding method type contained in the class we formulated, then our bindto is to return the MethodHandle handle of this method 83 return lookup (). findVirtual (reveiver. getClass (), "println", mt ). bindTo (reveiver); 84} 85}

When our data type is double

 

 

We get the corresponding output. If it is of the int type, an error will be reported, prompting that the corresponding method cannot be found.

In addition, if we remove the above one from the two outputs, an error will also be reported !!!!

 

The specific reason is that no definite answer has been obtained !!!!

If you know this, please kindly advise.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.