writeClassEncode method

  1. @override
void writeClassEncode(
  1. JavaOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Class klass,
  5. Set<String> customClassNames,
  6. Set<String> customEnumNames, {
  7. required String dartPackageName,
})
override

Writes a single class encode method to indent.

Implementation

@override
void writeClassEncode(
  JavaOptions generatorOptions,
  Root root,
  Indent indent,
  Class klass,
  Set<String> customClassNames,
  Set<String> customEnumNames, {
  required String dartPackageName,
}) {
  indent.newln();
  indent.writeln('@NonNull');
  indent.write('ArrayList<Object> toList() ');
  indent.addScoped('{', '}', () {
    indent.writeln(
        'ArrayList<Object> toListResult = new ArrayList<Object>(${klass.fields.length});');
    for (final NamedType field in getFieldsInSerializationOrder(klass)) {
      final HostDatatype hostDatatype = getFieldHostDatatype(
          field,
          root.classes,
          root.enums,
          (TypeDeclaration x) => _javaTypeForBuiltinDartType(x));
      String toWriteValue = '';
      final String fieldName = field.name;
      if (!hostDatatype.isBuiltin &&
          customClassNames.contains(field.type.baseName)) {
        toWriteValue = '($fieldName == null) ? null : $fieldName.toList()';
      } else if (!hostDatatype.isBuiltin &&
          customEnumNames.contains(field.type.baseName)) {
        toWriteValue = '$fieldName == null ? null : $fieldName.index';
      } else {
        toWriteValue = field.name;
      }
      indent.writeln('toListResult.add($toWriteValue);');
    }
    indent.writeln('return toListResult;');
  });
}