Basit
Basit

Reputation: 8606

How to set Array using reflection

I have a class in which there is setter and getter like this

private String[] message;

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

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

Now i am trying to call setter using reflection like

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

But when i come to line

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

i get the following error

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

What i am doing wrong ?

Thanks

Upvotes: 3

Views: 255

Answers (3)

veritas
veritas

Reputation: 2444

You Might Need to cast The Array reference to Object before calling the invoke method. see the below example

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

Its because invoke method takes varargs as parameter which is essentially an array. So now if you pass array as an reference it thinks that you are passing array.length number of arguments. And hence Wrong Argument exception. So therefore you might need to cast it before Object ref so that it consider it as a Single argument

Upvotes: 1

M Abbas
M Abbas

Reputation: 6479

You should replace:

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

By

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

Upvotes: 0

morgano
morgano

Reputation: 17422

Try this in the line you're getting the error:

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

Upvotes: 1

Related Questions