Class SimplifierAccessor<T>

java.lang.Object
org.jolokia.service.serializer.json.simplifier.SimplifierAccessor<T>
All Implemented Interfaces:
ObjectAccessor
Direct Known Subclasses:
ClassSimplifier, DomElementSimplifier, FileSimplifier, InetAddressSimplifier, ModuleSimplifier, ObjectNameSimplifier, PackageSimplifier, UrlSimplifier

public abstract class SimplifierAccessor<T> extends Object implements ObjectAccessor

Base class for all simplifiers. A simplifier is a special ObjectAccessor which condenses full blown Java beans (like File) to a more compact representation. Simplifiers cannot be used to set values and are only used for downstream serialization.

Simplifiers work similarly to BeanAccessor, but instead if extracting all properties for all public getters, they delegate to type-specific attribute extractors, which are used to create a JSONObject with explicitly configured attributes (not necessarily related to getters).

Simplifier are registered by listing the classes in a META-INF/simplifiers plain text file and then picked up during initialization. The default prepackaged simplifiers are taken from META-INF/simplifiers-default.

Since:
Jul 27, 2009
Author:
roland
  • Constructor Details

    • SimplifierAccessor

      protected SimplifierAccessor(Class<T> pType)
      Super constructor taking the value type as argument
      Parameters:
      pType - the type handled by this SimplifierAccessor
  • Method Details

    • getType

      public Class<?> getType()
      Description copied from interface: ObjectAccessor
      Gets a class of supported objects.
      Specified by:
      getType in interface ObjectAccessor
      Returns:
      supported type of the objects handled by this accessor
    • extractObject

      public Object extractObject(ObjectToJsonConverter pConverter, Object pValue, Deque<String> pPathParts, boolean pJsonify) throws AttributeNotFoundException
      Description copied from interface: ObjectAccessor

      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.

      Specified by:
      extractObject in interface ObjectAccessor
      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

      public final boolean supportsStringConversion()
      Description copied from interface: ObjectAccessor
      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.
      Specified by:
      supportsStringConversion in interface ObjectAccessor
      Returns:
    • canSetValue

      public final boolean canSetValue()
      No setting for simplifying accessors
      Specified by:
      canSetValue in interface ObjectAccessor
      Returns:
      always false
    • setObjectValue

      public final Object setObjectValue(Converter<String> pConverter, Object pObject, String pAttribute, Object pValue) throws IllegalAccessException, InvocationTargetException
      Always throws IllegalArgumentException since a simplifier cannot be used to write values to objects.
      Specified by:
      setObjectValue in interface ObjectAccessor
      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
    • addExtractor

      protected final void addExtractor(String pName, AttributeExtractor<T> pExtractor)
      Add a single extractor - to be called by implementing class in the constructor
      Parameters:
      pName - name of the extractor - it'll match the attribute name of the constructed JSONObject for the class being simplified.
      pExtractor - the extractor itself