sri*_*ram 4 java methods exception
我正在尝试使用Java 7中的MethodHandles。
我在这里上课:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
此类中的方法句柄示例调用在此处:
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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行此代码时,出现如下异常:
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)
Run Code Online (Sandbox Code Playgroud)
我得到的异常是在线上:
methodHandle.invoke();
Run Code Online (Sandbox Code Playgroud)
不知道在这种情况下如何调用基础方法。
| 归档时间: |
|
| 查看次数: |
2096 次 |
| 最近记录: |