Class MemberAccessors

java.lang.Object
org.linkki.util.reflection.accessor.MemberAccessors

public class MemberAccessors extends Object
Utility class to get easy access to the return value of member. In contrast to PropertyAccessor, values of fields can also be read, in addition to the return value of methods.

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 Details

    • 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.
    • getType

      @Deprecated(since="2.5.0") public static Class<?> getType(Member fieldOrMethod)
      Deprecated.
      use getType(Member, Type) instead to be able to handle generic types properly
      Returns the type of the given member, either the type of the field, or the return type of the method.
      Parameters:
      fieldOrMethod - a Field or Method to access the value
      Returns:
      the type of the member
      Throws:
      IllegalArgumentException - if the given member is not a field or a method
    • getType

      public static Class<?> getType(Member fieldOrMethod, Type clazz)
      Returns the type of the given member, either the type of the field, or the return type of the method. The concrete class is required to be able to handle generics types properly.
      Parameters:
      fieldOrMethod - a Field or Method to access the value
      clazz - the class that contains the member
      Returns:
      the type of the member
      Throws:
      IllegalArgumentException - if the given member is not a field or a method, or if the Type can not be resolved