I am trying to play with MethodHandles in Java 7.
I have a class here:
public class TestCase { MethodType mt; public static void main(String args[]) { Android a = new Android(); MethodHandleExample.doSomething(a); } } class Android { public void thisIsMagic() { System.out.println("Might be called from the method handel"); } }
And the calls to the example method descriptor are here in this class:
public class MethodHandleExample { public static void doSomething(Object obj) { MethodHandle methodHandle = null ; MethodType mt = MethodType.methodType(void.class); MethodHandles.Lookup lookup = MethodHandles.lookup(); try{ try { methodHandle = lookup.findVirtual(obj.getClass(),"thisIsMagic",mt); } catch (NoSuchMethodException e) { e.printStackTrace();
but when I try to run this code, I get the following exception:
java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(Android)void to ()void at java.lang.invoke.MethodHandle.asType(MethodHandle.java:691) at java.lang.invoke.InvokeGeneric.dispatch(InvokeGeneric.java:103) at com.generic.exception.AnnotationParser.doSomething(AnnotationParser.java:35) at com.generic.exception.TestCase.main(TestCase.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
The exception that I get is on the line:
methodHandle.invoke();
Not sure how to call the base method in this case.
source share