Today in writing a reflection of the example, encountered Java.lang.IllegalArgumentException:wrong number of arguments this error, and then read the source code, only to know the cause of the error, a very low-level error;
First of all, my code reads: In the piece of code that I annotate in my JPA column:
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > Public void Parse (Object obj, String methodname,string chinesename,string enlishname) {
method[] ms = Obj.ge Tclass (). GetMethods ();
For (method M:ms) {
if (methodname.equals (M.getname ())) {
HelloWorld hw = m.getannotation (helloworld.class); c5/>try{
M.invoke (obj, (object) Chinesename, (object) enlishname);
catch (Exception e) {
e.printstacktrace ();}}}
</span>
First I put
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >m.invoke (obj, (object) Chinesename, (object) enlishname);</span>
Wrote the new Oject[]{chinesename, (Object) Enlishname}, reported this error, and then I found that I resolved this method is 2 parameters, I write this, in fact, is to pass an array of parameters, so does not correspond; and then I changed it.
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >m.invoke (Obj,chinesename,enlishname);</span>
And there is:
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >public class Maintest {public
static void Main (string[] args) {
TestCase TC = new TestCase ("Harry", "Wangwu");
parseannotaion Parseanno = new Parseannotaion (); System.out.print ("Method print ():"); System.out.println (" ------------------------------"); Parseanno.parse (TC, "print", "Zhangsan", "Lisi"); System.out.print ("\ n" + "Method toString ():"); System.out.println (" --------------------------------"); Parseanno.parse (TC, "toString", "", "");
Parseanno.parse (TC, "print", New string[]{"1", "2"});
}
</span>
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > Public
String Print (string[] str) {
System.out.println (str[0]+ ", +str[1]);
Return str[0]+ "," +str[1];
} </span>
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >public void Parse (Object obj, String methodname,string[] name {
method[] ms = Obj.getclass (). GetMethods ();
For (method M:ms) {
if (m.isannotationpresent (helloworld.class)
&& methodname.equals (M.getname ())) {
HelloWorld HW = m.getannotation (helloworld.class);
System.out.println ("Hw.chinesename:" +hw.chinesename ());
System.out.println ("Hw.value:" +hw.enlishname ());
try{
System.out.println ("before Invoke");
M.invoke (obj,name);
System.out.println ("after Invoke");
} catch (Exception e) {
e.printstacktrace ();}}}
</span>
This situation also complains
Java.lang.IllegalArgumentException:wrong Number of arguments
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:39)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:25)
At Java.lang.reflect.Method.invoke (method.java:597)
At Com.rong.zhu.jpa.ParseAnnotaion.parse (parseannotaion.java:42)
At Com.rong.zhu.jpa.MainTest.main (maintest.java:17)
The reason is:
Because the print (string str[] method has only one parameter for a string array, this is the compiler will pass the string array as a variable length parameter to object str, and we get the method with only one argument, so there will be wrong number of Arguments exception, we can resolve this exception by casting an array of strings to an object, M.invoke (obj, (Object) name);