Class ReflectionUtils

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

public final class ReflectionUtils extends Object
Utility class for reflection-related operations.
  • 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
    private static @NotNull Object
    findParameter(@NotNull Parameter parameter, @NotNull List<Pair<Object,String>> values)
    Finds the value for the given parameter.
    The value will be searched by its type, and if the type is ambiguous, by its name.
    An exception will be thrown if no value for the parameter could be found.
    static Object @NotNull []
    findParameters(@NotNull Executable executable, Object @Nullable ... values)
    Finds the parameters in the correct order for the given executable.
    The parameters will be searched by their type, and if the type is ambiguous, by their name.
    static Object @NotNull []
    findParameters(@NotNull Executable executable, @Nullable List<Pair<Object,String>> values)
    Finds the parameters in the correct order for the given executable.
    The parameters will be searched by their type, and if the type is ambiguous, by their name.
    static @NotNull Optional<Field>
    getAnnotatedField(@NotNull Class<?> clazz, @NotNull String name, @NotNull Class<? extends Annotation> annotation)
    Gets the field with the given name from the given class which is annotated with the given annotation.
    If the field does not exist or is not annotated with the given annotation, an empty optional will be returned.
    static @NotNull List<Field>
    getAnnotatedFields(@NotNull Class<?> clazz, @NotNull Class<? extends Annotation> annotation)
    Gets all fields from the given class that are annotated with the given annotation.
    static @NotNull Optional<Method>
    getAnnotatedMethod(@NotNull Class<?> clazz, @NotNull String name, @NotNull Class<? extends Annotation> annotation, Class<?> @Nullable ... parameters)
    Gets the method with the given name from the given class which is annotated with the given annotation.
    static @NotNull List<Method>
    getAnnotatedMethods(@NotNull Class<?> clazz, @NotNull Class<? extends Annotation> annotation)
    Gets all methods from the given class that are annotated with the given annotation.
    static @NotNull List<Field>
    getFieldsForType(@NotNull Class<?> clazz, @Nullable Class<?> type)
    Gets all fields from the given class for the given type.
    If the given type is null, an empty list will be returned.
    static @NotNull Optional<Method>
    getMethodForName(@NotNull Class<?> clazz, @Nullable String name)
    Gets the method with the given name from the given class.
    If the given name is null or there are multiple methods with the given name, an empty optional will be returned.
    static @NotNull List<Method>
    getMethodsForName(@NotNull Class<?> clazz, @Nullable String name)
    Gets all methods of the given class for the given name (case-sensitive).
    If the given name is null, all methods of the given class will be returned.
    The methods will be returned in the order of their parameter count.
    static @NotNull String
    getRawName(@NotNull Method method, String @Nullable ... prefixes)
    Gets the raw name of the given method.
    The raw name is the name of the method without prefixes like "get", "set", "is" or "has".

    Methods inherited from class java.lang.Object

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

    • ReflectionUtils

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

    • getRawName

      @NotNull public static @NotNull String getRawName(@NotNull @NotNull Method method, String @Nullable ... prefixes)
      Gets the raw name of the given method.
      The raw name is the name of the method without prefixes like "get", "set", "is" or "has".
      Parameters:
      method - The method to get the raw name of
      prefixes - Additional prefixes to remove
      Returns:
      The raw name of the given method or Method.getName() as fallback
      Throws:
      NullPointerException - If the given method is null
    • getAnnotatedMethods

      @NotNull public static @NotNull List<Method> getAnnotatedMethods(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull Class<? extends Annotation> annotation)
      Gets all methods from the given class that are annotated with the given annotation.
      Parameters:
      clazz - The class in which the methods should be
      annotation - The annotation which should be present on the methods
      Returns:
      A list of all methods
      Throws:
      NullPointerException - If the given class or annotation is null
    • getAnnotatedMethod

      @NotNull public static @NotNull Optional<Method> getAnnotatedMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @NotNull @NotNull Class<? extends Annotation> annotation, Class<?> @Nullable ... parameters)
      Gets the method with the given name from the given class which is annotated with the given annotation.
      Parameters:
      clazz - The class in which the method should be
      name - The name of the method
      annotation - The annotation which should be present on the method
      parameters - The parameters of the method
      Returns:
      An optional containing the method or an empty optional if the method was not found
      Throws:
      NullPointerException - If the given class, name or annotation is null
    • getMethodsForName

      @NotNull public static @NotNull List<Method> getMethodsForName(@NotNull @NotNull Class<?> clazz, @Nullable @Nullable String name)
      Gets all methods of the given class for the given name (case-sensitive).
      If the given name is null, all methods of the given class will be returned.
      The methods will be returned in the order of their parameter count.
      Parameters:
      clazz - The class to get the methods of
      name - The name of the methods to get
      Returns:
      A list of all methods from the given class for the given name
      Throws:
      NullPointerException - If the given class is null
    • getMethodForName

      @NotNull public static @NotNull Optional<Method> getMethodForName(@NotNull @NotNull Class<?> clazz, @Nullable @Nullable String name)
      Gets the method with the given name from the given class.
      If the given name is null or there are multiple methods with the given name, an empty optional will be returned.
      Parameters:
      clazz - The class to get the method from
      name - The name of the method
      Returns:
      An optional containing the method or an empty optional if the method was not found
      Throws:
      NullPointerException - If the given class is null
    • findParameters

      public static Object @NotNull [] findParameters(@NotNull @NotNull Executable executable, Object @Nullable ... values)
      Finds the parameters in the correct order for the given executable.
      The parameters will be searched by their type, and if the type is ambiguous, by their name.

      In the case the compiler has not included the parameter names,
      and the types are ambiguous, an exception will be thrown.
      If the given executable has no parameters, an empty array will be returned.

      Parameters:
      executable - The executable to find the parameters for
      values - The possible values for the parameters
      Returns:
      An array of the found parameters in the correct order
      Throws:
      NullPointerException - If the given executable is null
      IllegalArgumentException - If the type of the parameter is ambiguous and the name is not present
      IllegalArgumentException - If no value for a parameter could be found
      See Also:
    • findParameters

      public static Object @NotNull [] findParameters(@NotNull @NotNull Executable executable, @Nullable @Nullable List<Pair<Object,String>> values)
      Finds the parameters in the correct order for the given executable.
      The parameters will be searched by their type, and if the type is ambiguous, by their name.

      In the case the compiler has not included the parameter names,
      and the types are ambiguous, an exception will be thrown.
      If the given executable has no parameters, an empty array will be returned.

      Parameters:
      executable - The executable to find the parameters for
      values - The possible values for the parameters
      Returns:
      An array of the found parameters in the correct order
      Throws:
      NullPointerException - If the given executable is null
      IllegalArgumentException - If the type of the parameter is ambiguous and the name is not present
      IllegalArgumentException - If no value for a parameter could be found
      See Also:
    • getAnnotatedFields

      @NotNull public static @NotNull List<Field> getAnnotatedFields(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull Class<? extends Annotation> annotation)
      Gets all fields from the given class that are annotated with the given annotation.
      Parameters:
      clazz - The class in which the fields should be
      annotation - The annotation which should be present on the fields
      Returns:
      A list of all fields
      Throws:
      NullPointerException - If the given class or annotation is null
    • getAnnotatedField

      @NotNull public static @NotNull Optional<Field> getAnnotatedField(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @NotNull @NotNull Class<? extends Annotation> annotation)
      Gets the field with the given name from the given class which is annotated with the given annotation.
      If the field does not exist or is not annotated with the given annotation, an empty optional will be returned.
      Parameters:
      clazz - The class in which the field should be
      name - The name of the field
      annotation - The annotation which should be present on the field
      Returns:
      An optional containing the field or an empty optional if the field was not found
      Throws:
      NullPointerException - If the given class, name or annotation is null
    • getFieldsForType

      @NotNull public static @NotNull List<Field> getFieldsForType(@NotNull @NotNull Class<?> clazz, @Nullable @Nullable Class<?> type)
      Gets all fields from the given class for the given type.
      If the given type is null, an empty list will be returned.
      Parameters:
      clazz - The class to get the fields of
      type - The type of the fields to get
      Returns:
      A list of all fields from the given class for the given type
      Throws:
      NullPointerException - If the given class is null
    • findParameter

      @NotNull private static @NotNull Object findParameter(@NotNull @NotNull Parameter parameter, @NotNull @NotNull List<Pair<Object,String>> values)
      Finds the value for the given parameter.
      The value will be searched by its type, and if the type is ambiguous, by its name.
      An exception will be thrown if no value for the parameter could be found.
      Parameters:
      parameter - The parameter to find the value for
      values - The possible values for the parameter
      Returns:
      The value found for the given parameter
      Throws:
      NullPointerException - If the given parameter or values are null
      IllegalArgumentException - If the type of the parameter is ambiguous and the name is not present
      IllegalStateException - If no value for the parameter could be found