Class ReflectionsProvider

java.lang.Object
com.github.thought2code.mcp.annotated.reflect.ReflectionsProvider

public final class ReflectionsProvider extends Object
A provider class for reflection operations using the Reflections library.

This class provides static methods for initializing and accessing reflection capabilities to scan for annotated methods and fields in a specified package. It uses the Reflections library to perform runtime scanning of classpath components.

The class maintains a singleton Reflections instance that is initialized with a base package derived from the main application class or the McpServerApplication annotation. The scanning is configured to look for annotated methods and fields.

Key features:

  • Initializes reflection scanning for a specified base package
  • Supports package configuration via McpServerApplication annotation
  • Retrieves methods annotated with specific annotations
  • Retrieves fields annotated with specific annotations

This class follows the utility class pattern with a private constructor to prevent instantiation.

Author:
codeboyzhou
See Also:
  • Method Details

    • initializeReflectionsInstance

      public static void initializeReflectionsInstance(Class<?> mainClass)
      Initializes the Reflections instance with the specified main class.

      This method determines the base package for reflection scanning by examining the provided main class. The base package can be configured in three ways:

      1. Default: Uses the package name of the main class
      2. Annotation string: Uses the basePackage attribute from McpServerApplication if specified and not blank
      3. Annotation class: Uses the package name of the basePackageClass attribute from McpServerApplication if specified and not Object.class

      The Reflections instance is configured to scan for annotated methods and fields within the determined base package.

      Parameters:
      mainClass - the main application class used to determine the base package
      See Also:
    • getMethodsAnnotatedWith

      public static Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation)
      Retrieves all methods annotated with the specified annotation.

      This method uses the initialized Reflections instance to scan the configured base package and return a set of all methods that are annotated with the given annotation type.

      The method requires that initializeReflectionsInstance(Class) has been called before invoking this method.

      Parameters:
      annotation - the annotation class to search for
      Returns:
      a set of methods annotated with the specified annotation
      See Also:
      • Reflections.getMethodsAnnotatedWith(Class)
      • Method
    • getFieldsAnnotatedWith

      public static Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation)
      Retrieves all fields annotated with the specified annotation.

      This method uses the initialized Reflections instance to scan the configured base package and return a set of all fields that are annotated with the given annotation type.

      The method requires that initializeReflectionsInstance(Class) has been called before invoking this method.

      Parameters:
      annotation - the annotation class to search for
      Returns:
      a set of fields annotated with the specified annotation
      See Also:
      • Reflections.getFieldsAnnotatedWith(Class)
      • Field