Class RuntimeSchema<T>

java.lang.Object
io.protostuff.runtime.RuntimeSchema<T>
All Implemented Interfaces:
FieldMap<T>, Schema<T>

public final class RuntimeSchema<T> extends Object implements Schema<T>, FieldMap<T>
A schema that can be generated and cached at runtime for objects that have no schema. This is particularly useful for pojos from 3rd party libraries.
  • Field Details

  • Constructor Details

  • Method Details

    • map

      public static <T> boolean map(Class<? super T> baseClass, Class<T> typeClass)
      Maps the baseClass to a specific non-interface/non-abstract typeClass and registers it (this must be done on application startup).

      With this approach, there is no overhead of writing the type metadata if a baseClass field is serialized.

      Returns true if the baseClass does not exist.

      NOTE: This is only supported when RuntimeEnv.ID_STRATEGY is DefaultIdStrategy.

      Throws:
      IllegalArgumentException - if the typeClass is an interface or an abstract class.
    • register

      public static <T> boolean register(Class<T> typeClass, Schema<T> schema)
      Returns true if this there is no existing one or the same schema has already been registered (this must be done on application startup).

      NOTE: This is only supported when RuntimeEnv.ID_STRATEGY is DefaultIdStrategy.

    • register

      public static <T> boolean register(Class<T> typeClass)
      Returns true if this there is no existing one or the same schema has already been registered (this must be done on application startup).

      NOTE: This is only supported when RuntimeEnv.ID_STRATEGY is DefaultIdStrategy.

    • isRegistered

      public static boolean isRegistered(Class<?> typeClass)
      Returns true if the typeClass was not lazily created.

      Method overload for backwards compatibility.

    • isRegistered

      public static boolean isRegistered(Class<?> typeClass, IdStrategy strategy)
      Returns true if the typeClass was not lazily created.
    • getSchema

      public static <T> Schema<T> getSchema(Class<T> typeClass)
      Gets the schema that was either registered or lazily initialized at runtime.

      Method overload for backwards compatibility.

    • getSchema

      public static <T> Schema<T> getSchema(Class<T> typeClass, IdStrategy strategy)
      Gets the schema that was either registered or lazily initialized at runtime.
    • getSchemaWrapper

      static <T> HasSchema<T> getSchemaWrapper(Class<T> typeClass)
      Returns the schema wrapper.

      Method overload for backwards compatibility.

    • getSchemaWrapper

      static <T> HasSchema<T> getSchemaWrapper(Class<T> typeClass, IdStrategy strategy)
      Returns the schema wrapper.
    • createFrom

      public static <T> RuntimeSchema<T> createFrom(Class<T> typeClass)
      Generates a schema from the given class.

      Method overload for backwards compatibility.

    • createFrom

      public static <T> RuntimeSchema<T> createFrom(Class<T> typeClass, IdStrategy strategy)
      Generates a schema from the given class.
    • createFrom

      public static <T> RuntimeSchema<T> createFrom(Class<T> typeClass, String[] exclusions, IdStrategy strategy)
      Generates a schema from the given class with the exclusion of certain fields.
    • createFrom

      public static <T> RuntimeSchema<T> createFrom(Class<T> typeClass, Set<String> exclusions, IdStrategy strategy)
      Generates a schema from the given class with the exclusion of certain fields.
    • createFrom

      public static <T> RuntimeSchema<T> createFrom(Class<T> typeClass, Map<String,String> declaredFields, IdStrategy strategy)
      Generates a schema from the given class with the declared fields (inclusive) based from the given Map. The value of a the Map's entry will be the name used for the field (which enables aliasing).
    • findInstanceFields

      static Map<String,Field> findInstanceFields(Class<?> typeClass)
    • fill

      static void fill(Map<String,Field> fieldMap, Class<?> typeClass)
    • createFieldMap

      private FieldMap<T> createFieldMap(Collection<Field<T>> fields)
    • preferHashFieldMap

      private boolean preferHashFieldMap(Collection<Field<T>> fields, int lastFieldNumber)
    • getPipeSchema

      public Pipe.Schema<T> getPipeSchema()
      Returns the pipe schema linked to this.
    • getFieldByNumber

      public Field<T> getFieldByNumber(int n)
      Specified by:
      getFieldByNumber in interface FieldMap<T>
    • getFieldByName

      public Field<T> getFieldByName(String fieldName)
      Specified by:
      getFieldByName in interface FieldMap<T>
    • getFieldCount

      public int getFieldCount()
      Specified by:
      getFieldCount in interface FieldMap<T>
    • getFields

      public List<Field<T>> getFields()
      Specified by:
      getFields in interface FieldMap<T>
    • typeClass

      public Class<T> typeClass()
      Description copied from interface: Schema
      Gets the class of the message.
      Specified by:
      typeClass in interface Schema<T>
    • messageName

      public String messageName()
      Description copied from interface: Schema
      Returns the simple name of the message tied to this schema. Allows custom schemas to provide a custom name other than typeClass().getSimpleName();
      Specified by:
      messageName in interface Schema<T>
    • messageFullName

      public String messageFullName()
      Description copied from interface: Schema
      Returns the full name of the message tied to this schema. Allows custom schemas to provide a custom name other than typeClass().getName();
      Specified by:
      messageFullName in interface Schema<T>
    • getFieldName

      public String getFieldName(int number)
      Description copied from interface: Schema
      Gets the field name associated with the number. This is particularly useful when serializing to different formats (Eg. JSON). When using numeric field names:
       return String.valueOf(number);
       
      Specified by:
      getFieldName in interface Schema<T>
    • getFieldNumber

      public int getFieldNumber(String name)
      Description copied from interface: Schema
      Gets the field number associated with the name. This is particularly useful when serializing to different formats (Eg. JSON). When using numeric field names:
       return Integer.parseInt(name);
       
      Specified by:
      getFieldNumber in interface Schema<T>
    • mergeFrom

      public final void mergeFrom(Input input, T message) throws IOException
      Description copied from interface: Schema
      Deserializes a message/object from the input.
      Specified by:
      mergeFrom in interface Schema<T>
      Throws:
      IOException
    • writeTo

      public final void writeTo(Output output, T message) throws IOException
      Description copied from interface: Schema
      Serializes a message/object to the output.
      Specified by:
      writeTo in interface Schema<T>
      Throws:
      IOException
    • isInitialized

      public boolean isInitialized(T message)
      Always returns true, everything is optional.
      Specified by:
      isInitialized in interface Schema<T>
    • newMessage

      public T newMessage()
      Description copied from interface: Schema
      Creates the message/object tied to this schema.
      Specified by:
      newMessage in interface Schema<T>
    • resolvePipeSchema

      static <T> Pipe.Schema<T> resolvePipeSchema(Schema<T> schema, Class<? super T> clazz, boolean throwIfNone)
      Invoked only when applications are having pipe io operations.