From 246e6e8bf36bf5c6f92a71c0c16111a258dd2d19 Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Mon, 6 Jul 2015 13:34:58 -0400 Subject: [PATCH 01/10] set each property as public --- android/code_generator.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index ba3aec5..f2321a9 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -261,7 +261,6 @@ bool CodeGenerator::Generate( "import com.fasterxml.jackson.databind.ObjectMapper;\n" "\n" "import java.io.IOException;\n" - "import java.io.InputStream;\n" "import java.util.List;\n" "\n"); @@ -301,12 +300,15 @@ void CodeGenerator::GenDescriptor( printer->Indent(); 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"); + 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) { @@ -368,13 +370,13 @@ void CodeGenerator::GenMessage_fromReader( const google::protobuf::Descriptor *message, google::protobuf::io::Printer *printer) { - printer->Print("public static $name$ fromReader(InputStream inputStream) {\n", + printer->Print("public static $name$ fromReader(String json) {\n", "name", message->name()); printer->Indent(); printer->Print("try {\n"); printer->Indent(); - printer->Print("return new ObjectMapper().readValue(inputStream, $name$.class);\n", + printer->Print("return new ObjectMapper().readValue(json, $name$.class);\n", "name", message->name()); printer->Outdent(); printer->Print("} catch (IOException e) {\n"); @@ -493,7 +495,7 @@ void CodeGenerator::GenMessage_toWriter( const google::protobuf::Descriptor *message, google::protobuf::io::Printer *printer) { - printer->Print("public String toWriter() {\n"); + printer->Print("public String toJson() {\n"); printer->Indent(); printer->Print("try {\n"); From a5fec01760697a7f16b46a0a0704d6747286a05f Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Mon, 20 Jul 2015 13:53:29 -0400 Subject: [PATCH 02/10] updated imports; removed default constructor --- android/code_generator.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index f2321a9..b44ba86 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -257,10 +257,12 @@ bool CodeGenerator::Generate( printer.Print( "\n" "import com.fasterxml.jackson.annotation.JsonProperty;\n" + "import com.fasterxml.jackson.annotation.JsonValue;\n" "import com.fasterxml.jackson.core.JsonProcessingException;\n" "import com.fasterxml.jackson.databind.ObjectMapper;\n" "\n" "import java.io.IOException;\n" + "import java.io.Serializable;\n" "import java.util.List;\n" "\n"); @@ -295,7 +297,7 @@ 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(); for (int i = 0; i < message->field_count(); ++i) { @@ -306,9 +308,6 @@ void CodeGenerator::GenDescriptor( } printer->Print("\n"); - 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) { From df60e750d434abe7e8b5d1d608be31653681436e Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Mon, 20 Jul 2015 13:54:35 -0400 Subject: [PATCH 03/10] removed toJson method --- android/code_generator.cc | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index b44ba86..0dbc6a1 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -329,9 +329,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"); @@ -490,26 +487,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 toJson() {\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) From 518f9504fb6996a3ef7fa85157b67d10756b1e3b Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Mon, 20 Jul 2015 14:04:45 -0400 Subject: [PATCH 04/10] fixed enums --- android/code_generator.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index 0dbc6a1..b742ef4 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -538,9 +538,19 @@ 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)); + if (i == enum_desc->value_count() - 1) { + printer->Print(";\n\n"); + } else { + printer->Print(",\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"); } From c7ccc3de287e488b0b7a3f1b5e6902ba5da34800 Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Mon, 20 Jul 2015 14:26:57 -0400 Subject: [PATCH 05/10] added serialVersionUID --- android/code_generator.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/android/code_generator.cc b/android/code_generator.cc index b742ef4..cd88684 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -300,6 +300,10 @@ void CodeGenerator::GenDescriptor( 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("public @JsonProperty(\"$name$\") $type$ $name$;\n", From 9065f5025fae2dd66d82617a0d800d56c09cd0fb Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Mon, 20 Jul 2015 15:02:58 -0400 Subject: [PATCH 06/10] fixed equals method; added empty constructor --- android/code_generator.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index cd88684..37819c2 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -257,12 +257,11 @@ bool CodeGenerator::Generate( printer.Print( "\n" "import com.fasterxml.jackson.annotation.JsonProperty;\n" - "import com.fasterxml.jackson.annotation.JsonValue;\n" - "import com.fasterxml.jackson.core.JsonProcessingException;\n" + "import com.fasterxml.jackson.annotation.JsonValue;\n" "import com.fasterxml.jackson.databind.ObjectMapper;\n" "\n" "import java.io.IOException;\n" - "import java.io.Serializable;\n" + "import java.io.Serializable;\n" "import java.util.List;\n" "\n"); @@ -312,6 +311,11 @@ void CodeGenerator::GenDescriptor( } 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) { @@ -510,8 +514,13 @@ 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()); + if (field->type() != google::protobuf::FieldDescriptor::TYPE_STRING) { + printer->Print("this.$name$ == castObject.$name$", + "name", field->camelcase_name()); + } else { + printer->Print("this.$name$.equals(castObject.$name$)", + "name", field->camelcase_name()); + } if (i != lastI) { printer->Print(" &&\n"); } From ab756fd03a06899ff72b6fa6c348dfc5cc3b4201 Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Mon, 20 Jul 2015 15:06:51 -0400 Subject: [PATCH 07/10] cleanUp --- android/code_generator.cc | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index 37819c2..4e9f5ca 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -514,13 +514,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); - if (field->type() != google::protobuf::FieldDescriptor::TYPE_STRING) { - printer->Print("this.$name$ == castObject.$name$", - "name", field->camelcase_name()); - } else { - printer->Print("this.$name$.equals(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"); } From be6fb3c228a649d543ab1b53e77da1261c78ceba Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Mon, 20 Jul 2015 15:09:32 -0400 Subject: [PATCH 08/10] cleanUp --- android/code_generator.cc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index 4e9f5ca..96cfbc1 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -548,11 +548,7 @@ void CodeGenerator::GenEnum( string number = to_string(enum_desc->value(i)->number()); printer->Print("$key$", "key", ToCamelCase(enum_desc->value(i)->name(), false)); - if (i == enum_desc->value_count() - 1) { - printer->Print(";\n\n"); - } else { - printer->Print(",\n"); - } + printer->Print(i == enum_desc->value_count() - 1 ? ";\n\n" : ",\n"); } printer->Print("@JsonValue\npublic int value() {\n"); printer->Indent(); From 8c82e129195596644b78cf45c8ec864fd7a5e987 Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Wed, 22 Jul 2015 13:29:57 -0400 Subject: [PATCH 09/10] removed fromReader method --- android/code_generator.cc | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index 96cfbc1..8c13730 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -336,9 +336,6 @@ void CodeGenerator::GenDescriptor( } printer->Outdent(); printer->Print("}\n\n"); - - CodeGenerator::GenMessage_fromReader(message, printer); - printer->Print("\n"); CodeGenerator::GenMessage_equality(message, printer); printer->Print("\n"); @@ -370,28 +367,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(String json) {\n", - "name", message->name()); - printer->Indent(); - - printer->Print("try {\n"); - printer->Indent(); - printer->Print("return new ObjectMapper().readValue(json, $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) From 1920a82f875b281d405e04b2a7900360a3e39a6e Mon Sep 17 00:00:00 2001 From: Evgeny Golomidov Date: Wed, 22 Jul 2015 13:30:45 -0400 Subject: [PATCH 10/10] removed unused import --- android/code_generator.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/android/code_generator.cc b/android/code_generator.cc index 8c13730..42bd815 100644 --- a/android/code_generator.cc +++ b/android/code_generator.cc @@ -258,9 +258,7 @@ bool CodeGenerator::Generate( "\n" "import com.fasterxml.jackson.annotation.JsonProperty;\n" "import com.fasterxml.jackson.annotation.JsonValue;\n" - "import com.fasterxml.jackson.databind.ObjectMapper;\n" "\n" - "import java.io.IOException;\n" "import java.io.Serializable;\n" "import java.util.List;\n" "\n");