diff --git a/src/google/protobuf/compiler/java/full/message.cc b/src/google/protobuf/compiler/java/full/message.cc index 89085d3e7f720..b26d8452765fb 100644 --- a/src/google/protobuf/compiler/java/full/message.cc +++ b/src/google/protobuf/compiler/java/full/message.cc @@ -843,8 +843,21 @@ void ImmutableMessageGenerator::GenerateIsInitialized(io::Printer* printer) { // TODO: Remove this in a future PBJ breaking release. printer->Print("private byte memoizedIsInitialized = -1;\n"); } else { + // If the message transitively has no required fields or extensions, + // isInitialized() is always true. + if (!HasRequiredFields(descriptor_)) { + printer->Print( + "@java.lang.Override\n" + "public final boolean isInitialized() {\n" + " return true;\n" + "}\n" + "\n"); + return; + } + printer->Print("private transient byte memoizedIsInitialized = -1;\n"); } + printer->Print( "@java.lang.Override\n" "public final boolean isInitialized() {\n");