使用
Guava's ClassPath我试图初始化位于特定包中的类,但我想使用构造函数进行初始化,因为它不会传播异常.所以这就是我为获得构造函数而设计的:
ClassPath.from(classLoader).getTopLevelClasses("test.package").stream()
.map(ClassPath.ClassInfo::load)
.map(Class::getConstructors)
.map(Arrays::stream)
.map(constructorStream -> constructorStream
.filter(constructor -> constructor.getParameterCount() == 0)
.findAny()
.orElseThrow(RuntimeException::new)
);
但是,这只会在简单地说明循环接口时在InteliJ中出错.我想我知道Cyclic接口是什么,但我不确定为什么会导致这个错误.据我所知,只要返回类型是已知的(对于orElseThrow,它在这种情况下它的返回值为Constructor)然后抛出未经检查的异常应该没问题.如果我使用orElse(null),则错误消失.这里发生了什么以及如何抛出我想抛出的RuntimeException?