遇到的第一个关于反射的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);

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java 反射 不定参数bug的更多相关文章

  1. Java的不定参数(eg:Object...)(转)

    第一个例子: public class VariArgs { public static void main(String[] args) { test(); test("aaa" ...

  2. java中不定参数的使用

    https://www.cnblogs.com/xy-hong/p/7192796.html

  3. Java中不定项参数(可变参数)的使用

    Java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理. 注意事项:   1)不定项参数必须放在参数列表最后一个.   2)不定项参数只能有一个(多 ...

  4. Java不定参数Object… obj 和 Object[] 的区别

    Java不定参数Object… obj 和 Object[] 的区别 简述: java中方法重载可以实现参数不同自动匹配对应方法.但现实中也存在这种问题.普通传参对于形如下面的方法,却显得臃肿而失优雅 ...

  5. Java不定参数

    先看两个简单的例子,来感受一下Java的不定长度参数 第一个例子: public class VariArgs { public static void main(String[] args) { t ...

  6. golang 防SQL注入 基于反射、TAG标记实现的不定参数检查器

    收到一个任务,所有http的handler要对入参检查,防止SQL注入.刚开始笨笨的,打算为所有的结构体写一个方法,后来统计了下,要写几十上百,随着业务增加,以后还会重复这个无脑力的机械劳作.想想就l ...

  7. java 编程基础 Class对象 反射 :参数反射

    方法参数反射 Java8在java.lang.reflect包下新增了Executable抽象基类,该对象代表可执行的类成员,该类派生了Constructor和Method两个子类.Executabl ...

  8. 有关 java 不定参数

    不定参数实际为数组参数的一种写法而已,本质上与数组参数完全相同 //1.数组参数函数 public static int sum(int[] values) { } //2.不定参数函数 不定参数只能 ...

  9. java 反射借助 asm 获取参数名称最优雅简单的方式

    背景说明 最近写反射相关的代码,想获取对应的参数名称,却发现没有特别好的方式. jdk7 及其以前,是无法通过反射获取参数名称的. jdk8 可以获取,但是要求指定 -parameter 启动参数,限 ...

随机推荐

  1. Ubuntu安装ss

    安装环境:ubuntu 16.04 (推荐使用此版本-2019年3月) 本文假设读者已经拥有一台vps. 安装ss 首先通过终端以root身份登录vps $ ssh root@[IP Address] ...

  2. Django:表单字段如何在模板中用中文显示

    在处理中文显示的时候,刚开始接触django时,很容易弄混表单汉化和Admin后台汉化. 1.表单汉化:是针对用户的.用户浏览网页时,表单的名称如果是英文的,不够友好,所以需要显示为中文,虽然代码编写 ...

  3. 针对铁定浏览器的css选择符

    /***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child ...

  4. 使用maven构建scala项目

    eclipse安装scala插件和m2e-scala并不是支持的很好,因此使用maven创建scala工程的时候,IDEA可谓是最好的开发利器. 1. 创建工程之前的准备 2. IDEA界面创建Sca ...

  5. SFTP Using Chilkat Active component

    https://www.example-code.com/vb6/sftp_uploadBandwidthThrottle.asp Private Sub Command1_Click() ' Imp ...

  6. 苏州Uber优步司机奖励政策(4月22日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. 【转载】MFC的Main函数跑哪去了

    原文:http://blog.csdn.net/weiwenhp/article/details/8455471 习惯的思维 用习惯了C的人要看一个程序时首先会想到找到那个main函数在哪,然后再顺着 ...

  8. Python day1 ---python基础1

    本节内容 Python介绍 编程语言分类 Hello World程序 变量 字符编码 用户输入 数据类型初识 表达式if ...else语句 表达式while 循环 表达式for 循环 break a ...

  9. let和var定义变量的区别

    使用 let 语句声明一个变量,该变量的范围限于声明它的块中.  可以在声明变量时为变量赋值,也可以稍后在脚本中给变量赋值. 使用 let 声明的变量,在声明前无法使用,否则将会导致错误. 如果未在  ...

  10. bootstrap图标显示为方框的解决方案

    这是因为没有 fonts 库导致的,只需要放入项目目录即可正常显示图标