The console shows a ArrayIndexOutOfBoundsException
when I run this program, it could not find the argument in main()
. I really don't know the reason, because I do have the argument String[] args
in public static void main(String[] args)
:
package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.time.chrono.JapaneseChronology;
public class ClassViewer {
public static void view(String clazName) throws ClassNotFoundException{
Class clz=Class.forName(clazName);
Package p=clz.getPackage();
System.out.printf("package %s;%n", clz.getName());
int modifier=clz.getModifiers();//取得类型的修饰常数
System.out.printf("%s %s %s {%n",
Modifier.toString(modifier),
Modifier.isInterface(modifier) ? "interface" :"class",
clz.getName()
);
//取得声明的数据成员代表对象
Field[] fields =clz.getDeclaredFields();
for(Field field:fields){
//显示权限修饰
System.out.printf("\t%s %s %s;%n",
Modifier.toString(field.getModifiers()),
field.getType().getName(),
field.getName()
);
}
//取得声明的创建方法代表对象
Constructor[] constructors=clz.getDeclaredConstructors();
for(Constructor constructor:constructors){
System.out.printf("\t%s %s();%n",
Modifier.toString(constructor.getModifiers()),
constructor.getName()
);
}
//取得声明的方法成员代表对象
Method[] methods=clz.getDeclaredMethods();
for(Method method:methods){
System.out.printf("\t%s %s %s;%n",
Modifier.toString(method.getModifiers()),
method.getReturnType(),
method.getName()
);
}
System.out.println("}");
}
public static void main(String[] args) {
try {
ClassViewer.view(args[0]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array Index Out Of Bounds Exception ");
}
catch (ClassNotFoundException e) {
System.out.println("can not find the class");
}
}
}