arrays - JAVA REFLECTION : java.lang.IllegalArgumentException: argument type mismatch -
am using java reflection, here using object org.openqa.selenium.remote.remotewebelement, call method called sendkeys. method accept charactersequence array parameter type, passing charactersequence[] parameter . using classname.getmethod(sendkeys, charactersequence[]) can methodname. when method innvoked in runtime passing array of charactersequences[] argument throws
java.lang.illegalargumentexception: argument type mismatch @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ com.thbs.manager.reflections.callseleniumcommand(reflections.java:115) @ com.google.manager.testing.dotestexecution(testing.java:105) @ com.google.manager.testing.main(testing.java:22)
could not post entire code,but posted code throws error
objtocallwith = org.openqa.selenium.remote.remotewebelement cls = objtocallwith.getclass(); methodname = sendkeys params = charsequence[].class // send keys method accept charsequence[] mymethod = cls.getmethod(methodname, params); // able getmethod args = new charsequence[]{'a','b'} // here exception java.lang.illegalargumentexception: argument type mismatch mymethod.invoke(objtocallwith,args);
mymethod.invoke(objtocallwith,args);
should changed to:
mymethod.invoke(objtocallwith, new object[]{args})
;
or
mymethod.invoke(objtocallwith, (object)args)
;
this because method.invoke
accepting varargs arguments. so, mymethod.invoke(objtocallwith,args);
equivalent mymethod.invoke(objtocallwith,"a", "b");
, gives two charsqeunce objects, rather one charsqeuqnce[]
array object.
as might know, foo(charsequence... args)
internally compiled foo(charsqeuqnce[] arg)
. so, when call foo("a", "b")
, internally foo(new charsequence[]{"a", "b"})
, one charsequence[]
array object.
Comments
Post a Comment