Class ReflectionUtils

java.lang.Object
com.bobocode.bring.core.utils.ReflectionUtils

public final class ReflectionUtils extends Object
Utility class providing reflection-related functionalities, including methods for constructor instantiation, field manipulation, method invocation, and annotation processing.
Since:
1.0
Author:
Blyzhnytsia Team
  • Field Details

  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • isAutowiredSetterMethod

      public static boolean isAutowiredSetterMethod(Method method)
      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

      public static Object getConstructorWithOutParameters(Class<?> clazz)
      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 created
      parameterType - The type of the constructor's single parameter
      instance - 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 created
      parameterTypesToInstance - 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

      public static void setField(Field field, Object obj, Object value)
      Sets a field's value within an object.
      Parameters:
      field - The field to be modified
      obj - The object containing the field
      value - The value to be set in the field
    • getParameterNames

      public static List<String> getParameterNames(AccessibleObject methodOrConstructor)
      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

      public static int extractParameterPosition(Parameter parameter)
      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 type
      reflections - The Reflections object to query types
      createdBeanAnnotations - 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 for
      reflections - The Reflections object to query types
      createdBeanAnnotations - 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 check
      createdBeanAnnotations - List of annotations indicating created beans
      Returns:
      True if the implementation is annotated, otherwise false
    • invokeBeanMethod

      public static Supplier<Object> invokeBeanMethod(Method method, Object obj, Object[] params)
      Invokes a method on an object and returns a Supplier for its result.
      Parameters:
      method - The method to invoke
      obj - The object to invoke the method on
      params - 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 instance
      args - The arguments to pass to the constructor
      clazz - The class of the instance to be created
      proxy - 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 object
      declaredMethods - The methods of the bean to process
      annotation - The annotation to process
      Throws:
      ReflectiveOperationException - If an error occurs during reflective operations