Unable to use getSubTypesOf when subtypes use Optional or Stream
See original GitHub issueThe getSubTypesOf
method fails to find types when they use Java 8 Optionals or Streams.
import org.junit.Test;
import org.reflections.Reflections;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import static org.junit.Assert.assertTrue;
public class ReflectionBugTest {
@Test
public void shouldGetAllTypes() {
assertTrue(SomeInterface.class.isAssignableFrom(BasicType.class));
assertTrue(SomeInterface.class.isAssignableFrom(TypeWithOptional.class));
assertTrue(SomeInterface.class.isAssignableFrom(TypeWithStream.class));
Reflections reflections = new Reflections(getClass().getPackage().getName());
Set<Class<? extends SomeInterface>> subTypesOf = reflections.getSubTypesOf(SomeInterface.class);
assertTrue(subTypesOf.contains(BasicType.class)); // <-- Succeeds
assertTrue(subTypesOf.contains(TypeWithStream.class)); // <-- Fails
assertTrue(subTypesOf.contains(TypeWithOptional.class)); // <-- Fails
}
interface SomeInterface {
}
static class BasicType implements SomeInterface {
}
static class TypeWithOptional implements SomeInterface {
void foo() {
Optional.of("Fooooo").ifPresent(System.out::println);
}
}
static class TypeWithStream implements SomeInterface {
void foo() {
Stream.of("Fooooo").forEach(System.out::println);
}
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:10
Top Results From Across the Web
java - Using Reflections.getSubTypeOf() to get subtypes of ...
I'm trying to extract all classes that implement an interface and put these in a Set. However, that interface is Parameterized and I'd...
Read more >Java Examples & Tutorials of Reflections.getSubTypesOf (org ...
Can you find all classes in a package using reflection? Reflections reflections = new Reflections("my.project.prefix"); Set<Class<? extends Object>> ...
Read more >Optional (Java SE 11 & JDK 11 ) - Oracle Help Center
Optional is primarily intended for use as a method return type where there is a clear need to represent "no result," and where...
Read more >Java 8 Optionals
This class is primarily used to signify the absence or presence of a value. If you believe a value can or cannot be...
Read more >Java 8 Friday: Optional Will Remain an Option in Java
There are certain merits to the above in fluent APIs, specifically in the new Java 8 Streams API, which makes extensive use of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
An upgrade of javassist from 3.12.1.GA to 3.22.0-GA fixed that issue for me.
Having this problem as well w/ Lambdas