lang

MethodHandle example

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: November 11th, 2012
0 84 1 minute read
public class MethodAccessExampleWithArgs { private final int i;  public MethodAccessExampleWithArgs(int i_) {  i = i_; }  private void bar(int j, String msg) {  System.out.println("Private Method 'bar' successfully accessed : "    + i + ", " + j + " : " + msg + "!"); }  // Using Reflection public static Method makeMethod() {  Method meth = null;   try {   Class[] argTypes = new Class[] { int.class, String.class };    meth = MethodAccessExampleWithArgs.class.getDeclaredMethod("bar",     argTypes);    meth.setAccessible(true);  } catch (IllegalArgumentException e) {   e.printStackTrace();  } catch (NoSuchMethodException e) {   e.printStackTrace();  } catch (SecurityException e) {   e.printStackTrace();  }   return meth; }  // Using method handles public static MethodHandle makeMh() {  MethodHandle mh;   MethodType desc = MethodType.methodType(void.class, int.class,    String.class);   try {   mh = MethodHandles.lookup().findVirtual(     MethodAccessExampleWithArgs.class, "bar", desc);   System.out.println("mh=" + mh);  } catch (NoAccessException e) {   throw (AssertionError) new AssertionError().initCause(e);  }  return mh; }}

Following, is a class meant to test the two approaches of accessing the private method “bar” :

public class MethodAccessMain {  private static void withReflectionArgs() {  Method meth = MethodAccessExampleWithArgs.makeMethod();   MethodAccessExampleWithArgs mh0 = new MethodAccessExampleWithArgs(0);  MethodAccessExampleWithArgs mh1 = new MethodAccessExampleWithArgs(1);   try {   System.out.println("Invocation using Reflection");   meth.invoke(mh0, 5, "Jabba the Hutt");   meth.invoke(mh1, 7, "Boba Fett");  } catch (IllegalAccessException e) {   e.printStackTrace();  } catch (IllegalArgumentException e) {   e.printStackTrace();  } catch (InvocationTargetException e) {   e.printStackTrace();  } }  private static void withMhArgs() {  MethodHandle mh = MethodAccessExampleWithArgs.makeMh();   MethodAccessExampleWithArgs mh0 = new MethodAccessExampleWithArgs(0);  MethodAccessExampleWithArgs mh1 = new MethodAccessExampleWithArgs(1);   try {   System.out.println("Invocation using Method Handle");   mh.invokeExact(mh0, 42, "R2D2");   mh.invokeExact(mh1, 43, "C3PO");  } catch (Throwable e) {   e.printStackTrace();  } }  public static void main(String[] args) {  withReflectionArgs();  withMhArgs(); }}

Related Article:

References :

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: November 11th, 2012
0 84 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.