Re: Interpreting a string as a command
Going with public seems to have done the trick. Ideally, I'd rather
leave it private, but this will work for now. A generic example of the
code as I'll be using it, for reference of anyone who needs it:
Class tst = this.getClass();
try {
Method fillin = tst.getMethod(theCommand);
String returnedMethod = ((String)fillin.invoke(this));
} catch (Exception e) { e.printStackTrace(); }
where the string "theCommand" is anything that a method is named for,
such as, in the example in my earlier post, "getMood" without the
parenthesis.
Of course, that (String) would have to be whatever type it's returning;
in my actual code, I'm getting Strings and using String.replace(), so
the typecast might not even been necessary.
Thanks for all the help.
Ian Shef wrote:
You didn't provide your code, or I am coming into this discussion too late,
but it could be because this method is private. As such, it (mostly) doesn't
exist as far as any other class is concerned.
Why are you attempting to call a private method? Are you doing this from
inside its own class, or from another class?
Reflection may have a cheat(*) in this area, but you should not use it unless
you have a really really really good reason.
(*) Reflection allows access to private fields (with some effort). I don't
know if it also works for private methods.
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *