• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Reflection  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java method which is supposed to compute some thing and return it back as a string using all of the information available from its arguments. The number of arguments may vary. Basically, the method should act like a variable-arg method like the way it can be done in C/C++. As we do not have variable-arg. facility in Java, one possible solution could be using an array of strings or something that would simulate variable-arguments. This would be work out fine in a normal scenario. But, I am calling this method using Reflection. You can think that I have a generic method calling mechanism as part of an XML file. The XML file contains the method name, arguments etc. and using reflection, I am loading the appropriate method and calling it. The problem is, I cannot build an array of strings to be passed as variable arguments to the method.
Let me put it through an example:
Method in a called called company.MyClass
public static String myMethod(variable args ...) {
// compute from the arguments and return a string
}
XML file:<method>company.MyClass::myMethod::hello::String::10::Integer</method>
Here, my application would call myMethod with "hello" and 10 as arguments. I want these arguments to be variable. ie., I want both these XML snippets to work:
<method>company.MyClass::myMethod::hello::String::first::String</method>
<method>company.MyClass::myMethod::hello::String::firstr:String::secong::String</method>
Remember that I am calling the method using reflection and so, I cannot build a string array in runtime because there are other methods which do not take String[] as arguments.
Hope I got the problem clear.
Any suggestions how to do this??
Thanks
Zooey
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Zooey, and welcome to JavaRanch.
I can think of a couple of ways of doing this; a slight modification of your XML data might be in order.

Note that this would change your XML code to something like the following:
<method>company.MyClass::myMethod::hello::String::10::Integer</method>
(calls myMethod("hello", 10))
<method>company.MyClass::myMethod::hello::String::first::String</method>
(calls myMethod("hello", "first"))
<method>company.MyClass::myMethod::hello,first::String[]</method>
(calls myMethod(new String[]{"hello", "first"}))
<method>company.MyClass::myMethod::hello, first, second::String[]</method>
(calls myMethod(new String[]{"hello", "first", "second"}))
=============
Also, as an aside, you may want to consider breaking your XML method element down into several sub-elements; it may look convenient to insert your own formatting mechanism, but in the long run, it would probably be more hassle than it is worth. Try:
<method>
<className>company.MyClass</className>
<methodName>myMethod</methodName>
<parameter>
<type>java.lang.String[]</type>
<value>hello</value>
<value>first</value>
</parameter>
</method>
and the like. Of course, this is just my suggestion
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!