• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reflection & Interospection

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all...
can anyone brief me about the concepts Interospection & Reflection.
rgds
Sas
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SaSaS,
Welcome to JavaRanch!
We don't have many rules here at JavaRanch, but we do have one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner!
To answer your question, Instrospection and Reflection are very closely related. Instrospection is the ability for an object to know what methods it has and Reflection is the ability to call those methods dynamically.
For example, you could have a class "Animal":

Now, you could design a GUI that would dynamically determine what methods and Animal contains and display them is a combo box. The user could select one to run and you could run it. The former would be Introspection and the letter Reflection. (You could do it through if...else statements, but that wouldn't be dynamic).
The java.lang.Class provides introspection capabilities, and the java.lang.reflect package provides the Reflaction capabilities.
Hope this helps, and don't forget to chang eyour display name!
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joel,
i have a q.
I am using reflections to invoke the associated methods of the object.
Now also one of the method returns me "another-object" which also i wish to execute the same way using(Recursively) the Reflections.Also the object is of type Array.
Thanks in advance,
-N
[ September 26, 2003: Message edited by: Navi Singh ]
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the problem that you are having? Since arrays are objects as well, you can use reflection on them, too. You might want to take a look at SUN's Reflection Tutorial for specifics on how to work with arrays.
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joel,
The SUN-link did help, still have a problem.
I get the following error.
"Method invocation failed: java.lang.IllegalArgumentException: object is not an instance of declaring class".
Thanks again,
N
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm.. If you could please post your code here, I think that I might be able to help you a little bit better. Without actually seeing it, I can't quite pinpoint the problem.
Thanks,
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to invoke the methods of an object which is a result of invoking a method previuosly.The object is itself an array type
The code follows as :

[ September 30, 2003: Message edited by: Navi Singh ]
// Added code formatting --JAM
[ September 30, 2003: Message edited by: Joel McNary ]
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I see what you're trying to do, but I also see where (at least one) error is. You are changing your responseMethods array mid-stream.

Now the next time that you loop through the outer for loop )the one with the i index), you will be using the responseMethods of whatever type is the declaring class of the Array, but you will be calling the methods on the original responseObject, which is (probably) not the same type!
I'm not sure what the point of the above code is -- are you trying to recursively obtain the data? If so, then you should probably loop thorough the resulting array and call this method for each object in the array; otherwise, just comment out this if block.
Or, if I'm totally off-base here, let me know what it is that you are terying to do and I'll see how I can help.
BTW, you can use [CODE] and [/CODE] tags in your posts to preserve the formatting of your code. It makes it easier to read. Since I'm the moderator here, I did it for you in the above message, but others might not be so nice Please do use them in the future, though.
Thanks.
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ur perfectly right.
I have to loop thru the array of objects and pass each object to the above loop.
But that i am not able to do.Can u show e how..

Thanks for responding so quickly,
N
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Again,
i tried to put hte following first as a test

Gives the folowing Exception :java.lang.IllegalArgumentException: argument is not an array.
It is an array,but don't know why...it gives the error
[ September 30, 2003: Message edited by: Navi Singh ]
 
Navi Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok More changes :-

Error is as follows :-
EXCEPTION IS AS FOLLOWS java.lang.IllegalArgumentException: wrong number of arguments
[ September 30, 2003: Message edited by: Navi Singh ]
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, this one is a little bit easier:
If the method starts with "get", you are calling it with no parameters; that is, you are assuming that the method signature is "getXXX()."
However, this may not be the case. You could have a method like "getAttributeValue(String anAttribute)", or some such. It would start with "get", but does not a a null parameter set. This will cause the "wrong number of arguments" exception.
You will need to not only check if the method starts with "get", but also the number of arguments that it takes before calling it.
reply
    Bookmark Topic Watch Topic
  • New Topic