Java 反射 不定参数bug
生活随笔
收集整理的這篇文章主要介紹了
Java 反射 不定参数bug
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Java 反射 不定參數(shù)bug 遇到的第一個關于反射的bug:java.lang.IllegalArgumentException: wrong number of
arguments的問題解析如下:
??
異常引起的原因分析: ??
??
引起錯誤的原碼: ??
public ZhengBean(String... name){System.out.println(name);}
執(zhí)行代碼: ??
? ?@Testpublic void test() {CreateClassByString aCreateClassByString = new CreateClassByString();Object oObject = null;try {Class clazz = Class.forName("cn.javass.spring.chapter5.ZhengBean");System.out.println(clazz.getName());} catch (ClassNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {oObject = aCreateClassByString.getAttributeObject1("cn.javass.spring.chapter5.ZhengBean", "say","i love you ");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{System.out.println("結(jié)果::"+oObject);}}
? ??package cn.javass.spring.chapter5;import java.lang.reflect.Constructor;public class CreateClassByString {/*** 根據(jù)傳入的類名和值,動態(tài)構造該類的實例** @param _sClassName* 要構造的類名 如:java.lang.String* @param _sRealValue* 要創(chuàng)建的對象的值,比如“wuguowei”* @return 返回String對象,即值為“wuguowei”的字符串* @throws Exception*/public Object getAttributeObject1(String _sClassName, String... _sRealValue)throws Exception {// 1. 根據(jù)指定的類名,獲取到對應的類Class clazz = Class.forName(_sClassName);// 2. 獲取指定參數(shù)類型的構造函數(shù),這里傳入我們想調(diào)用的構造函數(shù)所需的參數(shù)類型@SuppressWarnings("unchecked")Constructor constructor = clazz.getConstructor(String[].class);// 3. 根據(jù)構造函數(shù),創(chuàng)建指定類型的對象,傳入的參數(shù)個數(shù)需要與上面?zhèn)魅氲膮?shù)類型個數(shù)一致System.out.println("create object begin");Object object = constructor.newInstance(_sRealValue);System.out.println("create object end");// 4.返回對象return object;} }
編譯環(huán)境:jdk1.7
??
錯誤分析:由于public ZhengBean(String... name){構造器只有一個String數(shù)組的參數(shù)所以這是編譯器會把字符串數(shù)組當作一個可變長度參數(shù)傳 給對象name,而我們?nèi)〉梅椒ㄖ挥幸粋€參數(shù),所以就會出現(xiàn)wrong number of arguments的異常,我們只要把字符串數(shù)組強制轉(zhuǎn)換為一 ?個Object對象就可以解決這個異常了, ??
??
解決方案: ??
? Object object = constructor.newInstance((Object)_sRealValue);
arguments的問題解析如下:
1、錯誤bug ??
wrong number of argumentsat sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.lang.reflect.Constructor.newInstance(Constructor.java:525)at cn.javass.spring.chapter5.CreateClassByString.getAttributeObject1(Unknown Source)at cn.javass.spring.chapter5.ZjxTest.test(Unknown Source)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:601)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)??
異常引起的原因分析: ??
??
引起錯誤的原碼: ??
public ZhengBean(String... name){System.out.println(name);}
執(zhí)行代碼: ??
? ?@Testpublic void test() {CreateClassByString aCreateClassByString = new CreateClassByString();Object oObject = null;try {Class clazz = Class.forName("cn.javass.spring.chapter5.ZhengBean");System.out.println(clazz.getName());} catch (ClassNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {oObject = aCreateClassByString.getAttributeObject1("cn.javass.spring.chapter5.ZhengBean", "say","i love you ");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{System.out.println("結(jié)果::"+oObject);}}
? ??package cn.javass.spring.chapter5;import java.lang.reflect.Constructor;public class CreateClassByString {/*** 根據(jù)傳入的類名和值,動態(tài)構造該類的實例** @param _sClassName* 要構造的類名 如:java.lang.String* @param _sRealValue* 要創(chuàng)建的對象的值,比如“wuguowei”* @return 返回String對象,即值為“wuguowei”的字符串* @throws Exception*/public Object getAttributeObject1(String _sClassName, String... _sRealValue)throws Exception {// 1. 根據(jù)指定的類名,獲取到對應的類Class clazz = Class.forName(_sClassName);// 2. 獲取指定參數(shù)類型的構造函數(shù),這里傳入我們想調(diào)用的構造函數(shù)所需的參數(shù)類型@SuppressWarnings("unchecked")Constructor constructor = clazz.getConstructor(String[].class);// 3. 根據(jù)構造函數(shù),創(chuàng)建指定類型的對象,傳入的參數(shù)個數(shù)需要與上面?zhèn)魅氲膮?shù)類型個數(shù)一致System.out.println("create object begin");Object object = constructor.newInstance(_sRealValue);System.out.println("create object end");// 4.返回對象return object;} }
編譯環(huán)境:jdk1.7
??
錯誤分析:由于public ZhengBean(String... name){構造器只有一個String數(shù)組的參數(shù)所以這是編譯器會把字符串數(shù)組當作一個可變長度參數(shù)傳 給對象name,而我們?nèi)〉梅椒ㄖ挥幸粋€參數(shù),所以就會出現(xiàn)wrong number of arguments的異常,我們只要把字符串數(shù)組強制轉(zhuǎn)換為一 ?個Object對象就可以解決這個異常了, ??
??
解決方案: ??
? Object object = constructor.newInstance((Object)_sRealValue);
版權聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
posted on 2015-02-01 13:55 france 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/france/p/4808577.html
總結(jié)
以上是生活随笔為你收集整理的Java 反射 不定参数bug的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tomcat7.0.8的高级应用-apr
- 下一篇: c++的ORM解决方案 -- ODB