Home arrow static arrow Java Programming [Archive] - Request for comments
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Request for comments
This topic has 8 replies on 1 page.

Posts:349
Registered: 1/8/04
Request for comments  
Aug 3, 2004 2:13 PM



 
I just wrote some code to call methods from Strings so that commands can be issued to my app as Strings (from a file or network or command line, etc.). I thought I'd post it in the hopes that others might provide suggestions for improvements or maybe even benefit from it.

It's an abstract (hopefully very reusable) class that must be extended by a concrete class that has the methods you'd want to call. The methods should return true if they succeed, and false otherwise. They should take a String[] as their only argument.

import java.lang.reflect.Method;/* * Extend this class. * Set important values with the constructor: *  separator is the String separating fields - a Tab ("/t") for instance * * Implement a method for every command you want to call in the file: * 		boolean commandName(String[] args) {} * Return true for success running the command, false for failure */ public abstract class StringCommander {     private String separator = nullpublic StringCommander() {}     public StringCommander(String separator) {        setSeparator(separator);    }	    public void setSeparator(String newSeparator) {        this.separator = newSeparator;    }     public boolean dispatchCommand(final String line) {        if (line == null || line.trim().length() == 0) {            return true; //empty line        }         String[] command = line.split(separator, -1);        String commandName = command[0].toLowerCase();         Class cls = this.getClass();        Class[] parameters = new Class[] { String[].class };        Object args[] = new Object[] { command };         Method method = null;        Boolean worked = new Boolean(true);        try {            method = cls.getMethod(commandName, parameters);            worked = (Boolean)method.invoke(this, args);        } catch (Exception e) {            return false; // failure running command        }        return worked.booleanValue(); // success from command    }}


Example concrete class:
public class MyStringCommander extends StringCommander {		public SubscriptionStringCommander() {		super("\t");	} 	public boolean comment(String args[]) {		doStuff(args[0]);		doStuff(args[1]);		return true;	}}
 

Posts:3,258
Registered: 00-08-28
Re: Request for comments  
Aug 3, 2004 2:15 PM (reply 1 of 8)



 
well this is more of a code review rather than an rfc. I got misled by the topic of the thread.
 

Posts:2,206
Registered: 8/15/02
Re: Request for comments  
Aug 3, 2004 2:24 PM (reply 2 of 8)



 
A quick glance at your code, it seems ok, but should the comment method return a Boolean rather than a boolean?
 

Posts:14,142
Registered: 99-04-02
Re: Request for comments  
Aug 3, 2004 2:29 PM (reply 3 of 8)



 
ok,.... it doesn't compile. method doStuff is not defined
 

Posts:349
Registered: 1/8/04
Re: Request for comments  
Aug 3, 2004 2:31 PM (reply 4 of 8)



 
A quick glance at your code, it seems ok, but should
the comment method return a Boolean rather than a
boolean?

You would think, huh?
It seems to work both ways. Which must be some part of reflection I don't understand - unless you can cast a boolean to a Boolean. Which you can't.

I have changed the return types to Boolean in my app code, cause it does seem more correct.
 

Posts:349
Registered: 1/8/04
Re: Request for comments  
Aug 3, 2004 2:34 PM (reply 5 of 8)



 
ok,.... it doesn't compile. method doStuff is not defined

sorry, Robocop911 was writing that for me and was a little late:
public class MyStringCommander extends StringCommander {        public SubscriptionStringCommander() {        super("\t");    }    public boolean comment(String args[]) {        doStuff(args[0]);        doStuff(args[1]);        return true;     }    private void doStuff(String s) {        System.out.println(s);    }}
 

Posts:88
Registered: 6/10/01
Re: Request for comments  
Aug 4, 2004 1:55 AM (reply 6 of 8)



 
A quick glance at your code, it seems ok, but
should
the comment method return a Boolean rather than a
boolean?

You would think, huh?
It seems to work both ways. Which must be some part of
reflection I don't understand - unless you can cast a
boolean to a Boolean. Which you can't.

I have changed the return types to Boolean in my app
code, cause it does seem more correct.

You are using JDK 1.5 (or JDK 5.0), hm?
 

Posts:2,909
Registered: 13.8.2003
Re: Request for comments  
Aug 4, 2004 2:16 AM (reply 7 of 8)



 
If my memory serves me right, reflection does a primitive form of autoboxing for you resulting in the exchangeability of primitives and their Object counterparts.
 

Posts:14,142
Registered: 99-04-02
Re: Request for comments  
Aug 4, 2004 7:26 AM (reply 8 of 8)



 
If my memory serves me right, reflection does a
primitive form of autoboxing for you resulting in the
exchangeability of primitives and their Object
counterparts.

Yes, it has to. You handle primatives with their wrapper classes for the values (as parameters or returned objects). But you use the primatives types for class types (Integer.TYPE, Boolean.TYPE, etc... or int.class, I think works too) for getting method references.
 
This topic has 8 replies on 1 page.