Class RuntimeSchema<T>

  • All Implemented Interfaces:
    FieldMap<T>, Schema<T>

    public final class RuntimeSchema<T>
    extends java.lang.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.
    • Constructor Summary

      Constructors 
      Constructor Description
      RuntimeSchema​(java.lang.Class<T> typeClass, java.util.Collection<Field<T>> fields, RuntimeEnv.Instantiator<T> instantiator)  
      RuntimeSchema​(java.lang.Class<T> typeClass, java.util.Collection<Field<T>> fields, java.lang.reflect.Constructor<T> constructor)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private FieldMap<T> createFieldMap​(java.util.Collection<Field<T>> fields)  
      static <T> RuntimeSchema<T> createFrom​(java.lang.Class<T> typeClass)
      Generates a schema from the given class.
      static <T> RuntimeSchema<T> createFrom​(java.lang.Class<T> typeClass, IdStrategy strategy)
      Generates a schema from the given class.
      static <T> RuntimeSchema<T> createFrom​(java.lang.Class<T> typeClass, java.lang.String[] exclusions, IdStrategy strategy)
      Generates a schema from the given class with the exclusion of certain fields.
      static <T> RuntimeSchema<T> createFrom​(java.lang.Class<T> typeClass, java.util.Map<java.lang.String,​java.lang.String> declaredFields, IdStrategy strategy)
      Generates a schema from the given class with the declared fields (inclusive) based from the given Map.
      static <T> RuntimeSchema<T> createFrom​(java.lang.Class<T> typeClass, java.util.Set<java.lang.String> exclusions, IdStrategy strategy)
      Generates a schema from the given class with the exclusion of certain fields.
      (package private) static void fill​(java.util.Map<java.lang.String,​java.lang.reflect.Field> fieldMap, java.lang.Class<?> typeClass)  
      (package private) static java.util.Map<java.lang.String,​java.lang.reflect.Field> findInstanceFields​(java.lang.Class<?> typeClass)  
      Field<T> getFieldByName​(java.lang.String fieldName)  
      Field<T> getFieldByNumber​(int n)  
      int getFieldCount()  
      java.lang.String getFieldName​(int number)
      Gets the field name associated with the number.
      int getFieldNumber​(java.lang.String name)
      Gets the field number associated with the name.
      java.util.List<Field<T>> getFields()  
      Pipe.Schema<T> getPipeSchema()
      Returns the pipe schema linked to this.
      static <T> Schema<T> getSchema​(java.lang.Class<T> typeClass)
      Gets the schema that was either registered or lazily initialized at runtime.
      static <T> Schema<T> getSchema​(java.lang.Class<T> typeClass, IdStrategy strategy)
      Gets the schema that was either registered or lazily initialized at runtime.
      (package private) static <T> HasSchema<T> getSchemaWrapper​(java.lang.Class<T> typeClass)
      Returns the schema wrapper.
      (package private) static <T> HasSchema<T> getSchemaWrapper​(java.lang.Class<T> typeClass, IdStrategy strategy)
      Returns the schema wrapper.
      boolean isInitialized​(T message)
      Always returns true, everything is optional.
      static boolean isRegistered​(java.lang.Class<?> typeClass)
      Returns true if the typeClass was not lazily created.
      static boolean isRegistered​(java.lang.Class<?> typeClass, IdStrategy strategy)
      Returns true if the typeClass was not lazily created.
      static <T> boolean map​(java.lang.Class<? super T> baseClass, java.lang.Class<T> typeClass)
      Maps the baseClass to a specific non-interface/non-abstract typeClass and registers it (this must be done on application startup).
      void mergeFrom​(Input input, T message)
      Deserializes a message/object from the input.
      java.lang.String messageFullName()
      Returns the full name of the message tied to this schema.
      java.lang.String messageName()
      Returns the simple name of the message tied to this schema.
      T newMessage()
      Creates the message/object tied to this schema.
      private boolean preferHashFieldMap​(java.util.Collection<Field<T>> fields, int lastFieldNumber)  
      static <T> boolean register​(java.lang.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).
      static <T> boolean register​(java.lang.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).
      (package private) static <T> Pipe.Schema<T> resolvePipeSchema​(Schema<T> schema, java.lang.Class<? super T> clazz, boolean throwIfNone)
      Invoked only when applications are having pipe io operations.
      java.lang.Class<T> typeClass()
      Gets the class of the message.
      void writeTo​(Output output, T message)
      Serializes a message/object to the output.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RuntimeSchema

        public RuntimeSchema​(java.lang.Class<T> typeClass,
                             java.util.Collection<Field<T>> fields,
                             java.lang.reflect.Constructor<T> constructor)
    • Method Detail

      • map

        public static <T> boolean map​(java.lang.Class<? super T> baseClass,
                                      java.lang.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:
        java.lang.IllegalArgumentException - if the typeClass is an interface or an abstract class.
      • register

        public static <T> boolean register​(java.lang.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​(java.lang.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​(java.lang.Class<?> typeClass)
        Returns true if the typeClass was not lazily created.

        Method overload for backwards compatibility.

      • isRegistered

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

        public static <T> Schema<T> getSchema​(java.lang.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​(java.lang.Class<T> typeClass,
                                              IdStrategy strategy)
        Gets the schema that was either registered or lazily initialized at runtime.
      • getSchemaWrapper

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

        Method overload for backwards compatibility.

      • getSchemaWrapper

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

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

        Method overload for backwards compatibility.

      • createFrom

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

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

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

        public static <T> RuntimeSchema<T> createFrom​(java.lang.Class<T> typeClass,
                                                      java.util.Map<java.lang.String,​java.lang.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 java.util.Map<java.lang.String,​java.lang.reflect.Field> findInstanceFields​(java.lang.Class<?> typeClass)
      • fill

        static void fill​(java.util.Map<java.lang.String,​java.lang.reflect.Field> fieldMap,
                         java.lang.Class<?> typeClass)
      • createFieldMap

        private FieldMap<T> createFieldMap​(java.util.Collection<Field<T>> fields)
      • preferHashFieldMap

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

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

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

        public java.lang.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 java.lang.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 java.lang.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​(java.lang.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 java.io.IOException
        Description copied from interface: Schema
        Deserializes a message/object from the input.
        Specified by:
        mergeFrom in interface Schema<T>
        Throws:
        java.io.IOException
      • writeTo

        public final void writeTo​(Output output,
                                  T message)
                           throws java.io.IOException
        Description copied from interface: Schema
        Serializes a message/object to the output.
        Specified by:
        writeTo in interface Schema<T>
        Throws:
        java.io.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,
                                                    java.lang.Class<? super T> clazz,
                                                    boolean throwIfNone)
        Invoked only when applications are having pipe io operations.