Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > cleaning up some reflection / dynamic method invocation code

Reply
Thread Tools

cleaning up some reflection / dynamic method invocation code

 
 
Marc E
Guest
Posts: n/a
 
      07-19-2006
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().getCanonicalNam e());

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

}


 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      07-19-2006
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/
 
Reply With Quote
 
 
 
 
Marc E
Guest
Posts: n/a
 
      07-19-2006
great, thanks a lot, Tom. I'll check it out.


"Thomas Hawtin" <(E-Mail Removed)> wrote in message
news:44bd8099$0$970$(E-Mail Removed)...
> 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/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic method invocation on Proxy object? Sebastian Java 2 02-08-2012 02:16 PM
Dynamic method invocation jythonuser Python 3 06-26-2009 10:16 PM
Dynamic Method Invocation (specific line) ash Java 8 03-19-2008 01:58 PM
Method invocation via proxy and reflection Stefan Ram Java 3 10-01-2007 11:43 PM
dynamic method call using reflection only works with certain methods? Soefara Java 2 08-20-2003 06:00 PM



Advertisments