Class MemberAccessors


  • public class MemberAccessors
    extends Object
    Utility class to get easy access to the return value of a method or the current object of a field. Simply use getValue(Object, Member).

    The implementation may use any performance optimization like caching of access code but never caches the returned value. Every call will retrieve the current value from the method or field.

    • Method Detail

      • getValue

        public static <T> T getValue​(Object object,
                                     Member fieldOrMethod)
        Retrieve the value from the given member (Field or Method) in the given object by calling the method or getting the value from the field. The returned value will directly be cast to the expected type. Note that this is always an unsafe cast!
        Type Parameters:
        T - the expected type of the return value
        Parameters:
        object - the object where to read the value from
        fieldOrMethod - a Field or Method to access the value
        Returns:
        the value retrieved by the Field or Method
        Throws:
        IllegalArgumentException - if the field is not accessible or the value cannot be retrieved. The cause of the original exception is included.