3

Am using java reflection, Here am using object org.openqa.selenium.remote.RemoteWebElement, to call method called sendKeys. This method will accept characterSequence Array as a parameter type, so am passing characterSequence[] as parameter . Using classname.getMethod(sendKeys, characterSequence[]) I can get the methodname. But when this method is innvoked in runtime passing array of characterSequences[] as argument it throws

java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.thbs.manager.Reflections.callSeleniumCommand(Reflections.java:115)
at com.google.manager.Testing.doTestExecution(Testing.java:105)
at com.google.manager.Testing.main(Testing.java:22)

Could not post the entire code,but posted the code which throws error

objToCallWith = org.openqa.selenium.remote.RemoteWebElement
cls = objToCallWith.getClass();

methodName = sendKeys
params = CharSequence[].class
// send keys method accept only CharSequence[]
myMethod = cls.getMethod(methodName, params);

// able to getMethod

args = new CharSequence[]{'a','b'}
// Here Exception java.lang.IllegalArgumentException: argument type mismatch
myMethod.invoke(objToCallWith,args);
2
  • new CharSequence[]{'a','b'} is wrong. 'a' is char type and thus cannot be converted to CharSequence. You should change to new CharSequence[]{"a","b"}and it will work. Commented Mar 1, 2016 at 6:48
  • new CharSequence[]{"a","b"} is also not working showing illegal argument Exception Commented Mar 1, 2016 at 7:00

2 Answers 2

3

myMethod.invoke(objToCallWith,args);

should be changed to:

myMethod.invoke(objToCallWith, new Object[]{args});

or

myMethod.invoke(objToCallWith, (Object)args);

This is because Method.invoke is also accepting varargs arguments. So, myMethod.invoke(objToCallWith,args); is equivalent to myMethod.invoke(objToCallWith,"a", "b");, which gives two CharSqeunce objects, rather than one CharSqeuqnce[] array object.

As you might know, foo(CharSequence... args) is internally compiled as foo(CharSqeuqnce[] arg). So, when you call foo("a", "b"), it is internally foo(new CharSequence[]{"a", "b"}), with one CharSequence[] array object.

Sign up to request clarification or add additional context in comments.

1 Comment

Error Resolved : CharSequence[]arr=CharSequence[])Array.newInstance(CharSequence.class,1); arr[0]="ab"; returnObj = myMethod.invoke(objToCallWith,new Object[]{arr});
-1
CharSequence[]arr=CharSequence[])Array.newInstance(CharSequence.class,1);
arr[0]="ab";
returnObj = myMethod.invoke(objToCallWith,new Object[]{arr});

2 Comments

Please add some explanation. Otherwise, it is likely to be deleted.
Please add some explanation. Currently, your answer is flagged "low quality" and might get removed.

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.