Class ReflectionsProvider
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
McpServerApplicationannotation - 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:
-
ReflectionsMcpServerApplicationMethodField
-
Method Summary
Modifier and TypeMethodDescriptiongetFieldsAnnotatedWith(Class<? extends Annotation> annotation) Retrieves all fields annotated with the specified annotation.getMethodsAnnotatedWith(Class<? extends Annotation> annotation) Retrieves all methods annotated with the specified annotation.static voidinitializeReflectionsInstance(Class<?> mainClass) Initializes the Reflections instance with the specified main class.
-
Method Details
-
initializeReflectionsInstance
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:
- Default: Uses the package name of the main class
- Annotation string: Uses the
basePackageattribute fromMcpServerApplicationif specified and not blank - Annotation class: Uses the package name of the
basePackageClassattribute fromMcpServerApplicationif specified and notObject.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:
-
McpServerApplicationReflections
-
getMethodsAnnotatedWith
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
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
-