Java Error: The method getMethod(String, Class<boolean>) is undefined for type Class-Collection of common programming errors
The compile-time error suggests, that you are using Java 1.4 to compile the class. Now, in Java 1.4, it was illegal to define array parameters as Type...
, you had to define them as Type[]
, and this is the way the getMethod
is defined for the Class
:
Method getMethod(String name, Class[] parameterTypes)
Because of that, you cannot use the simplified 1.5 syntax and write:
MyClass.class.getMethod("myMethod",boolean.class));
what you need to do is:
MyClass.class.getMethod("myMethod",new Class[] {boolean.class}));
Update 1
The code you posted, does not compile because of another reason:
super(new ClickHandler() {
// This is anonymous class body
// You cannot place code directly here. Embed it in anonymous block,
// or a method.
try {
OtherClass.responseMethod(
MyClass.class.getMethod("myMethod",boolean.class));
} catch (Exception e) {
e.printStackTrace();
}
});
What you should do is create a ClickHander
constructor that accepts a Method
, like this
public ClickHandler(Method method) {
try {
OtherClass.responseMethod(method);
} catch (Exception e) {
e.printStackTrace();
}
}
and then, in MyClass
constructor invoke it like this:
public MyClass() {
super(new ClickHandler(MyClass.class.getMethod("myMethod",boolean.class)));
}
Original answer
More to this, from the JavaDoc of Class#getMethod(String, Class...)
Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.
And your method is private
, not public
.
If you want to get access to private methods, you should rather use Class#getDeclaredMethod(String, Class...)
and make it accessible by calling setAccessible(true)
.
Originally posted 2013-11-27 12:25:02.