Package com.bobocode.bring.core.utils
Class ReflectionUtils
java.lang.Object
com.bobocode.bring.core.utils.ReflectionUtils
Utility class providing reflection-related functionalities, including methods for constructor instantiation,
field manipulation, method invocation, and annotation processing.
- Since:
- 1.0
- Author:
- Blyzhnytsia Team
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate static class
Inner class extending AnnotationParanamer to handle Qualifier annotations. -
Field Summary
Modifier and TypeFieldDescriptionprivate static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final com.thoughtworks.paranamer.Paranamer
static final OrderComparator
private static final String
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptioncreateNewInstance
(Constructor<?> constructor, Object[] args, Class<?> clazz, boolean proxy) Creates a new instance using the given constructor and arguments and returns a Supplier for it.extractImplClasses
(Class<?> type, org.reflections.Reflections reflections, List<Class<? extends Annotation>> createdBeanAnnotations) Extracts implementation classes of a given type.extractImplClasses
(ParameterizedType genericType, org.reflections.Reflections reflections, List<Class<? extends Annotation>> createdBeanAnnotations) Extracts implementation classes of a generic type.static int
extractParameterPosition
(Parameter parameter) Extracts the position of a parameter.static Object
getConstructorWithOneParameter
(Class<?> clazz, Class<?> parameterType, Object instance) Retrieves the instance of a class by invoking a constructor with a single parameter.static Object
getConstructorWithOutParameters
(Class<?> clazz) Retrieves the instance of a class by invoking the default (parameterless) constructor.static Object
getConstructorWithParameters
(Class<?> clazz, Map<Class<?>, Object> parameterTypesToInstance) Retrieves the instance of a class by invoking a constructor with multiple parameters.getParameterNames
(AccessibleObject methodOrConstructor) Retrieves parameter names of a method or constructor.invokeBeanMethod
(Method method, Object obj, Object[] params) Invokes a method on an object and returns a Supplier for its result.static boolean
isAutowiredSetterMethod
(Method method) Checks if the given method is an autowired setter method.private static boolean
isImplementationAnnotated
(Class<?> implementation, List<Class<? extends Annotation>> createdBeanAnnotations) Checks if an implementation class is annotated with any of the specified annotations.static void
processBeanPostProcessorAnnotation
(Object bean, Method[] declaredMethods, Class<? extends Annotation> annotation) Processes the annotations on the methods of a bean.static void
Sets a field's value within an object.
-
Field Details
-
BEAN_SHOULD_HAVE_MESSAGE
- See Also:
-
BEAN_SHOULD_HAVE_DEFAULT_CONSTRUCTOR_MESSAGE
- See Also:
-
BEAN_SHOULD_HAVE_CONSTRUCTOR_WITH_ONE_PARAMETER_MESSAGE
- See Also:
-
BEAN_SHOULD_HAVE_CONSTRUCTOR_WITH_PARAMETERS_MESSAGE
- See Also:
-
DELIMITER
- See Also:
-
ORDER_COMPARATOR
-
info
private static final com.thoughtworks.paranamer.Paranamer info -
ARG
- See Also:
-
SET_METHOD_START_PREFIX
- See Also:
-
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
isAutowiredSetterMethod
Checks if the given method is an autowired setter method.- Parameters:
method
- The method to be checked- Returns:
- True if the method is an autowired setter method, otherwise false
-
getConstructorWithOutParameters
Retrieves the instance of a class by invoking the default (parameterless) constructor.- Parameters:
clazz
- The class for which an instance should be created- Returns:
- An instance of the specified class
- Throws:
BeanPostProcessorConstructionLimitationException
- If the default constructor is not present or accessible
-
getConstructorWithOneParameter
public static Object getConstructorWithOneParameter(Class<?> clazz, Class<?> parameterType, Object instance) Retrieves the instance of a class by invoking a constructor with a single parameter.- Parameters:
clazz
- The class for which an instance should be createdparameterType
- The type of the constructor's single parameterinstance
- The instance to be passed as the constructor's argument- Returns:
- An instance of the specified class created using the provided parameter
- Throws:
BeanPostProcessorConstructionLimitationException
- If the constructor with a single parameter is not present or accessible
-
getConstructorWithParameters
public static Object getConstructorWithParameters(Class<?> clazz, Map<Class<?>, Object> parameterTypesToInstance) Retrieves the instance of a class by invoking a constructor with multiple parameters.- Parameters:
clazz
- The class for which an instance should be createdparameterTypesToInstance
- A map containing parameter types and their corresponding instances- Returns:
- An instance of the specified class created using the provided parameters
- Throws:
BeanPostProcessorConstructionLimitationException
- If the constructor with specified parameters is not present or accessible
-
setField
Sets a field's value within an object.- Parameters:
field
- The field to be modifiedobj
- The object containing the fieldvalue
- The value to be set in the field
-
getParameterNames
Retrieves parameter names of a method or constructor.- Parameters:
methodOrConstructor
- The method or constructor to retrieve parameter names from- Returns:
- A list of parameter names
-
extractParameterPosition
Extracts the position of a parameter.- Parameters:
parameter
- The parameter to extract the position from- Returns:
- The position of the parameter
-
extractImplClasses
public static List<Class<?>> extractImplClasses(ParameterizedType genericType, org.reflections.Reflections reflections, List<Class<? extends Annotation>> createdBeanAnnotations) Extracts implementation classes of a generic type.- Parameters:
genericType
- The generic typereflections
- The Reflections object to query typescreatedBeanAnnotations
- List of annotations indicating created beans- Returns:
- A list of implementation classes
-
extractImplClasses
public static List<Class<?>> extractImplClasses(Class<?> type, org.reflections.Reflections reflections, List<Class<? extends Annotation>> createdBeanAnnotations) Extracts implementation classes of a given type.- Parameters:
type
- The type to extract implementations forreflections
- The Reflections object to query typescreatedBeanAnnotations
- List of annotations indicating created beans- Returns:
- A list of implementation classes
-
isImplementationAnnotated
private static boolean isImplementationAnnotated(Class<?> implementation, List<Class<? extends Annotation>> createdBeanAnnotations) Checks if an implementation class is annotated with any of the specified annotations.- Parameters:
implementation
- The implementation class to checkcreatedBeanAnnotations
- List of annotations indicating created beans- Returns:
- True if the implementation is annotated, otherwise false
-
invokeBeanMethod
Invokes a method on an object and returns a Supplier for its result.- Parameters:
method
- The method to invokeobj
- The object to invoke the method onparams
- The parameters to pass to the method- Returns:
- A Supplier representing the method invocation
-
createNewInstance
public static Supplier<Object> createNewInstance(Constructor<?> constructor, Object[] args, Class<?> clazz, boolean proxy) Creates a new instance using the given constructor and arguments and returns a Supplier for it.- Parameters:
constructor
- The constructor to create the instanceargs
- The arguments to pass to the constructorclazz
- The class of the instance to be createdproxy
- Boolean flag indicating whether to use proxy creation- Returns:
- A Supplier representing the new instance creation
-
processBeanPostProcessorAnnotation
public static void processBeanPostProcessorAnnotation(Object bean, Method[] declaredMethods, Class<? extends Annotation> annotation) throws ReflectiveOperationException Processes the annotations on the methods of a bean.- Parameters:
bean
- The bean objectdeclaredMethods
- The methods of the bean to processannotation
- The annotation to process- Throws:
ReflectiveOperationException
- If an error occurs during reflective operations
-