Invoking method from String
Hello,
I have this code (it does compile)
Code:
import java.lang.reflect.*;
public class Example
{
private final Object waitObj = new Object();
public static void main(String[] args) {
new Example();
}
private Example() {
waitForState(waitObj,getMethod(this,"initialized"));
System.out.println("Done");
}
private void waitForState(final Object waitObj, final Method method) {
try {
synchronized (waitObj) {
while ((new Boolean(true)).equals(method.invoke(method.getDeclaringClass(),new Object[] {})))
waitObj.wait();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private Method getMethod(final Object obj, final String method) {
try {
return obj.getClass().getMethod(method,new Class[]{});
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean initialized() {
return true;
}
}
I want it to wait until the method initialized() returns true.
This throws the following error:
Quote:
java.lang.IllegalArgumentException: object is not an instance of declaring class
How to fix this? If possible