如何使用反射设置数组

时间:2013-07-15 10:24:42

标签: java reflection

我有一个类,其中有像这样的setter和getter

private String[] message;

public String[] getMessage() {
    return message;
}

public void setMessage(String[] message) {
    this.message = message;
}

现在我尝试使用像

这样的反射来调用setter
private static String[] getMessageArray(int traineeIndex) {
    ....
    String[] messageArray = new String[nodesLength];

    for (int i = 0; i < nodesLength; i++) {
        ...
        messageArray[i] = nodeValue;
    }
    return messageArray;
} //end of getMessageArray()

private static void doProcessedStuff() {
     ...
     for (int i=1; i<=count ; i++) {

        Object myClassInstance = dynamicClassLoading(packageName, className);
        ...
        String[] messageArray = getMessageArray(i);
        printXpathResult(myClassInstance, result, messageArray);    
    }
} //end of doProcessedStuff()

public static void printXpathResult(Object myClassInstance, Object result, String[] messageArray){
    ...
    String methodName = methodPrefix + nodeName;  //setMessage
    invokeMethodDynamically(myClass, myClassInstance, methodName, null, messageArray);
} //end of printXpathResult()

private static void invokeMethodDynamically(Class<?> myClass, Object myClassInstance, String methodName, 
        String methodParameter, String[] messageArray) {
    ...
    if (messageArray != null) { 
        myMethod = myClass.getMethod(methodName, new Class[] { Array.class });
        String returnValue = (String) myMethod.invoke(myClassInstance, messageArray);            

    } else { 
        myMethod = myClass.getMethod(methodName, new Class[] { String.class }); 
        String returnValue = (String) myMethod.invoke(myClassInstance, new String(methodParameter));
    }
} //end of invokeMethodDynamically().

但是当我来排队时

myMethod = myClass.getMethod(methodName, new Class[] { Array.class });

我收到以下错误

java.lang.NoSuchMethodException: 
pk.training.basitMahmood.ParsingXmlUsingXpath.ResponseTrainee.
setMessage(java.lang.reflect.Array)
at java.lang.Class.getMethod(Class.java:1607)
at pk.training.basitMahmood.ParsingXmlUsingXpath.TryXpath.
invokeMethodDynamically(TryXpath.java:498)
...

我做错了什么?

由于

3 个答案:

答案 0 :(得分:1)

在您收到错误的行中尝试此操作:

myMethod = myClass.getMethod(methodName, new Class[] { String[].class });

答案 1 :(得分:1)

您可能需要在调用invoke方法之前将Array引用强制转换为Object。 见下面的例子

public class ReflectionTest {

    public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        String []  data = {"mango", "apple"};
        Method method = Dummy.class.getMethod("setMessage", new Class[] { String[].class });
        Dummy dummy = new Dummy();
        method.invoke(dummy,(Object)data);
        for(String mesg : dummy.getMessage()){
            System.out.println(mesg);
        }

    }

    static class Dummy  {

        private String [] message;

        public String[] getMessage() {
            return message;
        }

        public void setMessage(String[] message) {
            this.message = message;
        }
    }
}

因为invoke方法将varargs作为参数实际上是一个数组。所以现在如果你传递数组作为引用,它认为你传递的是array.length参数。因此错误的论点例外。因此,您可能需要在Object ref之前将其强制转换,以便将其视为Single参数

答案 2 :(得分:0)

你应该替换:

myMethod = myClass.getMethod(methodName, new Class[] { Array.class });

通过

 myMethod = myClass.getMethod(methodName, new Class[] { String[].class });