Loopback error when using orElseThrow

Using Guava ClassPath I am trying to initialize classes located in a specific package, but I want to use a constructor to initialize as this does not apply to exceptions. Here is what I developed to create constructors:

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

However, this leads to an error in InteliJ by simply declaring a cyclic interface. I think I know what the loopback interface is, but I'm not sure why this might cause this error. As far as I know, as long as the return type is known (for orElseThrow it has a return value in this case as Constructor<?> ), Then throwing an unchecked exception should be fine. If I use orElse(null) , the error will disappear. What happens here and how can I throw a RuntimeException that I want to throw?

+8
java lambda guava java-8
source share
1 answer

An exception from the runtime is thrown from the lambda map. An exception can be handled inside the api thread.

To avoid this, you can use the flatMap method to replace the current stream with the merged threads provided to your flatMap method. This is commonly used when combining threads. See also the adam bien example on a flat map.

In this example, the exception should not go through the streaming api - so you can also use checked exceptions and not be limited to the signature in the Stream interface, which does not have a throw clause.

  ClassPath.from(classloader).getTopLevelClasses("test.package").stream() .map(ClassPath.ClassInfo::load) .map(Class::getConstructors) .flatMap(Arrays::stream) .filter(constructor -> constructor.getParameterCount() == 0) .findAny() .orElseThrow(RuntimeException::new); 
0
source share

All Articles