- 反射机制出现的InstantiationException异常@H_502_3@
做反射机制时,执行如下的语句:@H_502_3@
Class class1 = Class.forName(className);@H_502_3@
Object o1 = class1.newInstance();@H_502_3@
出下面的异常:@H_502_3@
Exception in thread "main" java.lang.@H_502_3@InstantiationException@H_502_3@: classtest.PersonServiceBean@H_502_3@
@H_502_3@at java.lang.Class.newInstance0(Class.java:340)@H_502_3@
at java.lang.Class.newInstance(Class.java:308)@H_502_3@
at classtest.JustTest.classInstanceTest(JustTest.java:33)@H_502_3@
at classtest.JustTest.main(JustTest.java:13)@H_502_3@
@H_502_3@@H_502_3@
Class class1 = Class.forName(className);@H_502_3@
Object o1 = class1.newInstance();@H_502_3@
出下面的异常:@H_502_3@
Exception in thread "main" java.lang.@H_502_3@InstantiationException@H_502_3@: classtest.PersonServiceBean@H_502_3@
@H_502_3@
at java.lang.Class.newInstance(Class.java:308)@H_502_3@
at classtest.JustTest.classInstanceTest(JustTest.java:33)@H_502_3@
at classtest.JustTest.main(JustTest.java:13)@H_502_3@
进行debug调试:@H_502_3@
程序运行到Class.class文件,@H_502_3@
try {@H_502_3@
Class[] empty = {};@H_502_3@
final Constructor<T> c =@H_502_3@getConstructor0(empty,Member.DECLARED);@H_502_3@@H_502_3@//@H_502_3@执行到此句时,出noSuchMethod异常,跳到catch@H_502_3@
java.security.AccessController.doPrivileged@H_502_3@
(new java.security.PrivilegedAction() {@H_502_3@
public Object run() {@H_502_3@
c.setAccessible(true);@H_502_3@
return null;@H_502_3@
}@H_502_3@
});@H_502_3@
cachedConstructor = c;@H_502_3@
}@H_502_3@catch (NoSuchMethodException e) {//出现noSuchMethod异常 @H_502_3@
throw new InstantiationException(getName()); @H_502_3@//抛出的是java.lang.I@H_502_3@nstantiationException@H_502_3@
}@H_502_3@
@H_502_3@ @H_502_3@@H_502_3@
程序运行到Class.class文件,@H_502_3@
try {@H_502_3@
Class[] empty = {};@H_502_3@
final Constructor<T> c =@H_502_3@getConstructor0(empty,Member.DECLARED);@H_502_3@@H_502_3@//@H_502_3@执行到此句时,出noSuchMethod异常,跳到catch@H_502_3@
java.security.AccessController.doPrivileged@H_502_3@
(new java.security.PrivilegedAction() {@H_502_3@
public Object run() {@H_502_3@
c.setAccessible(true);@H_502_3@
return null;@H_502_3@
}@H_502_3@
});@H_502_3@
cachedConstructor = c;@H_502_3@
}@H_502_3@catch (NoSuchMethodException e) {
}@H_502_3@
@H_502_3@