CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-reflections--reflections

Java runtime metadata analysis library that enables reverse querying of classpath metadata for type system introspection

Pending
Overview
Eval results
Files

legacy-api.mddocs/

Legacy API

Backward-compatible methods providing the same functionality as earlier versions of Reflections for easy migration and compatibility. These methods offer a simpler, more direct approach to querying metadata without the functional composition capabilities.

Capabilities

Type Query Methods

Legacy methods for querying type hierarchies and annotated types.

/**
 * Legacy type query methods in Reflections class
 */
class Reflections {
    /** Get all subtypes of specified type */
    <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type);
    
    /** Get types annotated with specific annotation class */
    Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation);
    
    /** Get types annotated with specific annotation class, optionally honoring @Inherited */
    Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation, boolean honorInherited);
    
    /** Get types annotated with specific annotation instance (matches annotation values) */
    Set<Class<?>> getTypesAnnotatedWith(Annotation annotation);
    
    /** Get types annotated with specific annotation instance, optionally honoring @Inherited */
    Set<Class<?>> getTypesAnnotatedWith(Annotation annotation, boolean honorInherited);
}

Method Query Methods

Legacy methods for querying methods with annotations, signatures, parameters, and return types.

/**
 * Legacy method query methods in Reflections class
 */
class Reflections {
    /** Get methods annotated with specific annotation class */
    Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation);
    
    /** Get methods annotated with specific annotation instance (matches annotation values) */
    Set<Method> getMethodsAnnotatedWith(Annotation annotation);
    
    /** Get methods with specific parameter signature */
    Set<Method> getMethodsWithSignature(Class<?>... types);
    
    /** Get methods that have parameters of specified type */
    Set<Method> getMethodsWithParameter(AnnotatedElement type);
    
    /** Get methods with specific return type */
    Set<Method> getMethodsReturn(Class<?> type);
}

Constructor Query Methods

Legacy methods for querying constructors with annotations, signatures, and parameters.

/**
 * Legacy constructor query methods in Reflections class
 */
class Reflections {
    /** Get constructors annotated with specific annotation class */
    Set<Constructor> getConstructorsAnnotatedWith(Class<? extends Annotation> annotation);
    
    /** Get constructors annotated with specific annotation instance (matches annotation values) */
    Set<Constructor> getConstructorsAnnotatedWith(Annotation annotation);
    
    /** Get constructors with specific parameter signature */
    Set<Constructor> getConstructorsWithSignature(Class<?>... types);
    
    /** Get constructors that have parameters of specified type */
    Set<Constructor> getConstructorsWithParameter(AnnotatedElement type);
}

Field Query Methods

Legacy methods for querying fields with annotations.

/**
 * Legacy field query methods in Reflections class
 */
class Reflections {
    /** Get fields annotated with specific annotation class */
    Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation);
    
    /** Get fields annotated with specific annotation instance (matches annotation values) */
    Set<Field> getFieldsAnnotatedWith(Annotation annotation);
}

Resource Query Methods

Legacy methods for querying classpath resources.

/**
 * Legacy resource query methods in Reflections class
 */
class Reflections {
    /** Get resources matching regex pattern string */
    Set<String> getResources(String pattern);
    
    /** Get resources matching compiled regex pattern */
    Set<String> getResources(Pattern pattern);
}

ReflectionUtils Legacy Methods

Legacy static methods in ReflectionUtils for direct reflection operations (marked for removal in future versions).

/**
 * Legacy methods in ReflectionUtils (marked for removal)
 */
abstract class ReflectionUtils {
    /** @Deprecated Get all annotations of type with predicates */
    @Deprecated static <T extends AnnotatedElement> Set<Annotation> getAllAnnotations(T type, Predicate<Annotation>... predicates);
    
    /** @Deprecated Get all super types with predicates */
    @Deprecated static Set<Class<?>> getAllSuperTypes(Class<?> type, Predicate<? super Class<?>>... predicates);
    
    /** @Deprecated Get super types without predicates */
    @Deprecated static Set<Class<?>> getSuperTypes(Class<?> type);
    
    /** @Deprecated Get all methods with predicates */
    @Deprecated static Set<Method> getAllMethods(Class<?> type, Predicate<? super Method>... predicates);
    
    /** @Deprecated Get methods with predicates */
    @Deprecated static Set<Method> getMethods(Class<?> type, Predicate<? super Method>... predicates);
    
    /** @Deprecated Get all constructors with predicates */
    @Deprecated static Set<Constructor> getAllConstructors(Class<?> type, Predicate<? super Constructor>... predicates);
    
    /** @Deprecated Get constructors with predicates */
    @Deprecated static Set<Constructor> getConstructors(Class<?> type, Predicate<? super Constructor>... predicates);
    
    /** @Deprecated Get all fields with predicates */
    @Deprecated static Set<Field> getAllFields(Class<?> type, Predicate<? super Field>... predicates);
    
    /** @Deprecated Get fields with predicates */
    @Deprecated static Set<Field> getFields(Class<?> type, Predicate<? super Field>... predicates);
    
    /** @Deprecated Get annotations with predicates */
    @Deprecated static <T extends AnnotatedElement> Set<Annotation> getAnnotations(T type, Predicate<Annotation>... predicates);
}

Usage Examples

Basic Type Queries

import org.reflections.Reflections;
import java.util.Set;

Reflections reflections = new Reflections("com.mycompany");

// Get subtypes
Set<Class<? extends Service>> services = reflections.getSubTypesOf(Service.class);
Set<Class<? extends Repository>> repositories = reflections.getSubTypesOf(Repository.class);
Set<Class<? extends Controller>> controllers = reflections.getSubTypesOf(Controller.class);

// Get annotated types
Set<Class<?>> entities = reflections.getTypesAnnotatedWith(Entity.class);
Set<Class<?>> components = reflections.getTypesAnnotatedWith(Component.class);
Set<Class<?>> configurations = reflections.getTypesAnnotatedWith(Configuration.class);

// Honor inheritance for annotations
Set<Class<?>> inheritedServices = reflections.getTypesAnnotatedWith(Service.class, true);
Set<Class<?>> nonInheritedServices = reflections.getTypesAnnotatedWith(Service.class, false);

Annotation Value Matching

// Match specific annotation values
@RequestMapping(value = "/api", method = RequestMethod.GET)
class ApiController { }

// Create annotation instance for matching
RequestMapping requestMappingAnnotation = ApiController.class.getAnnotation(RequestMapping.class);

// Find types with matching annotation values
Set<Class<?>> apiControllers = reflections.getTypesAnnotatedWith(requestMappingAnnotation);

// This will only return classes with @RequestMapping(value = "/api", method = RequestMethod.GET)

Method Queries

// Get methods by annotation
Set<Method> requestMethods = reflections.getMethodsAnnotatedWith(RequestMapping.class);
Set<Method> autowiredMethods = reflections.getMethodsAnnotatedWith(Autowired.class);
Set<Method> transactionalMethods = reflections.getMethodsAnnotatedWith(Transactional.class);

// Get methods by signature
Set<Method> stringMethods = reflections.getMethodsWithSignature(String.class);
Set<Method> stringIntMethods = reflections.getMethodsWithSignature(String.class, int.class);
Set<Method> noParamMethods = reflections.getMethodsWithSignature();

// Get methods by parameter type
Set<Method> methodsWithStringParam = reflections.getMethodsWithParameter(String.class);
Set<Method> methodsWithUserParam = reflections.getMethodsWithParameter(User.class);

// Get methods by return type
Set<Method> voidMethods = reflections.getMethodsReturn(void.class);
Set<Method> stringReturningMethods = reflections.getMethodsReturn(String.class);
Set<Method> userReturningMethods = reflections.getMethodsReturn(User.class);

Constructor Queries

// Get constructors by annotation
Set<Constructor> injectConstructors = reflections.getConstructorsAnnotatedWith(Inject.class);
Set<Constructor> autowiredConstructors = reflections.getConstructorsAnnotatedWith(Autowired.class);

// Get constructors by signature
Set<Constructor> defaultConstructors = reflections.getConstructorsWithSignature();
Set<Constructor> stringConstructors = reflections.getConstructorsWithSignature(String.class);
Set<Constructor> stringIntConstructors = reflections.getConstructorsWithSignature(String.class, int.class);

// Get constructors by parameter type
Set<Constructor> constructorsWithStringParam = reflections.getConstructorsWithParameter(String.class);
Set<Constructor> constructorsWithUserParam = reflections.getConstructorsWithParameter(User.class);

Field Queries

// Get fields by annotation
Set<Field> autowiredFields = reflections.getFieldsAnnotatedWith(Autowired.class);
Set<Field> valueFields = reflections.getFieldsAnnotatedWith(Value.class);
Set<Field> resourceFields = reflections.getFieldsAnnotatedWith(Resource.class);

// Match specific field annotation values
@Value("${app.version}")
private String version;

Value valueAnnotation = MyClass.class.getDeclaredField("version").getAnnotation(Value.class);
Set<Field> appVersionFields = reflections.getFieldsAnnotatedWith(valueAnnotation);

Resource Queries

import java.util.regex.Pattern;

// Get resources by string pattern
Set<String> xmlFiles = reflections.getResources(".*\\.xml");
Set<String> propertyFiles = reflections.getResources(".*\\.properties");
Set<String> jsonFiles = reflections.getResources(".*\\.json");
Set<String> configFiles = reflections.getResources(".*config.*");

// Get resources by compiled pattern
Pattern xmlPattern = Pattern.compile(".*\\.xml");
Set<String> xmlResources = reflections.getResources(xmlPattern);

Pattern configPattern = Pattern.compile(".*(config|configuration).*", Pattern.CASE_INSENSITIVE);
Set<String> configResources = reflections.getResources(configPattern);

Legacy ReflectionUtils Usage

import org.reflections.ReflectionUtils;
import static org.reflections.ReflectionUtils.*;

// Get super types (deprecated but still available)
Set<Class<?>> superTypes = ReflectionUtils.getAllSuperTypes(MyClass.class);
Set<Class<?>> filteredSuperTypes = ReflectionUtils.getAllSuperTypes(MyClass.class, 
    clazz -> !clazz.equals(Object.class));

// Get methods (deprecated but still available)
Set<Method> allMethods = ReflectionUtils.getAllMethods(MyClass.class);
Set<Method> publicMethods = ReflectionUtils.getAllMethods(MyClass.class, 
    withPublic());
Set<Method> getters = ReflectionUtils.getAllMethods(MyClass.class, 
    withPrefix("get"), withParametersCount(0));

// Get fields (deprecated but still available)
Set<Field> allFields = ReflectionUtils.getAllFields(MyClass.class);
Set<Field> privateFields = ReflectionUtils.getAllFields(MyClass.class, 
    field -> Modifier.isPrivate(field.getModifiers()));

// Get constructors (deprecated but still available)
Set<Constructor> allConstructors = ReflectionUtils.getAllConstructors(MyClass.class);
Set<Constructor> publicConstructors = ReflectionUtils.getAllConstructors(MyClass.class, 
    withPublic());

Migration from Legacy to Modern API

// Legacy approach
Set<Class<? extends Service>> legacyServices = reflections.getSubTypesOf(Service.class);
Set<Method> legacyMethods = reflections.getMethodsAnnotatedWith(RequestMapping.class);

// Modern functional approach (equivalent)
Set<Class<?>> modernServices = reflections.get(SubTypes.of(Service.class).asClass());
Set<Method> modernMethods = reflections.get(MethodsAnnotated.with(RequestMapping.class).as(Method.class));

// Legacy with additional processing
Set<Class<? extends Service>> filteredLegacyServices = legacyServices.stream()
    .filter(clazz -> !Modifier.isAbstract(clazz.getModifiers()))
    .collect(Collectors.toSet());

// Modern with built-in filtering
Set<Class<?>> filteredModernServices = reflections.get(
    SubTypes.of(Service.class)
        .asClass()
        .filter(clazz -> !Modifier.isAbstract(clazz.getModifiers())));

Combining Legacy and Modern APIs

// You can mix legacy and modern approaches
Reflections reflections = new Reflections("com.mycompany");

// Use legacy for simple queries
Set<Class<? extends Service>> services = reflections.getSubTypesOf(Service.class);

// Use modern for complex queries
Set<Method> complexMethods = reflections.get(
    MethodsAnnotated.with(RequestMapping.class)
        .as(Method.class)
        .filter(method -> method.getParameterCount() > 0)
        .filter(method -> !method.getName().startsWith("get")));

// Combine results
Set<String> allServiceMethods = services.stream()
    .flatMap(clazz -> Arrays.stream(clazz.getDeclaredMethods()))
    .map(Method::getName)
    .collect(Collectors.toSet());

Error Handling in Legacy API

// Legacy methods may return empty sets but won't throw exceptions for missing types
Set<Class<? extends NonExistentInterface>> empty = reflections.getSubTypesOf(NonExistentInterface.class);
// empty.isEmpty() will be true

// Handle annotation classes that might not be present
try {
    Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) 
        Class.forName("com.example.MyAnnotation");
    Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(annotationClass);
} catch (ClassNotFoundException e) {
    // Handle missing annotation class
    Set<Class<?>> annotated = Collections.emptySet();
}

// Resource queries with invalid patterns
try {
    Set<String> resources = reflections.getResources("[invalid regex");
} catch (PatternSyntaxException e) {
    // Handle invalid regex pattern
    Set<String> resources = Collections.emptySet();
}

Legacy API Best Practices

When to Use Legacy API

  • Simple queries: When you need straightforward results without filtering or transformation
  • Migration: When upgrading existing code that uses older Reflections versions
  • Compatibility: When working with codebases that rely on the legacy API patterns
  • Learning curve: When team members are more familiar with the direct method approach

When to Use Modern API

  • Complex queries: When you need filtering, mapping, or composition of results
  • Performance: When you want to chain operations without intermediate collections
  • Type safety: When you want better compile-time type checking
  • Functional style: When working with modern functional programming patterns

Migration Strategy

  1. Start with legacy methods for basic functionality
  2. Gradually introduce modern API for new complex queries
  3. Refactor legacy calls to modern API when adding new filtering or processing
  4. Use both approaches in the same application during transition period

Types

/**
 * Common predicate utilities for legacy API filtering
 */
abstract class ReflectionUtilsPredicates {
    /** Filter for public members */
    static Predicate<Member> withPublic();
    
    /** Filter by parameter count */
    static Predicate<Member> withParametersCount(int count);
    
    /** Filter by method name prefix */
    static <T extends Member> Predicate<T> withPrefix(String prefix);
    
    /** Filter by annotation presence */
    static <T extends AnnotatedElement> Predicate<T> withAnnotation(Class<? extends Annotation> annotation);
    
    /** Filter by method parameters */
    static Predicate<Method> withParameters(Class<?>... types);
    
    /** Filter by method return type */
    static Predicate<Method> withReturnType(Class<?> type);
}

Install with Tessl CLI

npx tessl i tessl/maven-org-reflections--reflections

docs

configuration.md

core-operations.md

functional-queries.md

index.md

legacy-api.md

scanners.md

serialization.md

utilities.md

tile.json