遇到的第一个关于反射的bug:java.lang.IllegalArgumentException: wrong number of
arguments的问题解析如下:
1、错误bug
wrong number of arguments
at 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);
}
执行代码:
@Test
public 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 block
e1.printStackTrace();
}
try {
oObject = aCreateClassByString.getAttributeObject1(
"cn.javass.spring.chapter5.ZhengBean", "say","i love you ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
System.out.println("结果::"+oObject); } }
package cn.javass.spring.chapter5; import java.lang.reflect.Constructor; public class CreateClassByString {
/**
* 根据传入的类名和值,动态构造该类的实例
*
* @param _sClassName
* 要构造的类名 如:java.lang.String
* @param _sRealValue
* 要创建的对象的值,比如“wuguowei”
* @return 返回String对象,即值为“wuguowei”的字符串
* @throws Exception
*/
public Object getAttributeObject1(String _sClassName, String... _sRealValue)
throws Exception {
// 1. 根据指定的类名,获取到对应的类
Class clazz = Class.forName(_sClassName); // 2. 获取指定参数类型的构造函数,这里传入我们想调用的构造函数所需的参数类型
@SuppressWarnings("unchecked")
Constructor constructor = clazz.getConstructor(String[].class); // 3. 根据构造函数,创建指定类型的对象,传入的参数个数需要与上面传入的参数类型个数一致
System.out.println("create object begin");
Object object = constructor.newInstance(_sRealValue);
System.out.println("create object end");
// 4.返回对象
return object;
}
}
编译环境:jdk1.7
错误分析:由于public ZhengBean(String... name){构造器只有一个String数组的参数所以这是编译器会把字符串数组当作一个可变长度参数传 给对象name,而我们取得方法只有一个参数,所以就会出现wrong number of arguments的异常,我们只要把字符串数组强制转换为一 个Object对象就可以解决这个异常了,
解决方案:
Object object = constructor.newInstance((Object)_sRealValue);
版权声明:本文为博主原创文章,未经博主允许不得转载。