Let there be a class in my task:
class MyClass {
String name() {
return toString();
}
}
I want to create an instance MethodTypethat describes a return method, which is "any" object. I am trying to do the following:
MethodType genericTypeWithObjectReturn =
MethodType.methodType(Object.class, new Class[] {});
then try to find the method using
MethodHandle mh =
lookup.findVirtual(MyClass.class, "name", genericTypeWithObjectReturn);
Using the above line, I get an exception:
Caused by: java.lang.NoSuchMethodError: MyClass.name()Ljava/lang/Object;
My observation is that the type of the return method must be exactly the same type; namely, I should have used String.classin the above description to determine MethodType. It's right? Is there a way that I can do this the way I described, and prefer not to use the reflection API?
nobeh