Parsing Java Reflection Java.lang.IllegalArgumentException:wrong number of Arguments__java

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.