toMapFunc method

JavaFunction toMapFunc()

Implementation

JavaFunction toMapFunc() {
  return JavaFunction(
      functionName: 'toMap',
      returnType: AstMap(keyType: AstString()), // fixme 生成出来代码没有范性
      depth: depth + 1,
      isPublic: true,
      isOverride: true,
      body: (depth) => [
            OneLine(
                depth: depth + 1,
                body: Variable(AstMap(keyType: AstString()), 'result')
                    .javaDeclaration(newInstance: true)),
            ...privateFields.map((field) {
              return OneLine(
                  depth: depth + 1,
                  body:
                      '${(field.type.realType() is AstCustomType) ? 'if (${field.name} != null) ' : ''}result.put("${field.name}", ${field.type.realType().convertJavaObj2Json(field.name)});');
            }),
            OneLine(depth: depth + 1, body: 'return result;')
          ]);
}