Class Reflections

java.lang.Object
com.google.genai.gaos.utils.Reflections

public class Reflections extends Object
  • Constructor Details

    • Reflections

      public Reflections()
  • Method Details

    • getUnwrappedEnumValue

      public static Optional<?> getUnwrappedEnumValue(Class<?> clazz, Object instance)
      Extracts the underlying value from an enum wrapper instance if the class follows the enum wrapper pattern.

      An enum wrapper is a class that emulates enum behavior but can handle unknown values without runtime errors. This pattern is commonly used for API responses where new enum values might be added over time.

      The method validates that the class follows the enum wrapper pattern by checking for:

      • A static factory method of(String) or of(Integer) that returns the class type
      • An instance method value() returning String or Integer
      • At least one public static final field of the same class type (predefined constants)

      If all validation passes, the method invokes the value() method on the provided instance and returns the result.

      Parameters:
      clazz - the class to examine for enum wrapper pattern
      instance - the instance of the enum wrapper class from which to extract the value
      Returns:
      Optional<?> containing the extracted value (String or Integer) if the class follows the enum wrapper pattern and the value extraction succeeds, Optional.empty() otherwise
    • isEnumWrapper

      public static boolean isEnumWrapper(Class<?> clazz)
      Checks if the given class is an enum wrapper.

      An enum wrapper is a class that emulates enum behavior but can handle unknown values without runtime errors. This pattern is commonly used for API responses where new enum values might be added over time.

      The method validates that the class follows the enum wrapper pattern by checking for:

      • A static factory method of(String) or of(Integer) that returns the class type
      • An instance method value() returning String or Integer
      • At least one public static final field of the same class type (predefined constants)
      Parameters:
      clazz - the class to examine for enum wrapper pattern
      Returns:
      true if the class is an enum wrapper, false otherwise
    • isEnumWrapper

      public static boolean isEnumWrapper(Object obj)
      Checks if the given object is an instance of an enum wrapper class.
      Parameters:
      obj - the object to check
      Returns:
      true if the object is an instance of an enum wrapper class, false otherwise