Generate "modern" java to reduce warnings
* @SuppressWarnings("hiding") for field names that are reused in inner
classes.
* @Override for methods defined in com.google.protobuf.micro.MessageMicro
* Removed unnecessary type casting
* Only throw exception from writeTo when there are fields to write.
This removes over 1,000 warnings from the Play client.
Change-Id: I9049c94f1e6aec5e5547898defc03c8d379c3c10
diff --git a/src/google/protobuf/compiler/javamicro/javamicro_message.cc b/src/google/protobuf/compiler/javamicro/javamicro_message.cc
index 7fc6c3d..594203a 100644
--- a/src/google/protobuf/compiler/javamicro/javamicro_message.cc
+++ b/src/google/protobuf/compiler/javamicro/javamicro_message.cc
@@ -186,7 +186,11 @@
GOOGLE_LOG(FATAL) << "Extensions not supported in MICRO_RUNTIME\n";
}
+ // Note: Fields (which will be emitted in the loop, below) may have the same names as fields in
+ // the inner or outer class. This causes Java warnings, but is not fatal, so we suppress those
+ // warnings here in the class declaration.
printer->Print(
+ "@SuppressWarnings(\"hiding\")\n"
"public $modifiers$ final class $classname$ extends\n"
" com.google.protobuf.micro.MessageMicro {\n",
"modifiers", is_own_file ? "" : "static",
@@ -237,9 +241,17 @@
GOOGLE_LOG(FATAL) << "Extensions not supported in MICRO_RUNTIME\n";
}
- printer->Print(
- "public void writeTo(com.google.protobuf.micro.CodedOutputStreamMicro output)\n"
- " throws java.io.IOException {\n");
+ // writeTo only throws an exception if it contains one or more fields to write
+ if (descriptor_->field_count() > 0) {
+ printer->Print(
+ "@Override\n"
+ "public void writeTo(com.google.protobuf.micro.CodedOutputStreamMicro output)\n"
+ " throws java.io.IOException {\n");
+ } else {
+ printer->Print(
+ "@Override\n"
+ "public void writeTo(com.google.protobuf.micro.CodedOutputStreamMicro output) {\n");
+ }
printer->Indent();
// Output the fields in sorted order
@@ -252,6 +264,7 @@
"}\n"
"\n"
"private int cachedSize = -1;\n"
+ "@Override\n"
"public int getCachedSize() {\n"
" if (cachedSize < 0) {\n"
" // getSerializedSize sets cachedSize\n"
@@ -260,6 +273,7 @@
" return cachedSize;\n"
"}\n"
"\n"
+ "@Override\n"
"public int getSerializedSize() {\n"
" int size = 0;\n");
printer->Indent();
@@ -282,12 +296,14 @@
if (params_.java_use_vector()) {
printer->Print(
+ "@Override\n"
"public com.google.protobuf.micro.MessageMicro mergeFrom(\n"
" com.google.protobuf.micro.CodedInputStreamMicro input)\n"
" throws java.io.IOException {\n",
"classname", descriptor_->name());
} else {
printer->Print(
+ "@Override\n"
"public $classname$ mergeFrom(\n"
" com.google.protobuf.micro.CodedInputStreamMicro input)\n"
" throws java.io.IOException {\n",
@@ -359,7 +375,7 @@
"public $static$ $classname$ parseFrom(\n"
" com.google.protobuf.micro.CodedInputStreamMicro input)\n"
" throws java.io.IOException {\n"
- " return ($classname$) (new $classname$().mergeFrom(input));\n"
+ " return new $classname$().mergeFrom(input);\n"
"}\n"
"\n",
"static", (is_own_file ? "static" : ""),