Connecting Tech Pros Worldwide Forums | Help | Site Map

Reflection API

Newbie
 
Join Date: Jun 2007
Posts: 5
#1: Jun 21 '07
I am using Reflection API to get the details of a Class. In that Class there is a method which returns an object .The problem is that I am not able to find out any way to get the details of that returning object(Name of that Class, fields in that object etc..)
Please if anyone can help me in this regard.

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 21 '07

re: Reflection API


Quote:

Originally Posted by john444

I am using Reflection API to get the details of a Class. In that Class there is a method which returns an object .The problem is that I am not able to find out any way to get the details of that returning object(Name of that Class, fields in that object etc..)
Please if anyone can help me in this regard.

Get the class of the object first, even the getClass method will do that, then you can start to look for it's methods in the same way that you've found this method which returns the object.
Newbie
 
Join Date: Jun 2007
Posts: 5
#3: Jun 21 '07

re: Reflection API


I guess to get the class name I need to know its object , then only I can apply object .getClass(). And I dont kno how to find its object.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jun 21 '07

re: Reflection API


Quote:

Originally Posted by john444

I guess to get the class name I need to know its object , then only I can apply object .getClass(). And I dont kno how to find its object.

I assume you've read the reflection tutorial?
Newbie
 
Join Date: Jun 2007
Posts: 5
#5: Jun 21 '07

re: Reflection API


Well may be i hvent gone through thm very well.....bt let me send u an excerpt of my code

public class Server{
public static Object object(String s){
JButton jButton=new JButton("HELLO);
return jButton;
}
}

Now I am tryng to use Reflection API in the Client class


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Client{
Server n=new Server();
Class c = n.getClass();
Method[] theMethods = c.getDeclaredMethods();
String methodString = theMethods[i].getName();
String returnString = theMethods[i].getReturnType().getName();
if(returnString.equals("java.lang.Object")){
******************
}
}


If there is any method with Object Return Type I want to get its name .value etc..
Bytheway I am tryng to use
Object obj=method[i].invoke()
But I am not clear with the arguments the "invoke " method use.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Jun 21 '07

re: Reflection API


Maybe I misunderstand you completely but the Method.getReturnType()
method already returns the Class that represents the type of the returned
value.

Of course that method may return an Object that actually is a extending class
thereof. You can only reflect on it when you actually have such an object,

kind regards,

Jos
Newbie
 
Join Date: Jun 2007
Posts: 5
#7: Jun 21 '07

re: Reflection API


yeah u rite..
i wil try to implement it
Thankx
Reply