TimFoolery
TimFoolery

Reputation: 1925

(Java) Is there a type of object that can handle anything from primitives to arrays?

I'm pretty new to Java, so I'm hoping one of you guys knows how to do this. I'm having the user specify both the type and value of arguments, in any XML-like way, to be passed to methods that are external to my application.

Example: javac myAppsName externalJavaClass methodofExternalClass [parameters]

Of course, to find the proper method, we have to have the proper parameter types as the method may be overloaded and that's the only way to tell the difference between the different versions. Parameters are currently formatted in this manner:

(type)value(/type), e.g. (int)71(/int) (string)This is my string that I'm passing as a parameter!(/string)

I parse them, getting the constructor for whatever type is indicated, then execute that constructor by running its method, newInstance(<String value>), loading the new instance into an Object. This works fine and dandy, but as we all know, some methods take arrays, or even multi-dimensional arrays.

I could handle the argument formatting like so: (array)(array)(int)0(/int)(int)1(/int)(/array)(array)(int)2(/int)(int)3(/int)(/array)(/array)... or perhaps even better... {{(int)0(/int)(int)1(/int)}{(int)2(/int)(int)3(/int)}}.

The question is, how can this be implemented? Do I have to start wrapping everything in an Object[] array so I can handle primitives, etc. as argObj[0], but load an array as I normally would? (Unfortunately, I would have to make it an Object[][] array if I wanted to support two-dimensional arrays. This implementation wouldn't be very pretty.)

Upvotes: 0

Views: 445

Answers (3)

Lai Xin Chu
Lai Xin Chu

Reputation: 2482

I think what you are looking for here is a way to dynamically call Java methods based on attributes described inside an XML file.

If that's the case, you can explore the Java Reflection API.

Example class:

package foo;
public class Bar {

    public void doSomething(String x) {
        System.out.println("This is a method doSomething that takes in a String parameter");
    }

    public void doSomething(String [] arr, String str) {
        System.out.println("This is a method doSomething that takes in a String arr parameter and a String parameter");
    }
}

To dynamically use the methods in this class, do the following:

Class c = Class.forName("foo.Bar");
Object newInstance = c.newInstance();
Method method = c.getMethod("doSomething", String.class);
// This will locate the first method
method.invoke(newInstance, "Hello World");

Method method = c.getMethod("doSomething", String[].class, String.class);
// This will locate the second method

String [] arr = {"Hello", "World"};
method.invoke(newInstance, arr, "Hello World");

So you can specify in your XML file as follows:

<method>
    <name>doSomething</name>
    <params>
        <param>java.lang.String</param>
    </params>
</method>
<method>
    <name>doSomething</name>
    <params>
        <param>java.lang.String[]</param>   // or any other annotation to indicate that its an arr
        <param>java.lang.String</param>
    </params>
</method>

Then, read the XML file and use it to find the Java methods dynamically.

To dynamically create an array:

Class c = Class.forName("java.lang.String");
int length = 5;
Object o = Array.newInstance(c, length); // o is a String arr of length 5
String [] arr = (String []) o;

Upvotes: 1

xiaofeng.li
xiaofeng.li

Reputation: 8587

Yes there is, and it's called java.lang.Object. You can even assign arrays like int[][] to an variable declared as java.lang.Object.

But I fear that's not what you wanted. It seems that you are writing a client-service framework -- the client (your user) pass some data to the service (your app). There're existing libraries that can do the same thing, e.g., Thrift, Protobuf. If you are looking for XML-based solution, there is SOAP.

Upvotes: 3

Hot Licks
Hot Licks

Reputation: 47699

What you're really looking for is JSON, and one of the Java kits for handling it.

Upvotes: 3

Related Questions