cleaning up some reflection / dynamic method invocation code

M

Marc E

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;
}

}
 
T

Thomas Hawtin

Marc said:
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
 
M

Marc E

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


Thomas Hawtin said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top