question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use getSubTypesOf when subtypes use Optional or Stream

See original GitHub issue

The 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:open
  • Created 7 years ago
  • Comments:10

github_iconTop GitHub Comments

5reactions
MaximeCGcommented, Dec 1, 2017

An upgrade of javassist from 3.12.1.GA to 3.22.0-GA fixed that issue for me.

1reaction
datadiditcommented, Jul 6, 2017

Having this problem as well w/ Lambdas

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found