Java runtime metadata analysis library that enables reverse querying of classpath metadata for type system introspection
npx @tessl/cli install tessl/maven-org-reflections--reflections@0.10.0Reflections is a Java runtime metadata analysis library that enables reverse querying of the type system by scanning and indexing classpath metadata. It provides comprehensive functionality to discover subtypes, annotated types/methods/fields, method signatures, resources, and more using both modern functional query APIs and legacy backward-compatible methods.
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.FilterBuilder;
import org.reflections.scanners.Scanners;import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.scanners.Scanners;
import static org.reflections.scanners.Scanners.*;
// Simple package scanning with default scanners
Reflections reflections = new Reflections("com.mycompany");
// Advanced configuration with all scanners
Reflections reflections = new Reflections(new ConfigurationBuilder()
.forPackages("com.mycompany.package1", "com.mycompany.package2")
.addScanners(Scanners.values()) // all available scanners
.filterInputsBy(new FilterBuilder()
.includePackage("com.mycompany")
.excludePackage("com.mycompany.exclude")));
// Query using modern functional API
Set<Class<?>> subtypes = reflections.get(SubTypes.of(MyInterface.class).asClass());
Set<Method> methods = reflections.get(MethodsAnnotated.with(MyAnnotation.class).as(Method.class));
Set<String> resources = reflections.get(Resources.with(".*\\.properties"));
// Query using legacy backward-compatible API
Set<Class<? extends MyInterface>> legacySubtypes = reflections.getSubTypesOf(MyInterface.class);
Set<Class<?>> annotatedTypes = reflections.getTypesAnnotatedWith(MyAnnotation.class);Reflections is built around several key components:
Central scanning functionality for indexing classpath metadata and performing reverse queries on the type system. Supports both configuration and querying operations.
class Reflections {
Reflections(Configuration configuration);
Reflections(String prefix, Scanner... scanners);
<T> Set<T> get(QueryFunction<Store, T> query);
static Reflections collect();
}Comprehensive scanning capabilities for different types of metadata including types, annotations, methods, constructors, fields, resources, and signatures.
enum Scanners implements Scanner, QueryBuilder, NameHelper {
SubTypes, TypesAnnotated, MethodsAnnotated, ConstructorsAnnotated,
FieldsAnnotated, Resources, MethodsParameter, ConstructorsParameter,
MethodsSignature, ConstructorsSignature, MethodsReturn;
}
interface Scanner {
List<Map.Entry<String, String>> scan(ClassFile classFile);
String index();
boolean acceptsInput(String file);
}Flexible configuration system for setting up scanning parameters, URLs, filters, class loaders, and scanner selection.
class ConfigurationBuilder implements Configuration {
static ConfigurationBuilder build(Object... params);
ConfigurationBuilder forPackage(String pkg, ClassLoader... classLoaders);
ConfigurationBuilder forPackages(String... packages);
ConfigurationBuilder setScanners(Scanner... scanners);
ConfigurationBuilder addScanners(Scanner... scanners);
ConfigurationBuilder setUrls(Collection<URL> urls);
ConfigurationBuilder addUrls(Collection<URL> urls);
ConfigurationBuilder setInputsFilter(Predicate<String> inputsFilter);
}Extensive utility classes for classpath operations, filtering, name resolution, and reflection operations to support the core scanning functionality.
abstract class ClasspathHelper {
static Collection<URL> forPackage(String name, ClassLoader... classLoaders);
static Collection<URL> forResource(String resourceName, ClassLoader... classLoaders);
static Collection<URL> forClassLoader(ClassLoader... classLoaders);
static Collection<URL> forJavaClassPath();
}
class FilterBuilder implements Predicate<String> {
FilterBuilder includePackage(String value);
FilterBuilder excludePackage(String value);
FilterBuilder includePattern(String regex);
FilterBuilder excludePattern(String regex);
}Modern functional query system using QueryFunction for composable, type-safe metadata queries with filtering, mapping, and transformation capabilities.
interface QueryFunction<C, T> extends Function<C, Set<T>>, NameHelper {
Set<T> apply(C ctx);
QueryFunction<C, T> filter(Predicate<? super T> predicate);
<R> QueryFunction<C, R> map(Function<? super T, ? extends R> function);
<R> QueryFunction<C, R> as(Class<? extends R> type, ClassLoader... loaders);
QueryFunction<C, Class<?>> asClass(ClassLoader... loaders);
}
abstract class ReflectionUtils {
static <C, T> Set<T> get(QueryFunction<C, T> function);
static final UtilQueryBuilder<Class<?>, Class<?>> SuperTypes;
static final UtilQueryBuilder<AnnotatedElement, Annotation> Annotations;
static final UtilQueryBuilder<Class<?>, Method> Methods;
static final UtilQueryBuilder<Class<?>, Constructor> Constructors;
static final UtilQueryBuilder<Class<?>, Field> Fields;
}Backward-compatible methods providing the same functionality as earlier versions of Reflections for easy migration and compatibility.
class Reflections {
<T> Set<Class<? extends T>> getSubTypesOf(Class<T> type);
Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation);
Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation);
Set<Constructor> getConstructorsAnnotatedWith(Class<? extends Annotation> annotation);
Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation);
Set<String> getResources(String pattern);
}Serialization capabilities for saving and loading scanned metadata in multiple formats including XML, JSON, and Java code generation.
interface Serializer {
Reflections read(InputStream inputStream);
File save(Reflections reflections, String filename);
}
class XmlSerializer implements Serializer;
class JsonSerializer implements Serializer;
class JavaCodeSerializer implements Serializer;
class Reflections {
File save(String filename);
File save(String filename, Serializer serializer);
static Reflections collect();
static Reflections collect(String packagePrefix, Predicate<String> resourceNameFilter);
}interface Configuration {
Set<Scanner> getScanners();
Set<URL> getUrls();
Predicate<String> getInputsFilter();
boolean isParallel();
ClassLoader[] getClassLoaders();
boolean shouldExpandSuperTypes();
}
class Store extends HashMap<String, Map<String, Set<String>>> {
Store();
Store(Map<String, Map<String, Set<String>>> storeMap);
}
class ReflectionsException extends RuntimeException {
ReflectionsException(String message);
ReflectionsException(String message, Throwable cause);
ReflectionsException(Throwable cause);
}
interface QueryBuilder extends NameHelper {
String index();
QueryFunction<Store, String> get(String key);
QueryFunction<Store, String> get(AnnotatedElement element);
QueryFunction<Store, String> get(Collection<String> keys);
QueryFunction<Store, String> getAll(Collection<String> keys);
QueryFunction<Store, String> of(Collection<String> keys);
QueryFunction<Store, String> of(String key);
QueryFunction<Store, String> of(AnnotatedElement... elements);
}
interface NameHelper {
String toName(AnnotatedElement element);
String toName(Class<?> type);
String toName(Constructor<?> constructor);
String toName(Method method);
String toName(Field field);
Collection<String> toNames(Collection<? extends AnnotatedElement> elements);
<T> T forName(String name, Class<T> resultType, ClassLoader... loaders);
Class<?> forClass(String typeName, ClassLoader... loaders);
Member forMember(String descriptor, ClassLoader... loaders);
Method forMethod(String descriptor, ClassLoader... loaders);
Constructor<?> forConstructor(String descriptor, ClassLoader... loaders);
Field forField(String descriptor, ClassLoader... loaders);
}