Ok I do understand how Java Reflection works. But what I am doing is kinda different from what is shown on the Reflection tutorials. Now what I want below is to invoke a method that is returned by a invoking a method by using reflection.
class Foo{
private String str = "";
public Foo(String str){
str = this.str;
}
public void goo(){
System.out.println(this.str);
}
}
class Bar{
public Foo met(String str){
return new Foo(str);
}
}
class Hee{
public static void main(String [] args) throws Exception{
Class cls = Class.forName("Bar");
Object obj = cls.newInstance();
Class [] types = {String.class};
String [] arr = {"hello"};
Method method = cls.getMethod("met",types);
Object target = method.invoke(obj, arr);
target.goo(); //here where the error occurs
// 123456
}
}
Now, I depend to much on my experience that my method.invoke() would return the object that is being returned by the method that is being returned by the method that is reflected. But seems it doesn’t work.. I debugged my code ans seems it doesn’t return anything. What I doing wrong? Please tell me if I did something wrong
Probably need to cast
targetobject tofoo type.