Commit 86fdad0f authored by David Ostrovsky's avatar David Ostrovsky

Java: Generate warning free code

Partially fixes #4230.
parent c236896e
...@@ -554,6 +554,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { ...@@ -554,6 +554,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
GenerateParser(printer); GenerateParser(printer);
printer->Print( printer->Print(
"@java.lang.Override\n"
"public $classname$ getDefaultInstanceForType() {\n" "public $classname$ getDefaultInstanceForType() {\n"
" return DEFAULT_INSTANCE;\n" " return DEFAULT_INSTANCE;\n"
"}\n" "}\n"
...@@ -587,6 +588,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) { ...@@ -587,6 +588,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
std::sort(sorted_extensions.begin(), sorted_extensions.end(), std::sort(sorted_extensions.begin(), sorted_extensions.end(),
ExtensionRangeOrdering()); ExtensionRangeOrdering());
printer->Print( printer->Print(
"@java.lang.Override\n"
"public void writeTo(com.google.protobuf.CodedOutputStream output)\n" "public void writeTo(com.google.protobuf.CodedOutputStream output)\n"
" throws java.io.IOException {\n"); " throws java.io.IOException {\n");
printer->Indent(); printer->Indent();
...@@ -647,6 +649,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) { ...@@ -647,6 +649,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
printer->Print( printer->Print(
"}\n" "}\n"
"\n" "\n"
"@java.lang.Override\n"
"public int getSerializedSize() {\n" "public int getSerializedSize() {\n"
" int size = memoizedSize;\n" " int size = memoizedSize;\n"
" if (size != -1) return size;\n" " if (size != -1) return size;\n"
...@@ -788,6 +791,7 @@ void ImmutableMessageGenerator::GenerateSerializeOneExtensionRange( ...@@ -788,6 +791,7 @@ void ImmutableMessageGenerator::GenerateSerializeOneExtensionRange(
void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) { void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) {
// LITE_RUNTIME implements this at the GeneratedMessageLite level. // LITE_RUNTIME implements this at the GeneratedMessageLite level.
printer->Print( printer->Print(
"@java.lang.Override\n"
"public Builder newBuilderForType() { return newBuilder(); }\n"); "public Builder newBuilderForType() { return newBuilder(); }\n");
printer->Print( printer->Print(
...@@ -797,6 +801,7 @@ void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) { ...@@ -797,6 +801,7 @@ void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) {
"public static Builder newBuilder($classname$ prototype) {\n" "public static Builder newBuilder($classname$ prototype) {\n"
" return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n" " return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public Builder toBuilder() {\n" "public Builder toBuilder() {\n"
" return this == DEFAULT_INSTANCE\n" " return this == DEFAULT_INSTANCE\n"
" ? new Builder() : new Builder().mergeFrom(this);\n" " ? new Builder() : new Builder().mergeFrom(this);\n"
...@@ -840,6 +845,7 @@ GenerateDescriptorMethods(io::Printer* printer) { ...@@ -840,6 +845,7 @@ GenerateDescriptorMethods(io::Printer* printer) {
if (!map_fields.empty()) { if (!map_fields.empty()) {
printer->Print( printer->Print(
"@SuppressWarnings({\"rawtypes\"})\n" "@SuppressWarnings({\"rawtypes\"})\n"
"@java.lang.Override\n"
"protected com.google.protobuf.MapField internalGetMapField(\n" "protected com.google.protobuf.MapField internalGetMapField(\n"
" int number) {\n" " int number) {\n"
" switch (number) {\n"); " switch (number) {\n");
...@@ -865,6 +871,7 @@ GenerateDescriptorMethods(io::Printer* printer) { ...@@ -865,6 +871,7 @@ GenerateDescriptorMethods(io::Printer* printer) {
"}\n"); "}\n");
} }
printer->Print( printer->Print(
"@java.lang.Override\n"
"protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n"
" internalGetFieldAccessorTable() {\n" " internalGetFieldAccessorTable() {\n"
" return $fileclass$.internal_$identifier$_fieldAccessorTable\n" " return $fileclass$.internal_$identifier$_fieldAccessorTable\n"
...@@ -888,6 +895,7 @@ void ImmutableMessageGenerator::GenerateIsInitialized( ...@@ -888,6 +895,7 @@ void ImmutableMessageGenerator::GenerateIsInitialized(
printer->Print( printer->Print(
"private byte memoizedIsInitialized = -1;\n"); "private byte memoizedIsInitialized = -1;\n");
printer->Print( printer->Print(
"@java.lang.Override\n"
"public final boolean isInitialized() {\n"); "public final boolean isInitialized() {\n");
printer->Indent(); printer->Indent();
...@@ -1357,6 +1365,7 @@ void ImmutableMessageGenerator::GenerateParser(io::Printer* printer) { ...@@ -1357,6 +1365,7 @@ void ImmutableMessageGenerator::GenerateParser(io::Printer* printer) {
"classname", descriptor_->name()); "classname", descriptor_->name());
printer->Indent(); printer->Indent();
printer->Print( printer->Print(
"@java.lang.Override\n"
"public $classname$ parsePartialFrom(\n" "public $classname$ parsePartialFrom(\n"
" com.google.protobuf.CodedInputStream input,\n" " com.google.protobuf.CodedInputStream input,\n"
" com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
......
...@@ -180,11 +180,13 @@ Generate(io::Printer* printer) { ...@@ -180,11 +180,13 @@ Generate(io::Printer* printer) {
// to subclass a different GeneratedMessage class (e.g., in v3.0.0 release // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release
// we changed all generated code to subclass GeneratedMessageV3). // we changed all generated code to subclass GeneratedMessageV3).
printer->Print( printer->Print(
"@java.lang.Override\n"
"public final Builder setUnknownFields(\n" "public final Builder setUnknownFields(\n"
" final com.google.protobuf.UnknownFieldSet unknownFields) {\n" " final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
" return super.setUnknownFields$suffix$(unknownFields);\n" " return super.setUnknownFields$suffix$(unknownFields);\n"
"}\n" "}\n"
"\n" "\n"
"@java.lang.Override\n"
"public final Builder mergeUnknownFields(\n" "public final Builder mergeUnknownFields(\n"
" final com.google.protobuf.UnknownFieldSet unknownFields) {\n" " final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
" return super.mergeUnknownFields(unknownFields);\n" " return super.mergeUnknownFields(unknownFields);\n"
...@@ -277,6 +279,7 @@ GenerateDescriptorMethods(io::Printer* printer) { ...@@ -277,6 +279,7 @@ GenerateDescriptorMethods(io::Printer* printer) {
"}\n"); "}\n");
} }
printer->Print( printer->Print(
"@java.lang.Override\n"
"protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n" "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n"
" internalGetFieldAccessorTable() {\n" " internalGetFieldAccessorTable() {\n"
" return $fileclass$.internal_$identifier$_fieldAccessorTable\n" " return $fileclass$.internal_$identifier$_fieldAccessorTable\n"
...@@ -333,6 +336,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -333,6 +336,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
"}\n"); "}\n");
printer->Print( printer->Print(
"@java.lang.Override\n"
"public Builder clear() {\n" "public Builder clear() {\n"
" super.clear();\n"); " super.clear();\n");
...@@ -361,6 +365,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -361,6 +365,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
"\n"); "\n");
printer->Print( printer->Print(
"@java.lang.Override\n"
"public com.google.protobuf.Descriptors.Descriptor\n" "public com.google.protobuf.Descriptors.Descriptor\n"
" getDescriptorForType() {\n" " getDescriptorForType() {\n"
" return $fileclass$.internal_$identifier$_descriptor;\n" " return $fileclass$.internal_$identifier$_descriptor;\n"
...@@ -371,6 +376,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -371,6 +376,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
// LITE runtime implements this in GeneratedMessageLite. // LITE runtime implements this in GeneratedMessageLite.
printer->Print( printer->Print(
"@java.lang.Override\n"
"public $classname$ getDefaultInstanceForType() {\n" "public $classname$ getDefaultInstanceForType() {\n"
" return $classname$.getDefaultInstance();\n" " return $classname$.getDefaultInstance();\n"
"}\n" "}\n"
...@@ -378,6 +384,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -378,6 +384,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
"classname", name_resolver_->GetImmutableClassName(descriptor_)); "classname", name_resolver_->GetImmutableClassName(descriptor_));
printer->Print( printer->Print(
"@java.lang.Override\n"
"public $classname$ build() {\n" "public $classname$ build() {\n"
" $classname$ result = buildPartial();\n" " $classname$ result = buildPartial();\n"
" if (!result.isInitialized()) {\n" " if (!result.isInitialized()) {\n"
...@@ -389,6 +396,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -389,6 +396,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
"classname", name_resolver_->GetImmutableClassName(descriptor_)); "classname", name_resolver_->GetImmutableClassName(descriptor_));
printer->Print( printer->Print(
"@java.lang.Override\n"
"public $classname$ buildPartial() {\n" "public $classname$ buildPartial() {\n"
" $classname$ result = new $classname$(this);\n", " $classname$ result = new $classname$(this);\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_)); "classname", name_resolver_->GetImmutableClassName(descriptor_));
...@@ -457,27 +465,33 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -457,27 +465,33 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
// to subclass a different GeneratedMessage class (e.g., in v3.0.0 release // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release
// we changed all generated code to subclass GeneratedMessageV3). // we changed all generated code to subclass GeneratedMessageV3).
printer->Print( printer->Print(
"@java.lang.Override\n"
"public Builder clone() {\n" "public Builder clone() {\n"
" return (Builder) super.clone();\n" " return (Builder) super.clone();\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public Builder setField(\n" "public Builder setField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n" " com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" java.lang.Object value) {\n" " java.lang.Object value) {\n"
" return (Builder) super.setField(field, value);\n" " return (Builder) super.setField(field, value);\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public Builder clearField(\n" "public Builder clearField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field) {\n" " com.google.protobuf.Descriptors.FieldDescriptor field) {\n"
" return (Builder) super.clearField(field);\n" " return (Builder) super.clearField(field);\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public Builder clearOneof(\n" "public Builder clearOneof(\n"
" com.google.protobuf.Descriptors.OneofDescriptor oneof) {\n" " com.google.protobuf.Descriptors.OneofDescriptor oneof) {\n"
" return (Builder) super.clearOneof(oneof);\n" " return (Builder) super.clearOneof(oneof);\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public Builder setRepeatedField(\n" "public Builder setRepeatedField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n" " com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" int index, java.lang.Object value) {\n" " int index, java.lang.Object value) {\n"
" return (Builder) super.setRepeatedField(field, index, value);\n" " return (Builder) super.setRepeatedField(field, index, value);\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public Builder addRepeatedField(\n" "public Builder addRepeatedField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n" " com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" java.lang.Object value) {\n" " java.lang.Object value) {\n"
...@@ -486,24 +500,28 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -486,24 +500,28 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
if (descriptor_->extension_range_count() > 0) { if (descriptor_->extension_range_count() > 0) {
printer->Print( printer->Print(
"@java.lang.Override\n"
"public <Type> Builder setExtension(\n" "public <Type> Builder setExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, Type> extension,\n" " $classname$, Type> extension,\n"
" Type value) {\n" " Type value) {\n"
" return (Builder) super.setExtension(extension, value);\n" " return (Builder) super.setExtension(extension, value);\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public <Type> Builder setExtension(\n" "public <Type> Builder setExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, java.util.List<Type>> extension,\n" " $classname$, java.util.List<Type>> extension,\n"
" int index, Type value) {\n" " int index, Type value) {\n"
" return (Builder) super.setExtension(extension, index, value);\n" " return (Builder) super.setExtension(extension, index, value);\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public <Type> Builder addExtension(\n" "public <Type> Builder addExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, java.util.List<Type>> extension,\n" " $classname$, java.util.List<Type>> extension,\n"
" Type value) {\n" " Type value) {\n"
" return (Builder) super.addExtension(extension, value);\n" " return (Builder) super.addExtension(extension, value);\n"
"}\n" "}\n"
"@java.lang.Override\n"
"public <Type> Builder clearExtension(\n" "public <Type> Builder clearExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, ?> extension) {\n" " $classname$, ?> extension) {\n"
...@@ -516,6 +534,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -516,6 +534,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
if (context_->HasGeneratedMethods(descriptor_)) { if (context_->HasGeneratedMethods(descriptor_)) {
printer->Print( printer->Print(
"@java.lang.Override\n"
"public Builder mergeFrom(com.google.protobuf.Message other) {\n" "public Builder mergeFrom(com.google.protobuf.Message other) {\n"
" if (other instanceof $classname$) {\n" " if (other instanceof $classname$) {\n"
" return mergeFrom(($classname$)other);\n" " return mergeFrom(($classname$)other);\n"
...@@ -602,6 +621,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -602,6 +621,7 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
void MessageBuilderGenerator:: void MessageBuilderGenerator::
GenerateBuilderParsingMethods(io::Printer* printer) { GenerateBuilderParsingMethods(io::Printer* printer) {
printer->Print( printer->Print(
"@java.lang.Override\n"
"public Builder mergeFrom(\n" "public Builder mergeFrom(\n"
" com.google.protobuf.CodedInputStream input,\n" " com.google.protobuf.CodedInputStream input,\n"
" com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
...@@ -627,6 +647,7 @@ GenerateBuilderParsingMethods(io::Printer* printer) { ...@@ -627,6 +647,7 @@ GenerateBuilderParsingMethods(io::Printer* printer) {
void MessageBuilderGenerator::GenerateIsInitialized( void MessageBuilderGenerator::GenerateIsInitialized(
io::Printer* printer) { io::Printer* printer) {
printer->Print( printer->Print(
"@java.lang.Override\n"
"public final boolean isInitialized() {\n"); "public final boolean isInitialized() {\n");
printer->Indent(); printer->Indent();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment