To use Reflections, you must create an instance using a ConfigurationBuilder. You should specify the packages to scan and use filterInputsBy with a FilterBuilder to include or exclude specific packages to avoid scanning unnecessary 3rd party libraries.
Important: You must configure the specific Scanners you intend to query. If a scanner is not configured, the query will return an empty result. By default, only SubTypes and TypesAnnotated are used.
import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import static org.reflections.scanners.Scanners.*;
// Typical usage: scan a package with default scanners (SubTypes, TypesAnnotated)
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.forPackage("com.my.project")
.filterInputsBy(new FilterBuilder().includePackage("com.my.project")));
// Advanced usage: scan with specific scanners and package filters
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.forPackage("com.my.project")
.filterInputsBy(new FilterBuilder().includePackage("com.my.project").excludePackage("com.my.project.exclude"))
.setScanners(TypesAnnotated, MethodsAnnotated, MethodsReturn));