Class ReflectionHelper

java.lang.Object
net.luis.utils.util.unsafe.reflection.ReflectionHelper

public final class ReflectionHelper extends Object
Helper class for reflection-related operations.
Provides methods to:
  • check if a class has a constructor, method or field
  • get constructors, methods and fields
  • invoke constructors, methods and fields
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final org.apache.logging.log4j.Logger
    The logger for this class.
    Used for logging errors and warnings.
    private static final String
    Constant for the system property 'reflection.exceptions.log'.
    private static final String
    Constant for the system property 'reflection.exceptions.throw'.
    If this property is true, exceptions will be thrown when an error occurs.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation.
    This is a static helper class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static @NotNull Optional<Object>
    get(@NotNull Class<?> clazz, @NotNull String name, @Nullable Object instance)
    Gets the value from the field with the given name from the given class and instance.
    static @NotNull Optional<Object>
    get(@NotNull Field field, @Nullable Object instance)
    Gets the value of the given field from the given instance.
    Exceptions will not be logged or thrown by default.
    static @Nullable Class<?>
    getClassForName(@NotNull String className)
    Gets the class for the given name.
    static <T> @NotNull Optional<Constructor<T>>
    getConstructor(@NotNull Class<T> clazz, Class<?> @Nullable ... parameters)
    Gets the constructor from the given class with the given parameters.
    Exceptions will not be logged or thrown by default.
    static @NotNull Optional<Field>
    getField(@NotNull Class<?> clazz, @NotNull String name)
    Gets the field from the given class with the given name.
    Exceptions will not be logged or thrown by default.
    static @NotNull Optional<Method>
    getMethod(@NotNull Class<?> clazz, @NotNull String name, Class<?> @Nullable ... parameters)
    Gets the method from the given class with the given name and parameters.
    Exceptions will not be logged or thrown by default.
    private static @NotNull List<String>
    getSimpleNames(Class<?> @Nullable ... classes)
    Maps the given classes to their simple names.
    If the given array is null or empty, an empty list will be returned.
    private static @NotNull List<String>
    getSimpleNames(Object @Nullable ... objects)
    Maps the given objects to their simple names by converting them to their classes first.
    If the given array is null or empty, an empty list will be returned.
    private static void
    Handles the given exception.
    If the system property 'reflection.exceptions.log' is true, the exception will be logged.
    If the system property 'reflection.exceptions.throw' is true, the exception will be thrown.
    static boolean
    hasConstructor(@NotNull Class<?> clazz, Class<?> @Nullable ... parameters)
    Check if the given class has a constructor with the given parameters.
    Exceptions which occur during reflection will be ignored.
    static <T> boolean
    hasConstructor(@NotNull Class<T> clazz, @Nullable Predicate<Constructor<T>> predicate, Class<?> @Nullable ... parameters)
    Check if the given class has a constructor with the given parameters.
    Exceptions which occur during reflection will be ignored.
    static boolean
    hasField(@NotNull Class<?> clazz, @NotNull String name)
    Check if the given class has a field with the given name.
    Exceptions which occur during reflection will be ignored.
    static boolean
    hasField(@NotNull Class<?> clazz, @NotNull String name, @Nullable Predicate<Field> predicate)
    Check if the given class has a field with the given name.
    Exceptions which occur during reflection will be ignored.
    static boolean
    hasInterface(@NotNull Class<?> clazz, @NotNull Class<?> iface)
    Check if the given class is an instance of the given interface.
    static boolean
    hasMethod(@NotNull Class<?> clazz, @NotNull String name, Class<?> @Nullable ... parameters)
    Check if the given class has a method with the given name and parameters.
    Exceptions which occur during reflection will be ignored.
    static boolean
    hasMethod(@NotNull Class<?> clazz, @NotNull String name, @Nullable Predicate<Method> predicate, Class<?> @Nullable ... parameters)
    Check if the given class has a method with the given name and parameters.
    Exceptions which occur during reflection will be ignored.
    static @NotNull Optional<Object>
    invoke(@NotNull Class<?> clazz, @NotNull String name, @Nullable Object instance, Object @Nullable ... parameters)
    Invokes the method with the given name and parameters on the given instance.
    static @NotNull Optional<Object>
    invoke(@NotNull Method method, @Nullable Object instance, Object @Nullable ... parameters)
    Invokes the given method with the given parameters on the given instance.
    static <T> @NotNull Optional<T>
    newInstance(@NotNull Class<T> clazz, Object @Nullable ... parameters)
    Creates a new instance from the given class and parameters.
    static <T> @NotNull Optional<T>
    newInstance(@NotNull Constructor<T> constructor, Object @Nullable ... parameters)
    Creates a new instance from the given constructor and parameters.
    Exceptions will not be logged or thrown by default.
    static void
    set(@NotNull Class<?> clazz, @NotNull String name, @Nullable Object instance, @Nullable Object value)
    Sets the value of the field with the given name in the given class and instance to the given value.
    static void
    set(@NotNull Field field, @Nullable Object instance, @Nullable Object value)
    Sets the value of the given field in the given instance to the given value.
    Exceptions will not be logged or thrown by default.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOGGER

      private static final org.apache.logging.log4j.Logger LOGGER
      The logger for this class.
      Used for logging errors and warnings.
    • REFLECTION_EXCEPTIONS_THROW

      private static final String REFLECTION_EXCEPTIONS_THROW
      Constant for the system property 'reflection.exceptions.throw'.
      If this property is true, exceptions will be thrown when an error occurs.
      See Also:
    • REFLECTION_EXCEPTIONS_LOG

      private static final String REFLECTION_EXCEPTIONS_LOG
      Constant for the system property 'reflection.exceptions.log'.

      If this property is true, exceptions will be logged when an error occurs.
      If the property is false, the logging is not completely disabled
      only exceptions will not be logged.

      See Also:
  • Constructor Details

    • ReflectionHelper

      private ReflectionHelper()
      Private constructor to prevent instantiation.
      This is a static helper class.
  • Method Details

    • getClassForName

      @Nullable public static @Nullable Class<?> getClassForName(@NotNull @NotNull String className)
      Gets the class for the given name.
      Parameters:
      className - The name of the class
      Returns:
      The class for the given name or null if the class could not be found
      Throws:
      NullPointerException - If the given class name is null
      RuntimeException - If the class could not be found and throwing exceptions is enabled
    • hasInterface

      public static boolean hasInterface(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull Class<?> iface)
      Check if the given class is an instance of the given interface.
      Parameters:
      clazz - The class to check
      iface - The interface to check
      Returns:
      True if the given class is an instance of the given interface, otherwise false
      Throws:
      NullPointerException - If the given class or interface is null
    • getConstructor

      @NotNull public static <T> @NotNull Optional<Constructor<T>> getConstructor(@NotNull @NotNull Class<T> clazz, Class<?> @Nullable ... parameters)
      Gets the constructor from the given class with the given parameters.
      Exceptions will not be logged or thrown by default.
      Type Parameters:
      T - The type of the class
      Parameters:
      clazz - The class to get the constructor from
      parameters - The parameters of the constructor
      Returns:
      The constructor as an optional or an empty optional if the constructor could not be found
      Throws:
      NullPointerException - If the given class is null
      See Also:
    • hasConstructor

      public static boolean hasConstructor(@NotNull @NotNull Class<?> clazz, Class<?> @Nullable ... parameters)
      Check if the given class has a constructor with the given parameters.
      Exceptions which occur during reflection will be ignored.
      Parameters:
      clazz - The class to check
      parameters - The parameters of the constructor
      Returns:
      True if the given class has a constructor with the given parameters, otherwise false
      Throws:
      NullPointerException - If the given class is null
      See Also:
    • hasConstructor

      public static <T> boolean hasConstructor(@NotNull @NotNull Class<T> clazz, @Nullable @Nullable Predicate<Constructor<T>> predicate, Class<?> @Nullable ... parameters)
      Check if the given class has a constructor with the given parameters.
      Exceptions which occur during reflection will be ignored.
      Type Parameters:
      T - The type of the class
      Parameters:
      clazz - The class to check
      predicate - A predicate with an additional condition the constructor must meet
      parameters - The parameters of the constructor
      Returns:
      True if the given class has a constructor with the given parameters, otherwise false
      Throws:
      NullPointerException - If the given class is null
    • newInstance

      @NotNull public static <T> @NotNull Optional<T> newInstance(@NotNull @NotNull Constructor<T> constructor, Object @Nullable ... parameters)
      Creates a new instance from the given constructor and parameters.
      Exceptions will not be logged or thrown by default.
      Type Parameters:
      T - The type of the instance
      Parameters:
      constructor - The constructor to create the instance from
      parameters - The parameters of the constructor
      Returns:
      The new instance as an optional or an empty optional if the instance could not be created
      Throws:
      NullPointerException - If the given constructor is null
      See Also:
    • newInstance

      @NotNull public static <T> @NotNull Optional<T> newInstance(@NotNull @NotNull Class<T> clazz, Object @Nullable ... parameters)
      Creates a new instance from the given class and parameters.
      Type Parameters:
      T - The type of the instance
      Parameters:
      clazz - The class to create the instance from
      parameters - The parameters of the constructor
      Returns:
      The new instance as an optional or an empty optional if the instance could not be created
      Throws:
      NullPointerException - If the given class is null
      IllegalStateException - If no constructor for the given parameters could be found
      See Also:
    • getMethod

      @NotNull public static @NotNull Optional<Method> getMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, Class<?> @Nullable ... parameters)
      Gets the method from the given class with the given name and parameters.
      Exceptions will not be logged or thrown by default.
      Parameters:
      clazz - The class to get the method from
      name - The name of the method
      parameters - The parameters of the method
      Returns:
      The method as an optional or an empty optional if the method could not be found
      Throws:
      NullPointerException - If the given class or name is null
      See Also:
    • hasMethod

      public static boolean hasMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, Class<?> @Nullable ... parameters)
      Check if the given class has a method with the given name and parameters.
      Exceptions which occur during reflection will be ignored.
      Parameters:
      clazz - The class to check for the method
      name - The name of the method
      parameters - The parameters of the method
      Returns:
      True if the given class has a method with the given name and parameters, otherwise false
      Throws:
      NullPointerException - If the given class or name is null
    • hasMethod

      public static boolean hasMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Predicate<Method> predicate, Class<?> @Nullable ... parameters)
      Check if the given class has a method with the given name and parameters.
      Exceptions which occur during reflection will be ignored.
      Parameters:
      clazz - The class to check for the method
      name - The name of the method
      predicate - A predicate with an additional condition the method must meet
      parameters - The parameters of the method
      Returns:
      True if the given class has a method with the given name and parameters, otherwise false
      Throws:
      NullPointerException - If the given class or name is null
    • invoke

      @NotNull public static @NotNull Optional<Object> invoke(@NotNull @NotNull Method method, @Nullable @Nullable Object instance, Object @Nullable ... parameters)
      Invokes the given method with the given parameters on the given instance.

      If the invocation is successful, the return value will be returned.
      If the invocation fails, the return value will be null.

      Exceptions will not be logged or thrown by default.

      Parameters:
      method - The method to invoke
      instance - The instance to invoke the method on
      parameters - The parameters of the method
      Returns:
      The return value of the method as an optional or an empty optional if the invocation failed
      Throws:
      NullPointerException - If the given method is null
      See Also:
    • invoke

      @NotNull public static @NotNull Optional<Object> invoke(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Object instance, Object @Nullable ... parameters)
      Invokes the method with the given name and parameters on the given instance.

      If the invocation is successful, the return value will be returned.
      If the invocation fails, the return value will be null.

      Parameters:
      clazz - The class which contains the method
      name - The name of the method
      instance - The instance to invoke the method on
      parameters - The parameters of the method
      Returns:
      The return value of the method as an optional or an empty optional if the invocation failed
      Throws:
      NullPointerException - If the given class or name is null
      IllegalStateException - If no method for the given name and parameters could be found
      See Also:
    • getField

      @NotNull public static @NotNull Optional<Field> getField(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name)
      Gets the field from the given class with the given name.
      Exceptions will not be logged or thrown by default.
      Parameters:
      clazz - The class to get the field from
      name - The name of the field
      Returns:
      The field as an optional or an empty optional if the field could not be found
      Throws:
      NullPointerException - If the given class or name is null
      See Also:
    • hasField

      public static boolean hasField(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name)
      Check if the given class has a field with the given name.
      Exceptions which occur during reflection will be ignored.
      Parameters:
      clazz - The class to check for the field
      name - The name of the field
      Returns:
      True if the given class has a field with the given name, otherwise false
      Throws:
      NullPointerException - If the given class or name is null
    • hasField

      public static boolean hasField(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Predicate<Field> predicate)
      Check if the given class has a field with the given name.
      Exceptions which occur during reflection will be ignored.
      Parameters:
      clazz - The class to check for the field
      name - The name of the field
      predicate - A predicate with an additional condition the field must meet
      Returns:
      True if the given class has a field with the given name, otherwise false
      Throws:
      NullPointerException - If the given class or name is null
    • get

      @NotNull public static @NotNull Optional<Object> get(@NotNull @NotNull Field field, @Nullable @Nullable Object instance)
      Gets the value of the given field from the given instance.
      Exceptions will not be logged or thrown by default.
      Parameters:
      field - The field to get the value from
      instance - The instance to get the value from
      Returns:
      The value of the field as an optional or an empty optional if the value could not be determined
      Throws:
      NullPointerException - If the given field is null
      See Also:
    • get

      @NotNull public static @NotNull Optional<Object> get(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Object instance)
      Gets the value from the field with the given name from the given class and instance.
      Parameters:
      clazz - The class to get the field from
      name - The name of the field
      instance - The instance to get the value from
      Returns:
      The value of the field as an optional or an empty optional if the value could not be determined
      Throws:
      NullPointerException - If the given class or name is null
      IllegalStateException - If no field with the given name could be found
      See Also:
    • set

      public static void set(@NotNull @NotNull Field field, @Nullable @Nullable Object instance, @Nullable @Nullable Object value)
      Sets the value of the given field in the given instance to the given value.
      Exceptions will not be logged or thrown by default.
      Parameters:
      field - The field to set the value to
      instance - The instance to set the value to
      value - The value to set
      Throws:
      NullPointerException - If the given field is null
      See Also:
    • set

      public static void set(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Object instance, @Nullable @Nullable Object value)
      Sets the value of the field with the given name in the given class and instance to the given value.
      Parameters:
      clazz - The class to set the field in
      name - The name of the field
      instance - The instance to set the value to
      value - The value to set
      Throws:
      NullPointerException - If the given class or name is null
      IllegalStateException - If no field with the given name could be found
      See Also:
    • handleException

      private static void handleException(@NotNull @NotNull Exception e)
      Handles the given exception.
      If the system property 'reflection.exceptions.log' is true, the exception will be logged.
      If the system property 'reflection.exceptions.throw' is true, the exception will be thrown.
      Parameters:
      e - The exception to handle
    • getSimpleNames

      @NotNull private static @NotNull List<String> getSimpleNames(Class<?> @Nullable ... classes)
      Maps the given classes to their simple names.
      If the given array is null or empty, an empty list will be returned.
      Parameters:
      classes - The list of classes
      Returns:
      A list containing the simple names
    • getSimpleNames

      @NotNull private static @NotNull List<String> getSimpleNames(Object @Nullable ... objects)
      Maps the given objects to their simple names by converting them to their classes first.
      If the given array is null or empty, an empty list will be returned.
      Parameters:
      objects - The list of objects
      Returns:
      A list containing the simple names