Reflection API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • john444
    New Member
    • Jun 2007
    • 5

    #1

    Reflection API

    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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • john444
      New Member
      • Jun 2007
      • 5

      #3
      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.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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?

        Comment

        • john444
          New Member
          • Jun 2007
          • 5

          #5
          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.refle ct.Field;
          import java.lang.refle ct.InvocationTa rgetException;
          import java.lang.refle ct.Method;
          public class Client{
          Server n=new Server();
          Class c = n.getClass();
          Method[] theMethods = c.getDeclaredMe thods();
          String methodString = theMethods[i].getName();
          String returnString = theMethods[i].getReturnType( ).getName();
          if(returnString .equals("java.l ang.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.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Maybe I misunderstand you completely but the Method.getRetur nType()
            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

            Comment

            • john444
              New Member
              • Jun 2007
              • 5

              #7
              yeah u rite..
              i wil try to implement it
              Thankx

              Comment

              Working...