我有这段代码
List<Class<? extends SubApplication>> appClasses = new ArrayList<Class<? extends SubApplication>>();
List<SubApplication> subApps = new ArrayList<SubApplication>();
// instructions filling "appClasses"
// ... other instructions
for (Class<? extends SubApplication> crtClass : appClasses) {
try {
Constructor<? extends SubApplication> constructor = crtClass.getConstructor(new Class<?>[] { Application.class });
SubApplication subApp = constructor.newInstance(this);
if (!subApps.contains(subApp)) {
subApps.add(subApp);
}
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (InstantiationException e) {
System.out.println(e.getMessage());
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
而
this
是Application
的实例。我有几个
SubApplication
的实现。合同的一部分是此类的所有子类都需要实现以Application
为参数的构造函数。现在,即使我确切知道子类包含此类构造函数,也始终会收到
InstanciationException
。没有有关该异常的更多详细信息:getMessage()
返回null
编辑:
SubApplication
是抽象的。但是appClasses
仅包含SubApplication
的子类Edit2:我必须“构造” stacktrace,因为根据合同,我必须捕获异常。
sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
ch.migros.gmaare.tk.auth.gui.AuthenticableApp.instanciateSubApps(AuthenticableApp.java:64)
ch.migros.gmaare.tk.auth.gui.AuthenticableApp.<init>(AuthenticableApp.java:42)
ch.migros.gmaare.tk.voicesuite.VoicesuiteApplication.<init>(VoicesuiteApplication.java:34)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
java.lang.Class.newInstance0(Class.java:355)
java.lang.Class.newInstance(Class.java:308)
com.vaadin.terminal.gwt.server.ApplicationServlet.getNewApplication(ApplicationServlet.java:82)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.createApplication(AbstractApplicationServlet.java:978)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.findApplicationInstance(AbstractApplicationServlet.java:801)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:456)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:662)
任何想法表示赞赏。
最佳答案
当您尝试此代码时:
import java.lang.reflect.Constructor;
public class Main {
public static void main( final String[] args ) {
final Class<? extends A>[] classes = new Class[] { B.class, C.class };
for ( final Class<? extends A> clazz : classes ) {
try {
final Constructor<? extends A> constructor = clazz.getConstructor( null );
final A a = constructor.newInstance( null );
} catch ( final Exception e ) {
e.printStackTrace();
}
}
}
public static abstract class A {
}
public static class B extends A {
}
public static abstract class C extends A {
}
}
您将得到相同的错误:
java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at Main.main(Main.java:10)
您可以将代码修改为:
if ( Modifier.isAbstract( clazz.getModifiers() ) ) {
System.out.println( "skipping" );
} else {
final A a = constructor.newInstance( null );
}