0

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.)

4

3 回答 3

3

是的,它被称为java.lang.Object. 您甚至可以将数组分配给int[][]声明为java.lang.Object.

但我担心这不是你想要的。您似乎正在编写一个客户端服务框架——客户端(您的用户)将一些数据传递给服务(您的应用程序)。现有的库可以做同样的事情,例如 Thrift、Protobuf。如果您正在寻找基于 XML 的解决方案,可以使用 SOAP。

于 2012-06-05T02:49:12.303 回答
3

您真正需要的是 JSON,以及用于处理它的 Java 工具包之一。

于 2012-06-05T02:54:34.820 回答
1

我认为您在这里寻找的是一种基于 XML 文件中描述的属性动态调用 Java 方法的方法。

如果是这种情况,您可以探索 Java 反射 API。

示例类:

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

要动态使用此类中的方法,请执行以下操作:

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");

因此,您可以在 XML 文件中指定如下:

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

然后,读取 XML 文件并使用它动态查找 Java 方法。

动态创建数组:

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;
于 2012-06-05T04:12:54.377 回答