cleaning up some reflection / dynamic method invocation code

Discussion in 'Java' started by Marc E, Jul 19, 2006.

  1. Marc E

    Marc E Guest

    All,
    Greetings. Looking for some java smarties who can help me see what I'm
    missing.

    Basically, i've got an object with a bunch of setXXXX() methods, each of
    which takes a single argument that could be an int, boolean, long, or
    String. I'm calling those setters dynamically by looping through a list of
    properties to set. I've got it working with a bit of lame hackery, but i
    can't help thinking that it's gotta be easier and cleaner than this.

    My gut tells me that the convertArg() method and subsequent use of it are
    just really lame and probably aren't necessary, but i just don't know the
    API well enough to use what java already has built in. So, if i'm right
    about that...if anyone can help me clean up this ugliness...i'd be much
    obliged.

    Feel free to rip the code a new A-Hole. Thanks.


    /**** CODE STARTS HERE ****/


    public class POLARunner implements OptimizerRunner {
    Map types = createClassMap();
    ApplicationContext context = AppManager.getContext();
    ProfilePropertiesDAO propdao;
    POLAOptimizerInterface optimizer;
    boolean throwOnUnknownMethod = true;

    public void optimize(String inputFile, String outputFile, int fileID)
    throws Exception {
    String methodName = "";
    Class argTypeClass;
    Class tmp;
    Object objArg;
    String arg = "";
    Method dynamethod;

    //get the profile settings
    List<ProfileProperty> props = propdao.getProfileProperties(fileID);

    //for each setting, call the setter
    tmp = Class.forName(optimizer.getClass().getCanonicalName());

    for (ProfileProperty prop : props) {
    if(!prop.getPropertyValue().equalsIgnoreCase("off")){
    methodName = "set" + prop.getPropertyName();

    argTypeClass =
    (Class)types.get(prop.getArgDataType());//getArgDataType() will return
    strings like "String", "int", "boolean"

    try {
    dynamethod = tmp.getMethod(methodName,new
    Class[]{argTypeClass});
    arg = prop.getPropertyValue();

    // here's where it gets super lame....there's gotta be a
    way to not have to do this
    objArg = convertArg(arg,prop.getArgDataType());

    System.out.println("calling setter " + methodName
    + " with arg type " +
    argTypeClass.getCanonicalName()
    + " with value " + arg);
    dynamethod.invoke(optimizer, new Object[]{objArg});
    } catch (Exception e) {
    System.out.println("Method named " + methodName + " did
    not exist");
    if(throwOnUnknownMethod){
    throw new Exception(e);
    }
    }
    }
    }
    //.....the actual real work after the set calls is omitted....
    }


    private Map createClassMap(){
    Map<String,Class> classtypes = new HashMap<String,Class>();
    classtypes.put("string",String.class);
    classtypes.put("int",int.class);
    classtypes.put("long",long.class);
    classtypes.put("double", double.class);
    classtypes.put("boolean",boolean.class);
    return classtypes;
    }

    /**
    * My super lame hacker jobby for getting around the
    IllegalArgumentException
    * @param arg the argument to convert
    * @param type the type of the argument to convert to
    * @return
    */
    private Object convertArg(String arg, String type){
    Object returnval = arg;

    if(type.equals("boolean")){
    returnval = Boolean.valueOf(arg);
    }else if(type.equals("int")){
    returnval = Integer.valueOf(arg);
    }else if(type.equals("long")){
    returnval = Long.valueOf(arg);
    }else if(type.equals("double")){
    returnval = Double.valueOf(arg);
    }
    return returnval;
    }

    }
     
    Marc E, Jul 19, 2006
    #1
    1. Advertising

  2. Marc E wrote:
    >
    > Basically, i've got an object with a bunch of setXXXX() methods, each of
    > which takes a single argument that could be an int, boolean, long, or
    > String. I'm calling those setters dynamically by looping through a list of
    > properties to set. I've got it working with a bit of lame hackery, but i
    > can't help thinking that it's gotta be easier and cleaner than this.


    I'd probably use java.beans for this. I'd also use the class set method
    (bean property) to work out what type the argument should be. You may
    find java.beans.PropertyEditor.setAsText useful.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jul 19, 2006
    #2
    1. Advertising

  3. Marc E

    Marc E Guest

    great, thanks a lot, Tom. I'll check it out.


    "Thomas Hawtin" <> wrote in message
    news:44bd8099$0$970$...
    > Marc E wrote:
    >>
    >> Basically, i've got an object with a bunch of setXXXX() methods, each of
    >> which takes a single argument that could be an int, boolean, long, or
    >> String. I'm calling those setters dynamically by looping through a list
    >> of properties to set. I've got it working with a bit of lame hackery,
    >> but i can't help thinking that it's gotta be easier and cleaner than
    >> this.

    >
    > I'd probably use java.beans for this. I'd also use the class set method
    > (bean property) to work out what type the argument should be. You may find
    > java.beans.PropertyEditor.setAsText useful.
    >
    > Tom Hawtin
    > --
    > Unemployed English Java programmer
    > http://jroller.com/page/tackline/
     
    Marc E, Jul 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Ram
    Replies:
    3
    Views:
    800
    Daniel Pitts
    Oct 2, 2007
  2. ash
    Replies:
    8
    Views:
    434
    Roedy Green
    Mar 19, 2008
  3. odeits

    Help cleaning up some code

    odeits, Mar 7, 2009, in forum: Python
    Replies:
    16
    Views:
    468
  4. jythonuser

    Dynamic method invocation

    jythonuser, Jun 23, 2009, in forum: Python
    Replies:
    3
    Views:
    706
    jythonuser
    Jun 26, 2009
  5. Sebastian
    Replies:
    2
    Views:
    477
    Sebastian
    Feb 8, 2012
Loading...

Share This Page