java.lang.Object
ushiosan.jvm_utilities.lang.reflection.FieldUtils

public final class FieldUtils extends Object
Class with functionalities for handling class fields with reflection
  • Method Details

    • findField

      public static Field findField(@NotNull @NotNull Class<?> cls, @NotNull @NotNull String field, @NotNull @NotNull ReflectionOpts<Field> opts) throws NoSuchFieldException
      It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method. Java handles fields independently for each class, and you cannot access a field that is inside another class, but inherits from it. That is why a trick is performed to perform such a task.
      Parameters:
      cls - the class you want to inspect
      field - the field you want to search for
      opts - reflection options
      Returns:
      the class field you want to access
      Throws:
      NoSuchFieldException - error if such field does not exist in the entire inheritance tree of the class
      See Also:
    • findField

      public static Field findField(@NotNull @NotNull Class<?> cls, @NotNull @NotNull String field) throws NoSuchFieldException
      It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method. Java handles fields independently for each class, and you cannot access a field that is inside another class, but inherits from it. That is why a trick is performed to perform such a task.
      Parameters:
      cls - the class you want to inspect
      field - the field you want to search for
      Returns:
      the class field you want to access
      Throws:
      NoSuchFieldException - Error if such field does not exist in the entire inheritance tree of the class
    • findFieldObj

      It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method. Java handles fields independently for each class, and you cannot access a field that is inside another class, but inherits from it. That is why a trick is performed to perform such a task.
      Parameters:
      obj - the object you want to inspect
      field - the field you want to search for
      opts - reflection options
      Returns:
      the class field you want to access
      Throws:
      NoSuchFieldException - Error if such field does not exist in the entire inheritance tree of the class
      See Also:
    • findFieldObj

      public static Field findFieldObj(@NotNull @NotNull Object obj, @NotNull @NotNull String field) throws NoSuchFieldException
      It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method. Java handles fields independently for each class, and you cannot access a field that is inside another class, but inherits from it. That is why a trick is performed to perform such a task.
      Parameters:
      obj - the object you want to inspect
      field - the field you want to search for
      Returns:
      the class field you want to access
      Throws:
      NoSuchFieldException - Error if such field does not exist in the entire inheritance tree of the class
    • getAllClassFields

      public static Field @NotNull [] getAllClassFields(@NotNull @NotNull Class<?> cls, @NotNull @NotNull ReflectionOpts<Field> opts)
      Returns all defined and undefined fields of a class (for this, this method accesses the inheritance tree to get all these fields)
      Parameters:
      cls - the class you want to inspect
      opts - reflection options
      Returns:
      all valid class fields
      See Also:
    • getAllClassFields

      public static Field @NotNull [] getAllClassFields(@NotNull @NotNull Class<?> cls)
      Returns all defined and undefined fields of a class (for this, this method accesses the inheritance tree to get all these fields)
      Parameters:
      cls - the class you want to inspect
      Returns:
      all valid class fields
    • regexOf

      @NotNull public static <T extends Member> @NotNull Predicate<T> regexOf(@NotNull @RegExp @NotNull String regex, boolean inverted)
      Generates a filter to capture the elements that meet said restriction
      Type Parameters:
      T - generic member type
      Parameters:
      regex - regular expression to apply
      inverted - option to perform the action inverted
      Returns:
      the filter instance with the desired behavior
    • regexOf

      @NotNull public static <T extends Member> @NotNull Predicate<T> regexOf(@NotNull @RegExp @NotNull String regex)
      Generates a filter to capture the elements that meet said restriction
      Type Parameters:
      T - generic member type
      Parameters:
      regex - regular expression to apply
      Returns:
      the filter instance with the desired behavior
    • regexOf

      @Contract(pure=true) @NotNull public static <T extends Member> @NotNull Predicate<T> regexOf(@NotNull @NotNull Pattern pattern, boolean inverted)
      Generates a filter to capture the elements that meet said restriction
      Type Parameters:
      T - generic member type
      Parameters:
      pattern - regular expression object to apply
      inverted - option to perform the action inverted
      Returns:
      the filter instance with the desired behavior
    • regexOf

      @NotNull public static <T extends Member> @NotNull Predicate<T> regexOf(@NotNull @NotNull Pattern pattern)
      Generates a filter to capture the elements that meet said restriction
      Type Parameters:
      T - generic member type
      Parameters:
      pattern - regular expression object to apply
      Returns:
      the filter instance with the desired behavior
    • regexMultipleOf

      @NotNull public static <T extends Member> @NotNull Predicate<T> regexMultipleOf(boolean inverted, @RegExp String @NotNull ... regex)
      Generates a filter to capture the items that meet that constraint. The only difference with the regexOf(String) method is that it contains multiple constraints.
      Type Parameters:
      T - generic member type
      Parameters:
      inverted - option to perform the action inverted
      regex - regular expression to apply
      Returns:
      the filter instance with the desired behavior
    • regexMultipleOf

      @NotNull public static <T extends Member> @NotNull Predicate<T> regexMultipleOf(@RegExp String @NotNull ... regex)
      Generates a filter to capture the items that meet that constraint. The only difference with the regexOf(String) method is that it contains multiple constraints.
      Type Parameters:
      T - generic member type
      Parameters:
      regex - regular expression to apply
      Returns:
      the filter instance with the desired behavior
    • regexMultipleOf

      @NotNull public static <T extends Member> @NotNull Predicate<T> regexMultipleOf(boolean inverted, Pattern @NotNull ... patterns)
      Generates a filter to capture the items that meet that constraint. The only difference with the regexOf(String) method is that it contains multiple constraints.
      Type Parameters:
      T - generic member type
      Parameters:
      inverted - option to perform the action inverted
      patterns - regular expression object to apply
      Returns:
      the filter instance with the desired behavior
    • regexMultipleOf

      @NotNull public static <T extends Member> @NotNull Predicate<T> regexMultipleOf(Pattern @NotNull ... patterns)
      Generates a filter to capture the items that meet that constraint. The only difference with the regexOf(String) method is that it contains multiple constraints.
      Type Parameters:
      T - generic member type
      Parameters:
      patterns - regular expression object to apply
      Returns:
      the filter instance with the desired behavior
    • excludeAll

      @NotNull public static <T extends Member> @NotNull Predicate<T> excludeAll(String @NotNull ... methods)
      Generates a filter to capture elements that are different from the names passed as parameters.
      Type Parameters:
      T - generic member type
      Parameters:
      methods - all the names you want to exclude
      Returns:
      the filter instance with the desired behavior
    • validField

      @NotNull public static @NotNull Predicate<Field> validField()
      Generates a filter to check if a field is valid within a class
      Returns:
      the filter instance with the desired behavior
    • validGetterMethod

      @NotNull public static @NotNull Predicate<Method> validGetterMethod()
      Generates a filter to check if a method is valid inside a class (also checks if it is a getter)
      Returns:
      the filter instance with the desired behavior
    • requireAnnotations

      @SafeVarargs @NotNull public static <T extends AccessibleObject> @NotNull Predicate<T> requireAnnotations(boolean inverted, Class<? extends Annotation> @NotNull ... annotations)
      Generates a filter to check if the object contains one or more annotations.
      Type Parameters:
      T - generic accessible type
      Parameters:
      inverted - option to perform the action inverted
      annotations - the annotations that are required
      Returns:
      the filter instance with the desired behavior
    • requireAnnotations

      @SafeVarargs @NotNull public static <T extends AccessibleObject> @NotNull Predicate<T> requireAnnotations(Class<? extends Annotation> @NotNull ... annotations)
      Generates a filter to check if the object contains one or more annotations.
      Type Parameters:
      T - generic accessible type
      Parameters:
      annotations - the annotations that are required
      Returns:
      the filter instance with the desired behavior