Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to invoke a underlying void method using method handle class in java 7?
    primarykey
    data
    text
    <p>I'm trying to play with MethodHandles in Java 7.</p> <p>I have a class here:</p> <pre><code>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"); } } </code></pre> <p>And the method handle example calls are here in this class:</p> <pre><code>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(); //To change body of catch statement use File | Settings | File Templates. } catch (IllegalAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } finally { } try { methodHandle.invoke(); } catch (Throwable throwable) { throwable.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } </code></pre> <p>}</p> <p>but when I try to run this code I get an exception like this:</p> <pre><code>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) </code></pre> <p>The exception I'm getting is on the line :</p> <pre><code> methodHandle.invoke(); </code></pre> <p>Not sure how do I invoke the underlying method in this case.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload