Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 22 additions & 57 deletions android/code_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,9 @@ bool CodeGenerator::Generate(
printer.Print(
"\n"
"import com.fasterxml.jackson.annotation.JsonProperty;\n"
"import com.fasterxml.jackson.core.JsonProcessingException;\n"
"import com.fasterxml.jackson.databind.ObjectMapper;\n"
"import com.fasterxml.jackson.annotation.JsonValue;\n"
"\n"
"import java.io.IOException;\n"
"import java.io.InputStream;\n"
"import java.io.Serializable;\n"
"import java.util.List;\n"
"\n");

Expand Down Expand Up @@ -296,17 +294,26 @@ void CodeGenerator::GenDescriptor(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
{
printer->Print("public static class $name$ {\n\n",
printer->Print("public static class $name$ implements Serializable {\n\n",
"name", message->name());
printer->Indent();

printer->Print("private static final long serialVersionUID = -$number$L;\n\n",
"number", std::to_string(rand() % 9999999999999 + 1000000000000));

for (int i = 0; i < message->field_count(); ++i) {
const google::protobuf::FieldDescriptor *field = message->field(i);
printer->Print("@JsonProperty(\"$name$\") $type$ $name$;\n",
printer->Print("public @JsonProperty(\"$name$\") $type$ $name$;\n",
"name", field->camelcase_name(),
"type", AndroidTypeForField(field, false));
}
printer->Print("\n");

if (0 != message->field_count()) {
printer->Print("public $name$() { /* no-op */ }\n\n",
"name", message->name());
}

printer->Print("public $name$(",
"name", message->name());
for (int i = 0, lastI = message->field_count() - 1; i <= lastI; ++i) {
Expand All @@ -327,12 +334,6 @@ void CodeGenerator::GenDescriptor(
}
printer->Outdent();
printer->Print("}\n\n");

CodeGenerator::GenMessage_toWriter(message, printer);
printer->Print("\n");

CodeGenerator::GenMessage_fromReader(message, printer);
printer->Print("\n");

CodeGenerator::GenMessage_equality(message, printer);
printer->Print("\n");
Expand Down Expand Up @@ -364,28 +365,6 @@ void CodeGenerator::GenDescriptor(
printer->Print("\n");
}

void CodeGenerator::GenMessage_fromReader(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
{
printer->Print("public static $name$ fromReader(InputStream inputStream) {\n",
"name", message->name());
printer->Indent();

printer->Print("try {\n");
printer->Indent();
printer->Print("return new ObjectMapper().readValue(inputStream, $name$.class);\n",
"name", message->name());
printer->Outdent();
printer->Print("} catch (IOException e) {\n");
printer->Indent();
printer->Print("e.printStackTrace();\n");
printer->Outdent();
printer->Print("}\n\nreturn null;\n");
printer->Outdent();
printer->Print("}\n");
}

void CodeGenerator::GenMessage_builder(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
Expand Down Expand Up @@ -489,26 +468,6 @@ void CodeGenerator::GenMessageBuilder(
printer->Print("}\n");
}

void CodeGenerator::GenMessage_toWriter(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
{
printer->Print("public String toWriter() {\n");
printer->Indent();

printer->Print("try {\n");
printer->Indent();
printer->Print("return new ObjectMapper().writer().withDefaultPrettyPrinter().writeValueAsString(this);\n");
printer->Outdent();
printer->Print("} catch (JsonProcessingException e) {\n");
printer->Indent();
printer->Print("e.printStackTrace();\n");
printer->Outdent();
printer->Print("}\n\nreturn null;\n");
printer->Outdent();
printer->Print("}\n");
}

void CodeGenerator::GenMessage_equality(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
Expand All @@ -528,8 +487,8 @@ void CodeGenerator::GenMessage_equality(

for (int i = 0, lastI = message->field_count() - 1; i <= lastI; ++i) {
const google::protobuf::FieldDescriptor *field = message->field(i);
printer->Print("this.$name$ == castObject.$name$",
"name", field->camelcase_name());
printer->Print(field->type() != google::protobuf::FieldDescriptor::TYPE_STRING ? "this.$name$ == castObject.$name$" : "this.$name$.equals(castObject.$name$)",
"name", field->camelcase_name());
if (i != lastI) {
printer->Print(" &&\n");
}
Expand Down Expand Up @@ -560,9 +519,15 @@ void CodeGenerator::GenEnum(
printer->Indent();
for (int i = 0; i < enum_desc->value_count(); ++i) {
string number = to_string(enum_desc->value(i)->number());
printer->Print("$key$,\n",
printer->Print("$key$",
"key", ToCamelCase(enum_desc->value(i)->name(), false));
printer->Print(i == enum_desc->value_count() - 1 ? ";\n\n" : ",\n");
}
printer->Print("@JsonValue\npublic int value() {\n");
printer->Indent();
printer->Print("return ordinal();\n");
printer->Outdent();
printer->Print("}\n");
printer->Outdent();
printer->Print("}\n\n");
}
Expand Down