Package pl.gsmservice.gateway.utils
Class Reflections
- java.lang.Object
-
- pl.gsmservice.gateway.utils.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.
-
-
-
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)orof(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 patterninstance- 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
- A static factory method
-
-