Java反射出现死循环的一个疑问

chenshuo_szu 2015-02-25 03:01:19
public class Example_01 {
String s;
int i, i2, i3;
private Example_01() {
}
protected Example_01(String s, int i) {
this.s = s;
this.i = i;
}
public Example_01(String... strings) throws NumberFormatException {
if (0 < strings.length)
i = Integer.valueOf(strings[0]);
if (1 < strings.length)
i2 = Integer.valueOf(strings[1]);
if (2 < strings.length)
i3 = Integer.valueOf(strings[2]);
}
public void print() {
System.out.println("s=" + s);
System.out.println("i=" + i);
System.out.println("i2=" + i2);
System.out.println("i3=" + i3);
}
}

import java.lang.reflect.*;

public class Main_01 {

public static void main(String[] args) {

Example_01 example = new Example_01("10", "20", "30");
Class<? extends Example_01> exampleC = example.getClass();

Constructor[] declaredConstructors = exampleC.getDeclaredConstructors();
for (int i = 0; i < declaredConstructors.length; i++) {
Constructor<?> constructor = declaredConstructors[i];
System.out.println("查看是否允许带有可变数量的参数:" + constructor.isVarArgs());
System.out.println("该构造方法的入口参数类型依次为:");
Class[] parameterTypes = constructor.getParameterTypes();
for (int j = 0; j < parameterTypes.length; j++) {
System.out.println(" " + parameterTypes[j]);
}
System.out.println("该构造方法可能抛出的异常类型为:");
Class[] exceptionTypes = constructor.getExceptionTypes();
for (int j = 0; j < exceptionTypes.length; j++) {
System.out.println(" " + exceptionTypes[j]);
}
Example_01 example2 = null;
while (example2 == null) {
try {
if (i == 2)
example2 = (Example_01) constructor.newInstance();
else if (i == 1)
example2 = (Example_01) constructor.newInstance("7", 5);
else {
Object[] parameters = new Object[] { new String[] {
"100", "200", "300" } };
example2 = (Example_01) constructor
.newInstance(parameters);
}
} catch (Exception e) {
System.out.println("在创建对象时抛出异常,下面执行setAccessible()方法");
constructor.setAccessible(true);
}
}
if(example2!=null){
example2.print();
System.out.println();
}
}

}

}

请问各位大神,当我把while循环中的“if(i==2)”改成“if(i==0)”时为什么会出现死循环?两者的道理不一样吗?
...全文
264 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
波澜不惊wave 2015-02-28
  • 打赏
  • 举报
回复
Constructor[] declaredConstructors = exampleC.getDeclaredConstructors(); 这里取得的构造器的顺序跟你在Example_01类声明的顺序保持一致。从你的代码来看,死循环只有在 if(i == 2) 的时候才可能发生。 原因是 当i = 0时, 应该是作用域为private的缺省构造器,但你会进入这个分支, else { Object[] parameters = new Object[] { new String[] { "100", "200", "300" } }; example2 = (Example_01) constructor .newInstance(parameters); } 这里抛出异常 java.lang.IllegalArgumentException: wrong number of arguments 导致程序死循环。 将if(i==2)代码改为if(i == 0) 即可。
爱摸鱼de老邪 2015-02-25
  • 打赏
  • 举报
回复
当然不一样。从你的程序看,用反射取得的构造器与i是对应的: i=0时,conductor是接受变参的构造器,会抛出java.lang.NumberFormatException; i=1时,conductor是接受<string,int>的构造器; i=2时conductor才是缺省构造器;

if (i == 2)
      example2 = (Example_01) constructor.newInstance();
这句你改成

if (i == 0)
      example2 = (Example_01) constructor.newInstance();
即去调用变参构造器而不给出入参,就会抛出NumberFormatException(IllegalArgumentException的子类),导致创建对象实例失败,使得循环条件一直成立,即陷入死循环。 你可以将该处改成i==0,然后设置断点,同时在异常处理里输出捕获到的异常类型,debug试试,可以看到控制台输出: java.lang.IllegalArgumentException: wrong number of arguments

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧