3

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

1

3 Answers 3

1

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

myMethod = myClass.getMethod(methodName, new Class[] { String[].class });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But now when i come to line String returnValue = (String) myMethod.invoke(myClassInstance, messageArray); I get the java.lang.IllegalArgumentException: wrong number of arguments. Why i am getting this exception. I have message array with 2 length(messageArray). Thanks
now in the second line try String returnValue = (String) myMethod.invoke(myClassInstance, (Object)messageArray)
1

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

Comments

0

You should replace:

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

By

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.