Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2006

Tip: Looking for answers? Try searching our database.

Interpreting a string as a command

Thread view: 
jackroofman@gmail.com - 02 Nov 2006 21:01 GMT
I'm an Independent Study Java student, and just to see what all I can
do, I've been working on an AIM bot. I've made some chatting bots
before, mostly for the mIRC client. One of the things about mIRC I
liked was the ambiguity of variables; there were no types to mess
around with. This allowed you to interpret a variable as a command. For
example, you could store "/run program" as the variable %program, and
then you could use %program to run the command "/run program."

I'm looking for the Java version of the same concept. My bot reads a
text file, which has various texts and commands in it, such as
"getBuddy()" and "getMood()" and such. At the moment, I just have it
check for those and then insert as needed, as follows:

int whereMood = toReply.indexOf("getMood()");
if (whereMood != -1)
      toReply = toReply.substring(0, whereMood) + getMood() +
toReply.substring(whereMood+9);

Of course, this works with just a few commands, but a lot of commands
means I'd have to manually code them all. So, I'm looking for the
cure-all.

Is there a way to have it, assuming it already intercepted "getMood(),"
run a generic command that will work for "getMood()" "getBuddy()" and
"getWhatever()"?

Thanks.
Ingo R. Homann - 03 Nov 2006 08:38 GMT
Hi,

I'm not sure which one of the following you are looking for:

Runtime.exec()

or

java.lang.reflect.

Hth,
Ingo
jackroofman@gmail.com - 08 Nov 2006 19:59 GMT
java.lang.reflect.* seems promising, but I'm having a little trouble
figuring out exactly what to go with. As far as I can tell, I need a
Method, but it's throwing a NoSuchMethodException, despite the fact
that the Method does exist.
java.lang.NoSuchMethodException: com.sdd.BuildGUI.getMood()
with line 335 of com.sdd.BuildGUI saying:
private String getMood() {

Any idea what I'm missing?

> Hi,
>
[quoted text clipped - 8 lines]
> Hth,
> Ingo
Ian Shef - 08 Nov 2006 20:47 GMT
jackroofman@gmail.com wrote in news:1163015984.652138.216760
@k70g2000cwa.googlegroups.com:

> java.lang.reflect.* seems promising, but I'm having a little trouble
> figuring out exactly what to go with. As far as I can tell, I need a
[quoted text clipped - 3 lines]
> with line 335 of com.sdd.BuildGUI saying:
> private String getMood() {

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.

Signature

Ian Shef     805/F6      *    These are my personal opinions    
Raytheon Company         *    and not those of my employer.
PO Box 11337             *
Tucson, AZ 85734-1337    *

jackroofman@gmail.com - 09 Nov 2006 19:45 GMT
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.

> 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
[quoted text clipped - 14 lines]
> PO Box 11337             *
> Tucson, AZ 85734-1337    *
Ingo R. Homann - 10 Nov 2006 08:15 GMT
Hi,

> java.lang.reflect.* seems promising, but I'm having a little trouble
> figuring out exactly what to go with. As far as I can tell, I need a
[quoted text clipped - 5 lines]
>
> Any idea what I'm missing?

In contrast to what Ian writes, I think, getMethod() should also find a
private method. Howeven, invoking this method should throw an Exception.
But you could solve that by calling setAccessible(true) first.

However, Ian is right - since you do not provide your Code, I cannot say
much to that.

Ciao,
Ingo
jackroofman@gmail.com - 10 Nov 2006 20:02 GMT
I've got it all working, but thanks for the advice. I also just got
private access working a moment ago:

  Class tst = this.getClass();
  try {
     String fillin = "";
     Method[] theMethods = tst.getDeclaredMethods();
     for (int m=0; m<theMethods.length; m++) {
          if (theMethods[m].getName().equals(theCommand)) {
             theMethods[m].setAccessible(true);
             fillin = ((String)theMethods[m].invoke(this));
         }
     }
     toReply = toReply.replace(theCommand + "()", fillin);
  } catch (Exception e) { e.printStackTrace(); }

Just had to invoke the Method immediately after setAccessible(true) or
it wouldn't work.

> Hi,
>
[quoted text clipped - 17 lines]
> Ciao,
> Ingo
TechBookReport - 03 Nov 2006 13:20 GMT
> I'm an Independent Study Java student, and just to see what all I can
> do, I've been working on an AIM bot. I've made some chatting bots
[quoted text clipped - 23 lines]
>
> Thanks.

If you mean you want a Java expression parsing and execution engine then
take a look something like Beanshell scripting.

Signature

TechBookReport Java http://www.techbookreport.com/JavaIndex.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.