• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Calling Methods with strings??

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a question on whether something is possible? I want to, at runtime, call a method dynamically for a comparison. For example:

Is something like this possible? I've looked at the reflection API but couldn't get it to work. I could end up getting the field I wanted, but using it for a comparison wouldn't work:

I hope this is clear. Any suggestions or pointers to information would be helpful. Thanks!!
/rick
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it should be possible with reflection. Why not post the code you've got so far? The fact that you're talking about "fields" makes me think there's some confusion, as your example makes it look like you're really interested in return values from methods, not fields from object instances.
A rough outline would be: use getMethod() in Class to get the Method with a particular name, then use invoke() in Method to execute the method for a particular object, and save the return value. Cast this returned Object to the appropriate type - if the original method returns an int, the Method's invoke() should return an Integer. Once it's cast, Integer implements Comparable, so you can use compareTo().
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thanks Jim. I think I do have it wrong. here's the method that I've created:

So, I should change getField to getMethod? What I want to do here, is have the user pass in a string which refers to the name of a class member of a object (it has a corresponding get method).
So for getMethod, would it look something like this?

The part I'm unsure of invokeing the method. Would this be correct than:

If you could give me some pointers or tips, that would be fantastic!!
The field that the getAscending() method takes will also be on a methods like getStartDate(); So I'd pass in startDate. Would this mean that I would now have to pass in getStartDate? From what I have seen in reflection examples, it would appear to me that invoke only works with methods that take in parameters, but I could totally misinterpreting it.
Thanks a lot!!
/rick
[ June 07, 2002: Message edited by: Rick Salsa ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, you've stumbled into one of my pet peeves - a catch that does nothing. Look here for more info. Consider - what happens if the caller specifies a field or method name that doesn't exist?
Other than that... now that I see what you're doing, I understand the reason for the Field/Method confusion. You can probably do this with either one, since reflection can access even private fields if done right. But it's probably better OO technique to use only the public methods, so I'd go with that solution.
The new code is definitely closer. Problems are:
"new Class[]" and "new Object[]" don't work - to make an array, you must specify a size (0 in this case).
The first parameter of the invoke method should be the instance that you're trying to invoke the method on. Meaning, the original obj1 instance (or obj2). A Method can be invoked for any instance of a class, and you need to tell it which one you're using.
Your original code had a similar problem using Field - the Field represents the concept of a particular field, across all instances of the field's containing class; it does not represent a particular value of the field. To get a partucular value of a Field, you use the get(Object) method of Field. As for the invoke(Object, Object[]) method of Method, the first parameter represents the particular instance of a class, for which you want to know the value of the given field.
Back to the Method version, the latter part of the code would look something like:

The field that the getAscending() method takes will also be on a methods like getStartDate(); So I'd pass in startDate. Would this mean that I would now have to pass in getStartDate?
Yes.
From what I have seen in reflection examples, it would appear to me that invoke only works with methods that take in parameters, but I could totally misinterpreting it.
The invoke() method allows you to use any number of parameters, including zero. The Object[] array representing the arguments can be whatever size you need it to be for the given method. In the code above, new Object[0] creates an empty array, which is your way of telling the JVM "this method has no arguments".
Enjoy...
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Ah, you've stumbled into one of my pet peeves - a catch that does nothing. Look here for more info. Consider - what happens if the caller specifies a field or method name that doesn't exist?


lol. I guess it wouldn't help by saying I was too lazy to type it in.... I have to figure out exactly how I want to handle than in my application. For now a StackTrace should do. Thanks for keeping me from getting sloppy
I now see where I was going wrong. I'm going to try out this and I'll let you know how it goes... Thanks for all your help Jim.
Would there be an easy way of doing this? Just curious...
/rick
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
Everything works 100%!! Thanks a lot for your help with this.
/rick
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic