`类<! - 扩展BaseTab - > .newInstance()`使用Reflections抛出java.lang.StackOverflowError

时间:2015-07-03 05:33:17

标签: java class javafx google-reflections

代码:

Reflections reflections = new Reflections("com.erby.tab.tabs");

for(Class<? extends BaseTab> t : reflections.getSubTypesOf(BaseTab.class)) {
    try {
        BaseTab tab = t.newInstance();

        System.out.println(tab.toString());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

BaseTab.java:

public abstract class BaseTab extends Tab implements Serializable {
    public abstract String getPrefix();

    @Override
    public abstract String toString();
}

HomeTab.java:

public class HomeTab extends BaseTab {
    public HomeTab() {
        setText(toString());
    }

    @Override
    public String getPrefix() {
        return "home";
    }

    @Override
    public String toString() {
        return "Home Tab";
    }
}

调用时,我得到:

Caused by: java.lang.StackOverflowError
at sun.misc.URLClassPath.getLoader(URLClassPath.java:476)
at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:457)
at sun.misc.URLClassPath.access$100(URLClassPath.java:64)
at sun.misc.URLClassPath$1.next(URLClassPath.java:239)
at sun.misc.URLClassPath$1.hasMoreElements(URLClassPath.java:250)
at java.net.URLClassLoader$3$1.run(URLClassLoader.java:601)
at java.net.URLClassLoader$3$1.run(URLClassLoader.java:599)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader$3.next(URLClassLoader.java:598)
at java.net.URLClassLoader$3.hasMoreElements(URLClassLoader.java:623)
at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45)
at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54)
at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45)
at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54)
at org.reflections.util.ClasspathHelper.forResource(ClasspathHelper.java:61)
at org.reflections.util.ClasspathHelper.forPackage(ClasspathHelper.java:51)
at org.reflections.util.ConfigurationBuilder.build(ConfigurationBuilder.java:93)
at org.reflections.Reflections.<init>(Reflections.java:170)
at org.reflections.Reflections.<init>(Reflections.java:143)
at com.erby.tab.tabs.TestTab.<init>(TestTab.java:20)
at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at com.erby.tab.tabs.TestTab.<init>(TestTab.java:24)
at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at com.erby.tab.tabs.TestTab.<init>(TestTab.java:24)
at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)

FYI BaseTab正在延伸 javafx 标签。

我发现它在每个类上进行迭代并在获得错误之前创建数百个实例。 任务是我的代码出错的地方,我该如何解决?感谢。

3 个答案:

答案 0 :(得分:1)

正如您在评论中所写,第一个代码段出现在构造函数中。该代码段创建新实例,这会导致再次调用该consturctor。这导致无限递归,以堆栈溢出错误结束。

答案 1 :(得分:0)

Loop是一个构造函数。保持重复。

答案 2 :(得分:0)

我认为名称TestTab的另一个子类正在从它的构造函数中重复初始化。我从您的错误消息中看到了这一点。检查出。尽管如此,我还没有看到任何名字TestTab的类别。