使用java中的反射动态实例化类失败

时间:2012-04-02 14:41:53

标签: java reflection constructor

我有这段代码

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) {
    }
}

thisApplication的实例。

我有几个SubApplication的实现。合同的一部分是这个类的所有子类都需要实现一个以Application为参数的构造函数。

现在我总是收到InstanciationException,尽管我确切地知道子类包含这样的构造函数。有关例外的详细信息不可用:getMessage()返回null


编辑: SubApplication是抽象的。但appClasses仅包含 sub - SubApplication

的类

Edit2: 我必须“构建”堆栈跟踪,因为通过合同,我必须捕获异常。

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)

任何想法都表示赞赏。

2 个答案:

答案 0 :(得分:2)

尝试此代码时:

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 );
}

答案 1 :(得分:0)

如评论中所述。我愚蠢到没有验证我的数组中的类名称。实际上,我确实已将SubApplication添加到列表中。无论如何,感谢您的投入!