writeClassEncode method
void
writeClassEncode(
- JavaOptions generatorOptions,
- Root root,
- Indent indent,
- Class klass,
- Set<
String> customClassNames, - Set<
String> customEnumNames, { - 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;');
});
}