gpt4 book ai didi

java - `Class.newInstance()` 抛出带有反射的 java.lang.StackOverflowError

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:39 25 4
gpt4 key购买 nike

代码:

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)

仅供引用,BaseTab 正在扩展 javafx 标签。

我发现它在得到错误之前迭代了每个类并创建了数百个实例。问题是我的代码哪里出错了,我该如何解决?谢谢。

最佳答案

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

关于java - `Class<? extends BaseTab>.newInstance()` 抛出带有反射的 java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com