Class Reflections


  • public class Reflections
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      Reflections()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Optional<?> getOpenEnumValue​(java.lang.Class<?> clazz, java.lang.Object instance)
      Extracts the underlying value from an open enum instance if the class follows the open enum pattern.
      • Methods inherited from class java.lang.Object

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

      • Reflections

        public Reflections()
    • Method Detail

      • getOpenEnumValue

        public static java.util.Optional<?> getOpenEnumValue​(java.lang.Class<?> clazz,
                                                             java.lang.Object instance)
        Extracts the underlying value from an open enum instance if the class follows the open enum pattern.

        An open enum 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 open enum 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 open enum pattern
        instance - the instance of the open enum class from which to extract the value
        Returns:
        Optional<?> containing the extracted value (String or Integer) if the class follows the open enum pattern and the value extraction succeeds, Optional.empty() otherwise