Class DataClassCodecGenerator

java.lang.Object
eu.cloudnetservice.driver.impl.network.object.data.DataClassCodecGenerator

final class DataClassCodecGenerator extends Object
Internal generator for data class codec implementations in runtime.
Since:
4.0
  • Field Details

  • Constructor Details

    • DataClassCodecGenerator

      private DataClassCodecGenerator()
  • Method Details

    • generateClassCodec

      @NonNull public static @NonNull DataClassCodec generateClassCodec(@NonNull @NonNull List<Field> allFields, @NonNull @NonNull List<Class<?>> classHierarchy)
      Generates the codec implementation for the given class hierarchy, including all given fields. The root class is assumed to be the first element in the given class hierarchy list.
      Parameters:
      allFields - all fields in the class hierarchy that are subject to serialization.
      classHierarchy - the full class hierarchy that was inspected for fields to include.
      Returns:
      an instance of the generated codec for the root target class.
      Throws:
      NullPointerException - if the given fields or class hierarchy is null.
      NoSuchElementException - if the given hierarchy list has no elements.
    • generateClassCodecClass

      @NonNull private static MethodHandles.Lookup generateClassCodecClass(@NonNull @NonNull Class<?> target, @NonNull @NonNull List<Field> allFields, boolean root)
      Generates and defines the codec for the given target class. The deserialized method is only properly implemented if this is the root type, in all other cases the deserialize method will just return null.
      Parameters:
      target - the target class to create the codec for.
      allFields - all fields that are subject to serialization, include those not defined in the given target.
      root - if the given target class is the root class for which the codec is generated.
      Returns:
      a lookup with full privileged access to the defined codec class.
      Throws:
      NullPointerException - if the given target class or fields list is null.
    • generateSerializeMethod

      private static void generateSerializeMethod(@NonNull @NonNull CodeBuilder code, @NonNull @NonNull Class<?> target, @NonNull @NonNull List<Field> allFields, @NonNull @NonNull ClassDesc targetClassDesc, @NonNull @NonNull ClassDesc generatingClassDesc)
      Generates the serialize method of the data class codec for the given target type.
      Parameters:
      code - the code builder of the current codec class.
      target - the target class for which the codec is getting implemented.
      allFields - all fields, include those that are not defined in the given target class.
      targetClassDesc - the class descriptor of the target class.
      generatingClassDesc - the class descriptor of the class that is being generated.
      Throws:
      NullPointerException - if one of the parameters is null.
    • generateDeserializeMethod

      private static void generateDeserializeMethod(@NonNull @NonNull CodeBuilder code, @NonNull @NonNull List<Field> allFields, @NonNull @NonNull ClassDesc targetClassDesc, @NonNull @NonNull ClassDesc generatingClassDesc)
      Generates the deserialize method for the root type of the hierarchy.
      Parameters:
      code - the code builder of the current codec class.
      allFields - all fields in the class hierarchy.
      targetClassDesc - the class descriptor of the target class.
      generatingClassDesc - the class descriptor of the class that is being generated.
      Throws:
      NullPointerException - if one of the parameters is null.
    • getAndValidateGetterMethod

      @Nullable private static @Nullable Method getAndValidateGetterMethod(@NonNull @NonNull Field field)
      Finds and validates the getter method provided via an annotation for the given field. This method returns null in case no specific getter is provided for the given field.
      Parameters:
      field - the field to find the getter method for.
      Returns:
      the getter method for the given field, null if none is specifically provided.
      Throws:
      NullPointerException - if the given field is null.