解析Java反射java.lang.IllegalArgumentException: wrong number of arguments__Java

來源:互聯網
上載者:User

今天在寫一個反射的例子的時候,遇到了java.lang.IllegalArgumentException: wrong number of arguments這個錯誤,後來看了原始碼,才知道錯誤的原因,一個很低級的錯誤;

首先My Code是這樣寫的:在我的jpa欄中註解的那塊代碼中:

<span style="font-family:SimSun;font-size:18px;">public void parse(Object obj, String methodName,String chineseName,String EnlishName) {Method[] ms = obj.getClass().getMethods();for (Method m : ms) {if (methodName.equals(m.getName())) {HelloWorld hw = m.getAnnotation(HelloWorld.class);try{m.invoke(obj,(Object)chineseName,(Object)EnlishName);}catch(Exception e){e.printStackTrace();}}}}</span>
首先我把
<span style="font-family:SimSun;font-size:18px;">m.invoke(obj,(Object)chineseName,(Object)EnlishName);</span>
寫成了 new Oject[]{chineseName,(Object)EnlishName},報這個錯,然後我發現我解析的這個方法中是2個參數,我這樣寫,其實是傳一個數組參數的,所以不對應;後來我就改成了
<span style="font-family:SimSun;font-size:18px;">m.invoke(obj,chineseName,EnlishName);</span>
還有就是:

<span style="font-family:SimSun;font-size:18px;">public class MainTest {public static void main(String[] args) {TestCase tc = new TestCase("王五","wangwu");ParseAnnotaion parseAnno = new ParseAnnotaion();//System.out.print("方法  print():");//System.out.println("   ------------------------------");//parseAnno.parse(tc, "print", "zhangsan", "lisi");//System.out.print("\n"+"方法 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>

這種情況也報錯
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)

原因是:

由於print(String str[])方法只有一個String數組的參數所以這是編譯器會把字串數組當作一個可變長度參數傳給對象str,而我們取得方法只有一個參數,所以就會出現wrong number of arguments的異常,我們只要把字串數組強制轉換為一個Object對象就可以解決這個異常了,m.invoke(obj,(Object)name);就解決了;


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.