Interface ObjectAccessor

All Known Implementing Classes:
ArrayAccessor, BeanAccessor, CalendarAccessor, ClassSimplifier, CollectionAccessor, CompositeDataAccessor, DateAccessor, DomElementSimplifier, EnumObjectAccessor, FileSimplifier, InetAddressSimplifier, JavaTimeTemporalAccessor, ListAccessor, MapAccessor, ModuleSimplifier, ObjectNameSimplifier, PackageSimplifier, SimpleAccessor, SimplifierAccessor, TabularDataAccessor, UrlSimplifier

public interface ObjectAccessor

Interface for extracting objects or their inner data (with optional conversion of the values into their JSON representation) and for manipulating the internal state of the objects (not possible for some types of objects).

Up to Jolokia 2.3.0, this interface was called org.jolokia.converter.json.Extractor, but it was too confusing name for the two-way nature of this interface. New name is inspired by Spring's org.springframework.beans.PropertyAccessor which can also get and set values of an object. Instead of using property paths as strings, we use Deque of nested properties which are interpreted according to given type (like array index or key of a map).

Since:
Jul 2, 2010
Author:
roland
  • Method Details

    • getType

      Class<?> getType()
      Gets a class of supported objects.
      Returns:
      supported type of the objects handled by this accessor
    • extractObject

      Object extractObject(ObjectToJsonConverter pConverter, Object pValue, Deque<String> pPathParts, boolean pJsonify) throws AttributeNotFoundException

      Extract an object from given pValue of the supported type.

      In the simplest case without jsonification and without a property path, we extract the value itself.

      pPathParts may be used as a Deque (a stack) of keys into the object being accessed, where the first item (Deque.pop()) is the 1st level inner object and the remainder of the stack is passed recursively when accessing composite objects. First key may be null, which means that the remaining path parts are applied to each item of the object (like elements of the array).

      pJsonify flag indicates that the returned value should be converted into a JSON representation.

      Parameters:
      pConverter - the top-level converter that knows about other accessors used for drilling into inner objects.
      pValue - the objects being accessed
      pPathParts - a path into inner object(s) to access internal structure of the object. Each accessor should Deque.pop() its attribute and pass the remaining stack recursively.
      pJsonify - whether to convert the extracted object into a JSON representation
      Returns:
      the extracted object in JSON or original format
      Throws:
      AttributeNotFoundException - if the inner path does not exist.
    • supportsStringConversion

      default boolean supportsStringConversion()
      Some accessors may support dedicated conversion to String. By default, generic extraction of the value from given object should be used, but in some cases (like explicit conversion to String for Map keys), some accessors can be used to get a String value.
      Returns:
      Since:
      2.4.0
    • extractString

      default String extractString(Object pValue)
      Convert a value to String - only if such conversion is supported.
      Parameters:
      pValue -
      Returns:
      Since:
      2.4.0
    • canSetValue

      boolean canSetValue()
      Whether this accessor can be used to set a value inside (directly or nested) a value of given class.
      Returns:
      true if this extractor can be used to set inner values.
    • setObjectValue

      Object setObjectValue(Converter<String> pConverter, Object pObject, String pAttribute, Object pValue) throws IllegalAccessException, InvocationTargetException, IllegalArgumentException
      If this accessor can be used to set inner values, this method sets the value inside the passed outer object.
      Parameters:
      pConverter - Converter used to convert the value being set to a class of the accessed attribute
      pObject - object on which to set the value
      pAttribute - attribute of the object to set. (For arrays or lists it should be an index.)
      pValue - the new value to set after conversion
      Returns:
      the old value of the changed attribute
      Throws:
      IllegalAccessException - if the attribute to set to is not accessible
      InvocationTargetException - reflection error
      IllegalArgumentException - if the attribute can't be converted to desired kind (like array index number)