Class Factory

java.lang.Object
org.simpleframework.xml.core.Factory
Direct Known Subclasses:
ArrayFactory, CollectionFactory, MapFactory, PrimitiveFactory

abstract class Factory extends Object
The Factory object provides a base class for factories used to produce field values from XML elements. The goal of this type of factory is to make use of the Strategy object to determine the type of the field value. The strategy class must be assignable to the field class type, that is, it must extend it or implement it if it represents an interface. If the strategy returns a null Value then the subclass implementation determines the type used to populate the object field value.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Context
    This is the context object used for the serialization process.
    protected Class
    This is the class override to used when instantiating objects.
    protected Support
    This is used to translate all of the primitive type strings.
    protected Type
    This is the field type that the class must be assignable to.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Factory(Context context, Type type)
    Constructor for the Factory object.
    protected
    Factory(Context context, Type type, Class override)
    Constructor for the Factory object.
  • Method Summary

    Modifier and Type
    Method
    Description
    This performs the conversion from the element node to a type.
    This is used to create a default instance of the field type.
    protected Value
    This is used to get a possible override from the provided node.
    private Type
    getPrimitive(Type type, Class expect)
    This is used to convert the Type provided as an overridden type.
    This is used to extract the type this factory is using.
    static boolean
    isCompatible(Class expect, Class type)
    This is used to determine whether the provided base class can be assigned from the issued type.
    static boolean
    This is used to determine whether the type given is instantiable, that is, this determines if an instance of that type can be created.
    boolean
    setOverride(Type type, Object value, OutputNode node)
    This method is used to set the override class within an element.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • context

      protected Context context
      This is the context object used for the serialization process.
    • support

      protected Support support
      This is used to translate all of the primitive type strings.
    • override

      protected Class override
      This is the class override to used when instantiating objects.
    • type

      protected Type type
      This is the field type that the class must be assignable to.
  • Constructor Details

    • Factory

      protected Factory(Context context, Type type)
      Constructor for the Factory object. This is given the class type for the field that this factory will determine the actual type for. The actual type must be assignable to the field type to insure that any instance can be set.
      Parameters:
      context - the contextual object used by the persister
      type - this is the property representing the field
    • Factory

      protected Factory(Context context, Type type, Class override)
      Constructor for the Factory object. This is given the class type for the field that this factory will determine the actual type for. The actual type must be assignable to the field type to insure that any instance can be set.
      Parameters:
      context - the contextual object used by the persister
      type - this is the property representing the field
      override - this is the override used for this factory
  • Method Details

    • getType

      public Class getType()
      This is used to extract the type this factory is using. Each factory represents a specific class, which it instantiates if required. This method provides the represented class.
      Returns:
      this returns the class represented by the factory
    • getInstance

      public Object getInstance() throws Exception
      This is used to create a default instance of the field type. It is up to the subclass to determine how to best instantiate an object of the field type that best suits. This is used when the empty value is required or to create the default type instance.
      Returns:
      a type which is used to instantiate the collection
      Throws:
      Exception
    • getOverride

      protected Value getOverride(InputNode node) throws Exception
      This is used to get a possible override from the provided node. If the node provided is an element then this checks for a specific class override using the Strategy object. If the strategy cannot resolve a class then this will return null. If the resolved Value is not assignable to the field then this will thrown an exception.
      Parameters:
      node - this is the node used to search for the override
      Returns:
      this returns null if no override type can be found
      Throws:
      Exception - if the override type is not compatible
    • setOverride

      public boolean setOverride(Type type, Object value, OutputNode node) throws Exception
      This method is used to set the override class within an element. This delegates to the Strategy implementation, which depending on the implementation may add an attribute of a child element to describe the type of the object provided to this.
      Parameters:
      type - this is the class of the field type being serialized
      node - the XML element that is to be given the details
      Throws:
      Exception - thrown if an error occurs within the strategy
    • getPrimitive

      private Type getPrimitive(Type type, Class expect) throws Exception
      This is used to convert the Type provided as an overridden type. Overriding the type in this way ensures that if a primitive type, represented as a boxed object, is given to a strategy then the strategy will see a match in the types used.
      Parameters:
      type - this is the field or method that is a primitive
      expect - this is the boxed object type to be converted
      Returns:
      this returns a type representing the boxed type
      Throws:
      Exception
    • getConversion

      public Value getConversion(InputNode node) throws Exception
      This performs the conversion from the element node to a type. This is where the Strategy object is consulted and asked for a class that will represent the provided XML element. This will, depending on the strategy implementation, make use of attributes and/or elements to determine the type for the field.
      Parameters:
      node - this is the element used to extract the override
      Returns:
      this returns null if no override type can be found
      Throws:
      Exception - thrown if the override class cannot be loaded
    • isCompatible

      public static boolean isCompatible(Class expect, Class type)
      This is used to determine whether the provided base class can be assigned from the issued type. For an override to be compatible with the field type an instance of the override type must be assignable to the field value.
      Parameters:
      expect - this is the field value present the the object
      type - this is the specialized type that will be assigned
      Returns:
      true if the field type can be assigned the type value
    • isInstantiable

      public static boolean isInstantiable(Class type)
      This is used to determine whether the type given is instantiable, that is, this determines if an instance of that type can be created. If the type is an interface or an abstract class then this will return false.
      Parameters:
      type - this is the type to check the modifiers of
      Returns:
      false if the type is an interface or an abstract class