1

I'm trying to use Reflections classes as suggested here stackoverflow.com/questions/12538761/

Reflections reflections = new Reflections("my.project.prefix");
Set<Class<? extends SomeType>> subTypes = 
        reflections.getSubTypesOf(SomeType.class);
Set<SomeType> someTypes = new HashSet<SomeType>(subTypes.size());
for (Class<? extends SubType> subType : subTypes) {
    someTypes.add(subType.newInstance());
}

However, I cant seem to import the necessary package. NetBeans cant find it, and also I tried importing java.lang.reflections but it doesn't do it. I can see java.lang.Class, but I don't see Reflections class anywhere.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
tamir
  • 81
  • 2
  • 9
  • 1
    "I tried importing java.lang.reflections but it doesn't do it." That might just have to do with the fact that there is no such package as `java.lang.reflections`. – Lew Bloch May 26 '17 at 15:17
  • right, it's `java.lang.reflect`. thanks – tamir May 26 '17 at 15:38
  • 1
    Which does not contain the type you're trying to import as the Javadocs clearly show. http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/package-frame.html – Lew Bloch May 26 '17 at 16:07

2 Answers2

4

Reflections isn't part of Java, it's a 3rd party library. First, you need to add the relevant Jar to your classpath. E.g., if you're using Maven, you could add the following dependency:

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.11</version>
</dependency>

Once you've done that, you need to import the Reflections class, as you noted. It is located in the org.reflections package:

import org.reflections.Reflections
Mureinik
  • 297,002
  • 52
  • 306
  • 350
3

It's a library https://github.com/ronmamo/reflections Add it via Maven, Gradle or manually

Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91