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

scanners.mddocs/

Scanners

Comprehensive scanning capabilities for different types of metadata including types, annotations, methods, constructors, fields, resources, and signatures. The scanner system is the foundation of Reflections' metadata indexing.

Capabilities

Scanner Interface

Base contract for all metadata scanners that defines how to scan class files and other resources.

/**
 * Base contract for metadata scanners
 */
interface Scanner {
    /** Scan a class file and return key-value entries for indexing */
    List<Map.Entry<String, String>> scan(ClassFile classFile);
    
    /** Scan a VFS file (optional, returns null by default) */
    @Nullable List<Map.Entry<String, String>> scan(Vfs.File file);
    
    /** Get unique scanner index name (defaults to class simple name) */
    default String index();
    
    /** Check if scanner accepts a file for scanning (defaults to .class files) */
    default boolean acceptsInput(String file);
    
    /** Create a key-value entry */
    default Map.Entry<String, String> entry(String key, String value);
    
    /** Create multiple entries from keys collection to single value */
    default List<Map.Entry<String, String>> entries(Collection<String> keys, String value);
    
    /** Create multiple entries from single key to values collection */
    default List<Map.Entry<String, String>> entries(String key, Collection<String> values);
}

Built-in Scanners Enum

Comprehensive set of built-in scanner implementations covering all major metadata types.

/**
 * Built-in scanner implementations
 */
enum Scanners implements Scanner, QueryBuilder, NameHelper {
    /** Scan type superclasses and interfaces (excludes Object by default) */
    SubTypes,
    
    /** Scan type annotations */
    TypesAnnotated,
    
    /** Scan method annotations */
    MethodsAnnotated,
    
    /** Scan constructor annotations */
    ConstructorsAnnotated,
    
    /** Scan field annotations */
    FieldsAnnotated,
    
    /** Scan non-.class files such as xml or properties files */
    Resources,
    
    /** Scan method parameters types and annotations */
    MethodsParameter,
    
    /** Scan constructor parameters types and annotations */
    ConstructorsParameter,
    
    /** Scan method signatures (parameter types) */
    MethodsSignature,
    
    /** Scan constructor signatures (parameter types) */
    ConstructorsSignature,
    
    /** Scan method return types */
    MethodsReturn;
    
    /** Get scanner index name */
    String index();
    
    /** Filter scanner results by predicate */
    Scanners filterResultsBy(Predicate<String> filter);
}

Individual Scanner Classes

Concrete scanner implementations that can be used individually or customized.

/**
 * Abstract base scanner providing common functionality
 */
abstract class AbstractScanner implements Scanner {
    // Base implementation details
}

/**
 * Scan field annotations
 */
class FieldAnnotationsScanner extends AbstractScanner {
    // Implementation for scanning field annotations
}

/**
 * Scan member usage in code
 */
class MemberUsageScanner extends AbstractScanner {
    // Implementation for finding where methods/constructors/fields are used
}

/**
 * Scan method annotations
 */
class MethodAnnotationsScanner extends AbstractScanner {
    // Implementation for scanning method annotations
}

/**
 * Scan method parameter names using debug information
 */
class MethodParameterNamesScanner extends AbstractScanner {
    // Implementation for extracting parameter names from bytecode
}

/**
 * Scan method parameters types and annotations
 */
class MethodParameterScanner extends AbstractScanner {
    // Implementation for scanning method parameter information
}

/**
 * Scan non-class resources
 */
class ResourcesScanner extends AbstractScanner {
    // Implementation for scanning classpath resources
}

/**
 * Scan class inheritance hierarchies
 */
class SubTypesScanner extends AbstractScanner {
    // Implementation for scanning superclasses and interfaces
}

/**
 * Scan type annotations
 */
class TypeAnnotationsScanner extends AbstractScanner {
    // Implementation for scanning class/interface annotations
}

/**
 * Scan type elements (methods, fields, constructors)
 */
class TypeElementsScanner extends AbstractScanner {
    // Implementation for scanning all type members
}

Usage Examples

Using Built-in Scanners

import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import static org.reflections.scanners.Scanners.*;

// Use specific scanners
Reflections reflections = new Reflections(new ConfigurationBuilder()
    .forPackages("com.mycompany")
    .addScanners(SubTypes, TypesAnnotated, MethodsAnnotated));

// Use all available scanners
Reflections reflections = new Reflections(new ConfigurationBuilder()
    .forPackages("com.mycompany")
    .addScanners(Scanners.values()));

// Filter scanner results
SubTypes.filterResultsBy(name -> !name.startsWith("java."));

Scanner-Specific Queries

// SubTypes scanner
Set<String> subtypeNames = reflections.get(SubTypes.of(MyInterface.class));
Set<Class<?>> subtypeClasses = reflections.get(SubTypes.of(MyInterface.class).asClass());

// TypesAnnotated scanner
Set<String> annotatedTypeNames = reflections.get(TypesAnnotated.with(MyAnnotation.class));
Set<Class<?>> annotatedTypes = reflections.get(TypesAnnotated.with(MyAnnotation.class).asClass());

// MethodsAnnotated scanner
Set<String> methodNames = reflections.get(MethodsAnnotated.with(RequestMapping.class));
Set<Method> methods = reflections.get(MethodsAnnotated.with(RequestMapping.class).as(Method.class));

// Resources scanner with pattern matching
Set<String> xmlFiles = reflections.get(Resources.with(".*\\.xml"));
Set<String> propFiles = reflections.get(Resources.with(".*\\.properties"));

// Method signatures
Set<String> methodsWithIntParam = reflections.get(MethodsSignature.with(int.class));
Set<Method> methods = reflections.get(MethodsSignature.with(String.class, int.class).as(Method.class));

// Method return types
Set<String> voidMethods = reflections.get(MethodsReturn.with(void.class));
Set<Method> stringReturning = reflections.get(MethodsReturn.with(String.class).as(Method.class));

Custom Scanner Implementation

import org.reflections.scanners.AbstractScanner;
import javassist.bytecode.ClassFile;

public class CustomScanner extends AbstractScanner {
    @Override
    public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
        // Custom scanning logic
        if (meetsCriteria(classFile)) {
            entries.add(entry(extractKey(classFile), classFile.getName()));
        }
    }
    
    private boolean meetsCriteria(ClassFile classFile) {
        // Custom criteria logic
        return true;
    }
    
    private String extractKey(ClassFile classFile) {
        // Extract meaningful key for indexing
        return classFile.getName();
    }
}

// Use custom scanner
Reflections reflections = new Reflections(new ConfigurationBuilder()
    .forPackages("com.mycompany")
    .addScanners(new CustomScanner()));

Scanner Result Filtering

// Filter SubTypes to exclude system classes
SubTypes.filterResultsBy(name -> !name.startsWith("java.") && !name.startsWith("javax."));

// Filter TypesAnnotated to include only specific patterns
TypesAnnotated.filterResultsBy(name -> name.contains("Service") || name.contains("Controller"));

// Chain filters
Resources.filterResultsBy(name -> name.endsWith(".xml") || name.endsWith(".yml"));

Working with Scanner Indexes

// Get all data for a specific scanner
Set<String> allSubTypes = reflections.getAll(SubTypes);
Set<String> allAnnotatedTypes = reflections.getAll(TypesAnnotated);

// Access raw store data by scanner index
Store store = reflections.getStore();
Map<String, Set<String>> subTypesData = store.get(SubTypes.index());
Map<String, Set<String>> resourcesData = store.get(Resources.index());

Scanner Categories

Type Hierarchy Scanners

  • SubTypes: Scans superclasses and interfaces, building inheritance hierarchies
  • TypesAnnotated: Scans class-level annotations including inherited annotations

Member Annotation Scanners

  • MethodsAnnotated: Scans method-level annotations
  • ConstructorsAnnotated: Scans constructor-level annotations
  • FieldsAnnotated: Scans field-level annotations

Signature and Parameter Scanners

  • MethodsSignature: Scans method parameter type signatures
  • ConstructorsSignature: Scans constructor parameter type signatures
  • MethodsParameter: Scans method parameter types and annotations
  • ConstructorsParameter: Scans constructor parameter types and annotations
  • MethodsReturn: Scans method return types

Resource and Usage Scanners

  • Resources: Scans non-class files (XML, properties, etc.)
  • MemberUsageScanner: Finds code locations where members are used
  • MethodParameterNamesScanner: Extracts parameter names from debug info

Types

/**
 * Filter builder for scanner result filtering
 */
class FilterBuilder implements Predicate<String> {
    /** Include packages matching prefix */
    FilterBuilder includePackage(String value);
    
    /** Exclude packages matching prefix */
    FilterBuilder excludePackage(String value);
    
    /** Include names matching regex pattern */
    FilterBuilder includePattern(String regex);
    
    /** Exclude names matching regex pattern */
    FilterBuilder excludePattern(String regex);
    
    /** Add custom predicate filter */
    FilterBuilder add(Predicate<String> filter);
    
    /** Test string against all configured filters */
    boolean test(String input);
}

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