• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing dynamically created arguments to method  RSS feed

 
Michael Otto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a method called getParamSet that reads in a parameter set from
a file and returns this parameter set as an Object[]. This parameter
set is used by a method that is overloaded, let's call it doSomething.

A. private void doSomething(double a, int b, boolean c){...}
B. private void doSomething(String a, Calendar b, float c){...}
C. ....

getParamSet may create an Object[] matching the signature of A or B or ....

Here is what I'm doing now for the "A" situation.

Object [] args = new Object []{ 1.343d, 1, true};//This is the
return from getParamSet

Method doSomething = Alogrithm.class.getMethod("doSomething",
Class[]{double.class, int.class, boolean.class);
doSomething.invoke(algorithmInstance, args);

What I want to do is simply call doSomething pass it the parameter set
and let it figure it out which method signature to use i.e. take
advantage of overloading.

Any ideas?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use getMethods, iterate over them and if the name matches, check if the values are compatible:


You'll need to figure out what to do with primitive types though; isInstance will return false for the wrapped objects. Fortunately, Class has a method called isPrimitive() with which you can determine whether or not a Class represents a primtive type. Although, given the very limited amount of primitive types, you could try a couple of if-statements:


Edit: after a bit of testing the test becomes a bit harder; Byte objects are also valid for int parameters when calling invoke (and all other versions of primitive widening). So you'll have to check against that too.
 
Michael Otto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Rob, I will go with your example. After looking at the source code for Method and MethodAccessor, things look to be pretty locked down with "final"s, the procedure you provide will likely work best. It's to bad there isn't a construct that allows an Object[] to be passed as args to a method and let the overloading figure out which method signature to use.

thanks
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always write a utility class to do the hard work for you:

If you put that class in some global library or something you can reuse it when needed.
 
Michael Otto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I wasn't expecting you to code it for me.

Thank you
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it took me about 10 minutes to create that method for myself, figuring out what wrapper objects are allowed for what primitive types, so I thought I'd save you the trouble
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!