Refactored the Java generator into separate packages and targets.

PiperOrigin-RevId: 626057584
pull/16501/head
Joshua Haberman 2024-04-18 09:22:50 -07:00 committed by Copybara-Service
parent 1d6fdc1342
commit 8e3e09937e
64 changed files with 1347 additions and 839 deletions

View File

@ -198,6 +198,7 @@ cc_dist_library(
"//src/google/protobuf/compiler/cpp",
"//src/google/protobuf/compiler/csharp",
"//src/google/protobuf/compiler/java",
"//src/google/protobuf/compiler/java:kotlin",
"//src/google/protobuf/compiler/objectivec",
"//src/google/protobuf/compiler/php",
"//src/google/protobuf/compiler/python",

View File

@ -195,6 +195,7 @@ cc_library(
"//src/google/protobuf/compiler/cpp",
"//src/google/protobuf/compiler/csharp",
"//src/google/protobuf/compiler/java",
"//src/google/protobuf/compiler/java:kotlin",
"//src/google/protobuf/compiler/objectivec",
"//src/google/protobuf/compiler/php",
"//src/google/protobuf/compiler/python",

View File

@ -6,6 +6,12 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("//build_defs:cpp_opts.bzl", "COPTS")
package(
default_visibility = [
"//src/google/protobuf/compiler/java:__subpackages__",
],
)
cc_library(
name = "names",
hdrs = ["names.h"],
@ -13,19 +19,22 @@ cc_library(
strip_include_prefix = "/src",
visibility = ["//visibility:public"],
deps = [
":names_internal",
":helpers",
"//src/google/protobuf",
],
)
cc_library(
name = "names_internal",
name = "helpers",
srcs = [
"doc_comment.cc",
"helpers.cc",
"name_resolver.cc",
"names.cc",
],
hdrs = [
"context.h",
"doc_comment.h",
"generator.h",
"helpers.h",
"name_resolver.h",
@ -34,7 +43,10 @@ cc_library(
],
copts = COPTS,
strip_include_prefix = "/src",
visibility = ["//pkg:__pkg__"],
visibility = [
"//pkg:__pkg__",
"//src/google/protobuf/compiler/java:__subpackages__",
],
deps = [
":java_features_bootstrap",
"//src/google/protobuf",
@ -70,64 +82,16 @@ cc_library(
name = "java",
srcs = [
"context.cc",
"doc_comment.cc",
"enum.cc",
"enum_field.cc",
"enum_field_lite.cc",
"enum_lite.cc",
"extension.cc",
"extension_lite.cc",
"field.cc",
"file.cc",
"generator.cc",
"generator_factory.cc",
"kotlin_generator.cc",
"map_field.cc",
"map_field_lite.cc",
"message.cc",
"message_builder.cc",
"message_builder_lite.cc",
"message_field.cc",
"message_field_lite.cc",
"message_lite.cc",
"message_serialization.cc",
"primitive_field.cc",
"primitive_field_lite.cc",
"service.cc",
"shared_code_generator.cc",
"string_field.cc",
"string_field_lite.cc",
],
hdrs = [
"context.h",
"doc_comment.h",
"enum.h",
"enum_field.h",
"enum_field_lite.h",
"enum_lite.h",
"extension.h",
"extension_lite.h",
"field.h",
"file.h",
"generator.h",
"generator_factory.h",
"java_features.pb.h",
"kotlin_generator.h",
"map_field.h",
"map_field_lite.h",
"message.h",
"message_builder.h",
"message_builder_lite.h",
"message_field.h",
"message_field_lite.h",
"message_lite.h",
"message_serialization.h",
"primitive_field.h",
"primitive_field_lite.h",
"service.h",
"shared_code_generator.h",
"string_field.h",
"string_field_lite.h",
],
copts = COPTS,
strip_include_prefix = "/src",
@ -136,9 +100,10 @@ cc_library(
"//src/google/protobuf/compiler:__pkg__",
],
deps = [
":generator_common",
":helpers",
":java_features_bootstrap",
":names",
":names_internal",
"//src/google/protobuf",
"//src/google/protobuf:arena",
"//src/google/protobuf:port",
@ -146,6 +111,8 @@ cc_library(
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:retention",
"//src/google/protobuf/compiler:versions",
"//src/google/protobuf/compiler/java/immutable",
"//src/google/protobuf/compiler/java/lite",
"//src/google/protobuf/io",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/container:btree",
@ -158,6 +125,62 @@ cc_library(
],
)
cc_library(
name = "generator_common",
srcs = ["field_common.cc"],
hdrs = [
"field_common.h",
"generator_common.h",
"generator_factory.h",
],
strip_include_prefix = "/src",
visibility = ["//src/google/protobuf/compiler/java:__subpackages__"],
deps = [
":helpers",
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/strings:string_view",
],
)
cc_library(
name = "message_serialization",
srcs = ["message_serialization.cc"],
hdrs = ["message_serialization.h"],
strip_include_prefix = "/src",
deps = [
":generator_common",
":helpers",
"//src/google/protobuf",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)
cc_library(
name = "kotlin",
srcs = ["kotlin_generator.cc"],
hdrs = ["kotlin_generator.h"],
strip_include_prefix = "/src",
visibility = [
"//pkg:__pkg__",
"//src/google/protobuf/compiler:__pkg__",
],
deps = [
":helpers",
":java",
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler:code_generator",
],
)
cc_test(
name = "doc_comment_unittest",
srcs = ["doc_comment_unittest.cc"],
@ -167,7 +190,7 @@ cc_test(
"//src/google/protobuf:descriptor_proto_srcs",
],
deps = [
":java",
":helpers",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
@ -178,7 +201,7 @@ cc_test(
srcs = ["plugin_unittest.cc"],
deps = [
":java",
"//:protobuf",
"//src/google/protobuf",
"//src/google/protobuf/compiler:command_line_interface",
"//src/google/protobuf/io",
"//src/google/protobuf/io:printer",

View File

@ -13,7 +13,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.h"

View File

@ -1,294 +0,0 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/field.h"
#include <memory>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/enum_field.h"
#include "google/protobuf/compiler/java/enum_field_lite.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/map_field.h"
#include "google/protobuf/compiler/java/map_field_lite.h"
#include "google/protobuf/compiler/java/message_field.h"
#include "google/protobuf/compiler/java/message_field_lite.h"
#include "google/protobuf/compiler/java/primitive_field.h"
#include "google/protobuf/compiler/java/primitive_field_lite.h"
#include "google/protobuf/compiler/java/string_field.h"
#include "google/protobuf/compiler/java/string_field_lite.h"
#include "google/protobuf/io/printer.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
namespace {
ImmutableFieldGenerator* MakeImmutableGenerator(const FieldDescriptor* field,
int messageBitIndex,
int builderBitIndex,
Context* context) {
if (field->is_repeated()) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
if (IsMapEntry(field->message_type())) {
return new ImmutableMapFieldGenerator(field, messageBitIndex,
builderBitIndex, context);
} else {
return new RepeatedImmutableMessageFieldGenerator(
field, messageBitIndex, builderBitIndex, context);
}
case JAVATYPE_ENUM:
return new RepeatedImmutableEnumFieldGenerator(
field, messageBitIndex, builderBitIndex, context);
case JAVATYPE_STRING:
return new RepeatedImmutableStringFieldGenerator(
field, messageBitIndex, builderBitIndex, context);
default:
return new RepeatedImmutablePrimitiveFieldGenerator(
field, messageBitIndex, builderBitIndex, context);
}
} else {
if (IsRealOneof(field)) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
return new ImmutableMessageOneofFieldGenerator(
field, messageBitIndex, builderBitIndex, context);
case JAVATYPE_ENUM:
return new ImmutableEnumOneofFieldGenerator(field, messageBitIndex,
builderBitIndex, context);
case JAVATYPE_STRING:
return new ImmutableStringOneofFieldGenerator(
field, messageBitIndex, builderBitIndex, context);
default:
return new ImmutablePrimitiveOneofFieldGenerator(
field, messageBitIndex, builderBitIndex, context);
}
} else {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
return new ImmutableMessageFieldGenerator(field, messageBitIndex,
builderBitIndex, context);
case JAVATYPE_ENUM:
return new ImmutableEnumFieldGenerator(field, messageBitIndex,
builderBitIndex, context);
case JAVATYPE_STRING:
return new ImmutableStringFieldGenerator(field, messageBitIndex,
builderBitIndex, context);
default:
return new ImmutablePrimitiveFieldGenerator(field, messageBitIndex,
builderBitIndex, context);
}
}
}
}
ImmutableFieldLiteGenerator* MakeImmutableLiteGenerator(
const FieldDescriptor* field, int messageBitIndex, Context* context) {
if (field->is_repeated()) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
if (IsMapEntry(field->message_type())) {
return new ImmutableMapFieldLiteGenerator(field, messageBitIndex,
context);
} else {
return new RepeatedImmutableMessageFieldLiteGenerator(
field, messageBitIndex, context);
}
case JAVATYPE_ENUM:
return new RepeatedImmutableEnumFieldLiteGenerator(
field, messageBitIndex, context);
case JAVATYPE_STRING:
return new RepeatedImmutableStringFieldLiteGenerator(
field, messageBitIndex, context);
default:
return new RepeatedImmutablePrimitiveFieldLiteGenerator(
field, messageBitIndex, context);
}
} else {
if (IsRealOneof(field)) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
return new ImmutableMessageOneofFieldLiteGenerator(
field, messageBitIndex, context);
case JAVATYPE_ENUM:
return new ImmutableEnumOneofFieldLiteGenerator(
field, messageBitIndex, context);
case JAVATYPE_STRING:
return new ImmutableStringOneofFieldLiteGenerator(
field, messageBitIndex, context);
default:
return new ImmutablePrimitiveOneofFieldLiteGenerator(
field, messageBitIndex, context);
}
} else {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
return new ImmutableMessageFieldLiteGenerator(field, messageBitIndex,
context);
case JAVATYPE_ENUM:
return new ImmutableEnumFieldLiteGenerator(field, messageBitIndex,
context);
case JAVATYPE_STRING:
return new ImmutableStringFieldLiteGenerator(field, messageBitIndex,
context);
default:
return new ImmutablePrimitiveFieldLiteGenerator(
field, messageBitIndex, context);
}
}
}
}
static inline void ReportUnexpectedPackedFieldsCall(io::Printer* printer) {
// Reaching here indicates a bug. Cases are:
// - This FieldGenerator should support packing,
// but this method should be overridden.
// - This FieldGenerator doesn't support packing, and this method
// should never have been called.
ABSL_LOG(FATAL) << "GenerateBuilderParsingCodeFromPacked() "
<< "called on field generator that does not support packing.";
}
} // namespace
ImmutableFieldGenerator::~ImmutableFieldGenerator() {}
void ImmutableFieldGenerator::GenerateBuilderParsingCodeFromPacked(
io::Printer* printer) const {
ReportUnexpectedPackedFieldsCall(printer);
}
ImmutableFieldLiteGenerator::~ImmutableFieldLiteGenerator() {}
// ===================================================================
template <>
FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap(
const Descriptor* descriptor, Context* context)
: descriptor_(descriptor), field_generators_(descriptor->field_count()) {
// Construct all the FieldGenerators and assign them bit indices for their
// bit fields.
int messageBitIndex = 0;
int builderBitIndex = 0;
for (int i = 0; i < descriptor->field_count(); i++) {
ImmutableFieldGenerator* generator = MakeImmutableGenerator(
descriptor->field(i), messageBitIndex, builderBitIndex, context);
field_generators_[i].reset(generator);
messageBitIndex += generator->GetNumBitsForMessage();
builderBitIndex += generator->GetNumBitsForBuilder();
}
}
template <>
FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap() {}
template <>
FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap(
const Descriptor* descriptor, Context* context)
: descriptor_(descriptor), field_generators_(descriptor->field_count()) {
// Construct all the FieldGenerators and assign them bit indices for their
// bit fields.
int messageBitIndex = 0;
for (int i = 0; i < descriptor->field_count(); i++) {
ImmutableFieldLiteGenerator* generator = MakeImmutableLiteGenerator(
descriptor->field(i), messageBitIndex, context);
field_generators_[i].reset(generator);
messageBitIndex += generator->GetNumBitsForMessage();
}
}
template <>
FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap() {}
void SetCommonFieldVariables(
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables) {
(*variables)["field_name"] = descriptor->name();
(*variables)["name"] = info->name;
(*variables)["classname"] = descriptor->containing_type()->name();
(*variables)["capitalized_name"] = info->capitalized_name;
(*variables)["disambiguated_reason"] = info->disambiguated_reason;
(*variables)["constant_name"] = FieldConstantName(descriptor);
(*variables)["number"] = absl::StrCat(descriptor->number());
(*variables)["kt_dsl_builder"] = "_builder";
// These variables are placeholders to pick out the beginning and ends of
// identifiers for annotations (when doing so with existing variables would
// be ambiguous or impossible). They should never be set to anything but the
// empty string.
(*variables)["{"] = "";
(*variables)["}"] = "";
(*variables)["kt_name"] = IsForbiddenKotlin(info->name)
? absl::StrCat(info->name, "_")
: info->name;
(*variables)["kt_capitalized_name"] =
IsForbiddenKotlin(info->name) ? absl::StrCat(info->capitalized_name, "_")
: info->capitalized_name;
if (!descriptor->is_repeated()) {
(*variables)["annotation_field_type"] =
std::string(FieldTypeName(descriptor->type()));
} else if (GetJavaType(descriptor) == JAVATYPE_MESSAGE &&
IsMapEntry(descriptor->message_type())) {
(*variables)["annotation_field_type"] =
absl::StrCat(FieldTypeName(descriptor->type()), "MAP");
} else {
(*variables)["annotation_field_type"] =
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST");
if (descriptor->is_packed()) {
variables->insert(
{"annotation_field_type",
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST_PACKED")});
}
}
}
void SetCommonOneofVariables(
const FieldDescriptor* descriptor, const OneofGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables) {
(*variables)["oneof_name"] = info->name;
(*variables)["oneof_capitalized_name"] = info->capitalized_name;
(*variables)["oneof_index"] =
absl::StrCat(descriptor->containing_oneof()->index());
(*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor);
(*variables)["set_oneof_case_message"] =
absl::StrCat(info->name, "Case_ = ", descriptor->number());
(*variables)["clear_oneof_case_message"] =
absl::StrCat(info->name, "Case_ = 0");
(*variables)["has_oneof_case_message"] =
absl::StrCat(info->name, "Case_ == ", descriptor->number());
}
void PrintExtraFieldInfo(
const absl::flat_hash_map<absl::string_view, std::string>& variables,
io::Printer* printer) {
auto it = variables.find("disambiguated_reason");
if (it != variables.end() && !it->second.empty()) {
printer->Print(
variables,
"// An alternative name is used for field \"$field_name$\" because:\n"
"// $disambiguated_reason$\n");
}
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google

View File

@ -1,169 +0,0 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
#include <cstdint>
#include <memory>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_check.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/port.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
class Context; // context.h
class ClassNameResolver; // name_resolver.h
} // namespace java
} // namespace compiler
namespace io {
class Printer; // printer.h
}
} // namespace protobuf
} // namespace google
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
class ImmutableFieldGenerator {
public:
ImmutableFieldGenerator() {}
ImmutableFieldGenerator(const ImmutableFieldGenerator&) = delete;
ImmutableFieldGenerator& operator=(const ImmutableFieldGenerator&) = delete;
virtual ~ImmutableFieldGenerator();
virtual int GetMessageBitIndex() const = 0;
virtual int GetBuilderBitIndex() const = 0;
virtual int GetNumBitsForMessage() const = 0;
virtual int GetNumBitsForBuilder() const = 0;
virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
virtual void GenerateMembers(io::Printer* printer) const = 0;
virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0;
virtual void GenerateMergingCode(io::Printer* printer) const = 0;
virtual void GenerateBuildingCode(io::Printer* printer) const = 0;
virtual void GenerateBuilderParsingCode(io::Printer* printer) const = 0;
virtual void GenerateBuilderParsingCodeFromPacked(io::Printer* printer) const;
virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
virtual void GenerateFieldBuilderInitializationCode(
io::Printer* printer) const = 0;
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
virtual void GenerateEqualsCode(io::Printer* printer) const = 0;
virtual void GenerateHashCode(io::Printer* printer) const = 0;
virtual std::string GetBoxedType() const = 0;
};
class ImmutableFieldLiteGenerator {
public:
ImmutableFieldLiteGenerator() {}
ImmutableFieldLiteGenerator(const ImmutableFieldLiteGenerator&) = delete;
ImmutableFieldLiteGenerator& operator=(const ImmutableFieldLiteGenerator&) =
delete;
virtual ~ImmutableFieldLiteGenerator();
virtual int GetNumBitsForMessage() const = 0;
virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
virtual void GenerateMembers(io::Printer* printer) const = 0;
virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
virtual void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const = 0;
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
virtual std::string GetBoxedType() const = 0;
};
// Convenience class which constructs FieldGenerators for a Descriptor.
template <typename FieldGeneratorType>
class FieldGeneratorMap {
public:
explicit FieldGeneratorMap(const Descriptor* descriptor, Context* context);
FieldGeneratorMap(const FieldGeneratorMap&) = delete;
FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete;
~FieldGeneratorMap();
const FieldGeneratorType& get(const FieldDescriptor* field) const;
private:
const Descriptor* descriptor_;
std::vector<std::unique_ptr<FieldGeneratorType>> field_generators_;
};
template <typename FieldGeneratorType>
inline const FieldGeneratorType& FieldGeneratorMap<FieldGeneratorType>::get(
const FieldDescriptor* field) const {
ABSL_CHECK_EQ(field->containing_type(), descriptor_);
return *field_generators_[field->index()];
}
// Instantiate template for mutable and immutable maps.
template <>
FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap(
const Descriptor* descriptor, Context* context);
template <>
FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap();
template <>
FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap(
const Descriptor* descriptor, Context* context);
template <>
FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap();
// Field information used in FieldGenerators.
struct FieldGeneratorInfo {
std::string name;
std::string capitalized_name;
std::string disambiguated_reason;
};
// Oneof information used in OneofFieldGenerators.
struct OneofGeneratorInfo {
std::string name;
std::string capitalized_name;
};
// Set some common variables used in variable FieldGenerators.
void SetCommonFieldVariables(
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables);
// Set some common oneof variables used in OneofFieldGenerators.
void SetCommonOneofVariables(
const FieldDescriptor* descriptor, const OneofGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables);
// Print useful comments before a field's accessors.
void PrintExtraFieldInfo(
const absl::flat_hash_map<absl::string_view, std::string>& variables,
io::Printer* printer);
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__

View File

@ -0,0 +1,85 @@
#include "google/protobuf/compiler/java/field_common.h"
#include <string>
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
void SetCommonFieldVariables(
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables) {
(*variables)["field_name"] = descriptor->name();
(*variables)["name"] = info->name;
(*variables)["classname"] = descriptor->containing_type()->name();
(*variables)["capitalized_name"] = info->capitalized_name;
(*variables)["disambiguated_reason"] = info->disambiguated_reason;
(*variables)["constant_name"] = FieldConstantName(descriptor);
(*variables)["number"] = absl::StrCat(descriptor->number());
(*variables)["kt_dsl_builder"] = "_builder";
// These variables are placeholders to pick out the beginning and ends of
// identifiers for annotations (when doing so with existing variables would
// be ambiguous or impossible). They should never be set to anything but the
// empty string.
(*variables)["{"] = "";
(*variables)["}"] = "";
(*variables)["kt_name"] = IsForbiddenKotlin(info->name)
? absl::StrCat(info->name, "_")
: info->name;
(*variables)["kt_capitalized_name"] =
IsForbiddenKotlin(info->name) ? absl::StrCat(info->capitalized_name, "_")
: info->capitalized_name;
if (!descriptor->is_repeated()) {
(*variables)["annotation_field_type"] =
std::string(FieldTypeName(descriptor->type()));
} else if (GetJavaType(descriptor) == JAVATYPE_MESSAGE &&
IsMapEntry(descriptor->message_type())) {
(*variables)["annotation_field_type"] =
absl::StrCat(FieldTypeName(descriptor->type()), "MAP");
} else {
(*variables)["annotation_field_type"] =
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST");
if (descriptor->is_packed()) {
variables->insert(
{"annotation_field_type",
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST_PACKED")});
}
}
}
void SetCommonOneofVariables(
const FieldDescriptor* descriptor, const OneofGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables) {
(*variables)["oneof_name"] = info->name;
(*variables)["oneof_capitalized_name"] = info->capitalized_name;
(*variables)["oneof_index"] =
absl::StrCat(descriptor->containing_oneof()->index());
(*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor);
(*variables)["set_oneof_case_message"] =
absl::StrCat(info->name, "Case_ = ", descriptor->number());
(*variables)["clear_oneof_case_message"] =
absl::StrCat(info->name, "Case_ = 0");
(*variables)["has_oneof_case_message"] =
absl::StrCat(info->name, "Case_ == ", descriptor->number());
}
void PrintExtraFieldInfo(
const absl::flat_hash_map<absl::string_view, std::string>& variables,
io::Printer* printer) {
auto it = variables.find("disambiguated_reason");
if (it != variables.end() && !it->second.empty()) {
printer->Print(
variables,
"// An alternative name is used for field \"$field_name$\" because:\n"
"// $disambiguated_reason$\n");
}
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google

View File

@ -0,0 +1,44 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
// Field information used in FieldGenerators.
struct FieldGeneratorInfo {
std::string name;
std::string capitalized_name;
std::string disambiguated_reason;
};
// Oneof information used in OneofFieldGenerators.
struct OneofGeneratorInfo {
std::string name;
std::string capitalized_name;
};
// Set some common variables used in variable FieldGenerators.
void SetCommonFieldVariables(
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables);
// Set some common oneof variables used in OneofFieldGenerators.
void SetCommonOneofVariables(
const FieldDescriptor* descriptor, const OneofGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables);
// Print useful comments before a field's accessors.
void PrintExtraFieldInfo(
const absl::flat_hash_map<absl::string_view, std::string>& variables,
io::Printer* printer);
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__

View File

@ -12,22 +12,23 @@
#include "google/protobuf/compiler/java/file.h"
#include <memory>
#include <string>
#include <vector>
#include "absl/container/btree_set.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/enum.h"
#include "google/protobuf/compiler/java/enum_lite.h"
#include "google/protobuf/compiler/java/extension.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/message.h"
#include "google/protobuf/compiler/java/immutable/generator_factory.h"
#include "google/protobuf/compiler/java/lite/generator_factory.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/compiler/java/service.h"
#include "google/protobuf/compiler/java/options.h"
#include "google/protobuf/compiler/java/shared_code_generator.h"
#include "google/protobuf/compiler/retention.h"
#include "google/protobuf/compiler/versions.h"
@ -36,6 +37,7 @@
#include "google/protobuf/io/printer.h"
#include "google/protobuf/io/zero_copy_stream.h"
// Must be last.
#include "google/protobuf/port_def.inc"
@ -165,6 +167,18 @@ void MaybeRestartJavaMethod(io::Printer* printer, int* bytecode_estimate,
*bytecode_estimate = 0;
}
}
std::unique_ptr<GeneratorFactory> CreateGeneratorFactory(
const FileDescriptor* file, const Options& options, Context* context,
bool immutable_api) {
ABSL_CHECK(immutable_api)
<< "Open source release does not support the mutable API";
if (HasDescriptorMethods(file, context->EnforceLite())) {
return MakeImmutableGeneratorFactory(context);
} else {
return MakeImmutableLiteGeneratorFactory(context);
}
}
} // namespace
FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options,
@ -174,18 +188,19 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options,
message_generators_(file->message_type_count()),
extension_generators_(file->extension_count()),
context_(new Context(file, options)),
generator_factory_(
CreateGeneratorFactory(file, options, context_.get(), immutable_api)),
name_resolver_(context_->GetNameResolver()),
options_(options),
immutable_api_(immutable_api) {
classname_ = name_resolver_->GetFileClassName(file, immutable_api);
generator_factory_.reset(new ImmutableGeneratorFactory(context_.get()));
for (int i = 0; i < file_->message_type_count(); ++i) {
message_generators_[i].reset(
generator_factory_->NewMessageGenerator(file_->message_type(i)));
message_generators_[i] =
generator_factory_->NewMessageGenerator(file_->message_type(i));
}
for (int i = 0; i < file_->extension_count(); ++i) {
extension_generators_[i].reset(
generator_factory_->NewExtensionGenerator(file_->extension(i)));
extension_generators_[i] =
generator_factory_->NewExtensionGenerator(file_->extension(i));
}
}
@ -322,13 +337,8 @@ void FileGenerator::Generate(io::Printer* printer) {
if (!MultipleJavaFiles(file_, immutable_api_)) {
for (int i = 0; i < file_->enum_type_count(); i++) {
if (HasDescriptorMethods(file_, context_->EnforceLite())) {
EnumGenerator(file_->enum_type(i), immutable_api_, context_.get())
.Generate(printer);
} else {
EnumLiteGenerator(file_->enum_type(i), immutable_api_, context_.get())
.Generate(printer);
}
generator_factory_->NewEnumGenerator(file_->enum_type(i))
->Generate(printer);
}
for (int i = 0; i < file_->message_type_count(); i++) {
message_generators_[i]->GenerateInterface(printer);
@ -556,21 +566,12 @@ void FileGenerator::GenerateSiblings(
std::vector<std::string>* annotation_list) {
if (MultipleJavaFiles(file_, immutable_api_)) {
for (int i = 0; i < file_->enum_type_count(); i++) {
if (HasDescriptorMethods(file_, context_->EnforceLite())) {
EnumGenerator generator(file_->enum_type(i), immutable_api_,
context_.get());
GenerateSibling<EnumGenerator>(
package_dir, java_package_, file_->enum_type(i), context, file_list,
options_.annotate_code, annotation_list, "", &generator,
options_.opensource_runtime, &EnumGenerator::Generate);
} else {
EnumLiteGenerator generator(file_->enum_type(i), immutable_api_,
context_.get());
GenerateSibling<EnumLiteGenerator>(
package_dir, java_package_, file_->enum_type(i), context, file_list,
options_.annotate_code, annotation_list, "", &generator,
options_.opensource_runtime, &EnumLiteGenerator::Generate);
}
std::unique_ptr<EnumGenerator> generator(
generator_factory_->NewEnumGenerator(file_->enum_type(i)));
GenerateSibling<EnumGenerator>(
package_dir, java_package_, file_->enum_type(i), context, file_list,
options_.annotate_code, annotation_list, "", generator.get(),
options_.opensource_runtime, &EnumGenerator::Generate);
}
for (int i = 0; i < file_->message_type_count(); i++) {
if (immutable_api_) {

View File

@ -88,8 +88,8 @@ class FileGenerator {
std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
std::unique_ptr<GeneratorFactory> generator_factory_;
std::unique_ptr<Context> context_;
std::unique_ptr<GeneratorFactory> generator_factory_;
ClassNameResolver* name_resolver_;
const Options options_;
bool immutable_api_;

View File

@ -0,0 +1,89 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__
#include <cstddef>
#include <memory>
#include <vector>
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
static const int kMaxStaticSize = 1 << 15; // aka 32k
class FieldGenerator {
public:
virtual ~FieldGenerator() = default;
virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
};
// Convenience class which constructs FieldGenerators for a Descriptor.
template <typename FieldGeneratorType>
class FieldGeneratorMap {
public:
explicit FieldGeneratorMap(const Descriptor* descriptor)
: descriptor_(descriptor) {
field_generators_.reserve(static_cast<size_t>(descriptor->field_count()));
}
~FieldGeneratorMap() {
for (const auto* g : field_generators_) {
delete g;
}
}
FieldGeneratorMap(FieldGeneratorMap&&) = default;
FieldGeneratorMap& operator=(FieldGeneratorMap&&) = default;
FieldGeneratorMap(const FieldGeneratorMap&) = delete;
FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete;
void Add(const FieldDescriptor* field,
std::unique_ptr<FieldGeneratorType> field_generator) {
ABSL_CHECK_EQ(field->containing_type(), descriptor_);
field_generators_.push_back(field_generator.release());
}
const FieldGeneratorType& get(const FieldDescriptor* field) const {
ABSL_CHECK_EQ(field->containing_type(), descriptor_);
return *field_generators_[static_cast<size_t>(field->index())];
}
std::vector<const FieldGenerator*> field_generators() const {
std::vector<const FieldGenerator*> field_generators;
field_generators.reserve(field_generators_.size());
for (const auto* g : field_generators_) {
field_generators.push_back(g);
}
return field_generators;
}
private:
const Descriptor* descriptor_;
std::vector<const FieldGeneratorType*> field_generators_;
};
inline void ReportUnexpectedPackedFieldsCall() {
// Reaching here indicates a bug. Cases are:
// - This FieldGenerator should support packing,
// but this method should be overridden.
// - This FieldGenerator doesn't support packing, and this method
// should never have been called.
ABSL_LOG(FATAL) << "GenerateBuilderParsingCodeFromPacked() "
<< "called on field generator that does not support packing.";
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__

View File

@ -1,63 +0,0 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: liujisi@google.com (Pherl Liu)
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/enum_field.h"
#include "google/protobuf/compiler/java/extension.h"
#include "google/protobuf/compiler/java/extension_lite.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/message.h"
#include "google/protobuf/compiler/java/message_lite.h"
#include "google/protobuf/compiler/java/service.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
GeneratorFactory::GeneratorFactory() {}
GeneratorFactory::~GeneratorFactory() {}
// ===================================================================
ImmutableGeneratorFactory::ImmutableGeneratorFactory(Context* context)
: context_(context) {}
ImmutableGeneratorFactory::~ImmutableGeneratorFactory() {}
MessageGenerator* ImmutableGeneratorFactory::NewMessageGenerator(
const Descriptor* descriptor) const {
if (HasDescriptorMethods(descriptor, context_->EnforceLite())) {
return new ImmutableMessageGenerator(descriptor, context_);
} else {
return new ImmutableMessageLiteGenerator(descriptor, context_);
}
}
ExtensionGenerator* ImmutableGeneratorFactory::NewExtensionGenerator(
const FieldDescriptor* descriptor) const {
if (HasDescriptorMethods(descriptor->file(), context_->EnforceLite())) {
return new ImmutableExtensionGenerator(descriptor, context_);
} else {
return new ImmutableExtensionLiteGenerator(descriptor, context_);
}
}
ServiceGenerator* ImmutableGeneratorFactory::NewServiceGenerator(
const ServiceDescriptor* descriptor) const {
return new ImmutableServiceGenerator(descriptor, context_);
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google

View File

@ -10,69 +10,124 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__
#include "google/protobuf/port.h"
#include <memory>
#include <string>
namespace google {
namespace protobuf {
class FieldDescriptor; // descriptor.h
class Descriptor; // descriptor.h
class ServiceDescriptor; // descriptor.h
namespace compiler {
namespace java {
class MessageGenerator; // message.h
class ExtensionGenerator; // extension.h
class ServiceGenerator; // service.h
class Context; // context.h
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
class MessageGenerator {
public:
explicit MessageGenerator(const Descriptor* descriptor);
MessageGenerator(const MessageGenerator&) = delete;
MessageGenerator& operator=(const MessageGenerator&) = delete;
virtual ~MessageGenerator();
// All static variables have to be declared at the top-level of the file
// so that we can control initialization order, which is important for
// DescriptorProto bootstrapping to work.
virtual void GenerateStaticVariables(io::Printer* printer,
int* bytecode_estimate) = 0;
// Output code which initializes the static variables generated by
// GenerateStaticVariables(). Returns an estimate of bytecode size.
virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0;
// Generate the class itself.
virtual void Generate(io::Printer* printer) = 0;
// Generates the base interface that both the class and its builder
// implement
virtual void GenerateInterface(io::Printer* printer) = 0;
// Generate code to register all contained extensions with an
// ExtensionRegistry.
virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0;
virtual void GenerateKotlinDsl(io::Printer* printer) const = 0;
virtual void GenerateKotlinMembers(io::Printer* printer) const = 0;
virtual void GenerateTopLevelKotlinMembers(io::Printer* printer) const = 0;
protected:
const Descriptor* descriptor_;
absl::btree_map<int, const OneofDescriptor*> oneofs_;
};
class EnumGenerator {
public:
virtual ~EnumGenerator() = default;
virtual void Generate(io::Printer* printer) = 0;
};
// Generates code for an extension, which may be within the scope of some
// message or may be at file scope. This is much simpler than FieldGenerator
// since extensions are just simple identifiers with interesting types.
class ExtensionGenerator {
public:
virtual ~ExtensionGenerator() = default;
virtual void Generate(io::Printer* printer) = 0;
// Returns an estimate of the number of bytes the printed code will compile
// to
virtual int GenerateNonNestedInitializationCode(io::Printer* printer) = 0;
// Returns an estimate of the number of bytes the printed code will compile
// to
virtual int GenerateRegistrationCode(io::Printer* printer) = 0;
protected:
static void InitTemplateVars(
const FieldDescriptor* descriptor, const std::string& scope,
bool immutable, ClassNameResolver* name_resolver,
absl::flat_hash_map<absl::string_view, std::string>* vars_pointer,
Context* context);
};
class ServiceGenerator {
public:
explicit ServiceGenerator(const ServiceDescriptor* descriptor)
: descriptor_(descriptor) {}
virtual ~ServiceGenerator() = default;
ServiceGenerator(const ServiceGenerator&) = delete;
ServiceGenerator& operator=(const ServiceGenerator&) = delete;
virtual void Generate(io::Printer* printer) = 0;
enum RequestOrResponse { REQUEST, RESPONSE };
enum IsAbstract { IS_ABSTRACT, IS_CONCRETE };
protected:
const ServiceDescriptor* descriptor_;
};
// An interface to create generators for a given descriptor. This interface
// is implemented for every variant of the Java API (Mutable, Immutable, Lite).
class GeneratorFactory {
public:
GeneratorFactory();
GeneratorFactory(const GeneratorFactory&) = delete;
GeneratorFactory& operator=(const GeneratorFactory&) = delete;
virtual ~GeneratorFactory();
virtual ~GeneratorFactory() = default;
virtual MessageGenerator* NewMessageGenerator(
virtual std::unique_ptr<MessageGenerator> NewMessageGenerator(
const Descriptor* descriptor) const = 0;
virtual ExtensionGenerator* NewExtensionGenerator(
virtual std::unique_ptr<EnumGenerator> NewEnumGenerator(
const EnumDescriptor* descriptor) const = 0;
virtual std::unique_ptr<ExtensionGenerator> NewExtensionGenerator(
const FieldDescriptor* descriptor) const = 0;
virtual ServiceGenerator* NewServiceGenerator(
virtual std::unique_ptr<ServiceGenerator> NewServiceGenerator(
const ServiceDescriptor* descriptor) const = 0;
};
// Factory that creates generators for immutable-default messages.
class ImmutableGeneratorFactory : public GeneratorFactory {
public:
ImmutableGeneratorFactory(Context* context);
ImmutableGeneratorFactory(const ImmutableGeneratorFactory&) = delete;
ImmutableGeneratorFactory& operator=(const ImmutableGeneratorFactory&) =
delete;
~ImmutableGeneratorFactory() override;
MessageGenerator* NewMessageGenerator(
const Descriptor* descriptor) const override;
ExtensionGenerator* NewExtensionGenerator(
const FieldDescriptor* descriptor) const override;
ServiceGenerator* NewServiceGenerator(
const ServiceDescriptor* descriptor) const override;
private:
Context* context_;
};
} // namespace java
} // namespace compiler
} // namespace protobuf

View File

@ -0,0 +1,131 @@
# We use abbreviated target names in this directory to work around:
# https://github.com/bazelbuild/bazel/issues/18683
cc_library(
name = "fg",
hdrs = ["field_generator.h"],
strip_include_prefix = "/src",
deps = [
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/io:printer",
],
)
# We have to expose the field generators individually (instead of just through the factory) because
# the mutable generators delegate to them directly.
cc_library(
name = "fgs",
srcs = [
"enum_field.cc",
"map_field.cc",
"message_field.cc",
"primitive_field.cc",
"string_field.cc",
],
hdrs = [
"enum_field.h",
"map_field.h",
"message_field.h",
"primitive_field.h",
"string_field.h",
],
strip_include_prefix = "/src",
deps = [
":fg",
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/compiler/java:helpers",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "mfg",
srcs = ["make_field_generators.cc"],
hdrs = ["make_field_generators.h"],
strip_include_prefix = "/src",
deps = [
":fg",
":fgs",
"//src/google/protobuf",
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/compiler/java:helpers",
],
)
cc_library(
name = "service",
srcs = ["service.cc"],
hdrs = ["service.h"],
strip_include_prefix = "/src",
visibility = ["//src/google/protobuf/compiler/java/lite:__pkg__"],
deps = [
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/compiler/java:helpers",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "eg",
srcs = ["enum.cc"],
hdrs = ["enum.h"],
strip_include_prefix = "/src",
deps = [
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/compiler/java:helpers",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "immutable",
srcs = [
"extension.cc",
"generator_factory.cc",
"message.cc",
"message.h",
"message_builder.cc",
],
hdrs = [
"generator_factory.h",
# We don't actually want to put the remaining headers in `hdrs`.
# They are logically private, and should be in srcs=[], but
# unfortunately `strip_include_prefix` doesn't have any effect
# on headers listed in `srcs.`
"message.h",
"extension.h",
"message_builder.h",
],
strip_include_prefix = "/src",
visibility = ["//src/google/protobuf/compiler/java:__pkg__"],
deps = [
":eg",
":fg",
":mfg",
":service",
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/compiler/java:helpers",
"//src/google/protobuf/compiler/java:message_serialization",
"//src/google/protobuf/io",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/strings",
],
)

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/enum.h"
#include "google/protobuf/compiler/java/immutable/enum.h"
#include <string>
@ -30,8 +30,8 @@ namespace protobuf {
namespace compiler {
namespace java {
EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
bool immutable_api, Context* context)
EnumNonLiteGenerator::EnumNonLiteGenerator(const EnumDescriptor* descriptor,
bool immutable_api, Context* context)
: descriptor_(descriptor),
immutable_api_(immutable_api),
context_(context),
@ -52,9 +52,7 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
}
}
EnumGenerator::~EnumGenerator() {}
void EnumGenerator::Generate(io::Printer* printer) {
void EnumNonLiteGenerator::Generate(io::Printer* printer) {
WriteEnumDocComment(printer, descriptor_, context_->options());
MaybePrintGeneratedAnnotation(context_, printer, descriptor_, immutable_api_);
@ -376,7 +374,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
printer->Print("}\n\n");
}
bool EnumGenerator::CanUseEnumValues() {
bool EnumNonLiteGenerator::CanUseEnumValues() {
if (canonical_values_.size() != descriptor_->value_count()) {
return false;
}

View File

@ -15,6 +15,7 @@
#include <string>
#include <vector>
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/descriptor.h"
namespace google {
@ -36,15 +37,17 @@ namespace protobuf {
namespace compiler {
namespace java {
class EnumGenerator {
// This class is in the immutable/ directory, but since the mutable API for
// enums is nearly identical to the immutable one, we also use this for mutable
// enums. It is used for all enums except lite enums.
class EnumNonLiteGenerator : public EnumGenerator {
public:
EnumGenerator(const EnumDescriptor* descriptor, bool immutable_api,
Context* context);
EnumGenerator(const EnumGenerator&) = delete;
EnumGenerator& operator=(const EnumGenerator&) = delete;
~EnumGenerator();
EnumNonLiteGenerator(const EnumDescriptor* descriptor, bool immutable_api,
Context* context);
EnumNonLiteGenerator(const EnumNonLiteGenerator&) = delete;
EnumNonLiteGenerator& operator=(const EnumNonLiteGenerator&) = delete;
void Generate(io::Printer* printer);
void Generate(io::Printer* printer) override;
private:
const EnumDescriptor* descriptor_;

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/enum_field.h"
#include "google/protobuf/compiler/java/immutable/enum_field.h"
#include <cstdint>
#include <string>
@ -20,6 +20,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"

View File

@ -15,7 +15,9 @@
#include <string>
#include "absl/container/flat_hash_map.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
namespace google {
namespace protobuf {

View File

@ -9,13 +9,10 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/extension.h"
#include "google/protobuf/compiler/java/immutable/extension.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"

View File

@ -15,6 +15,7 @@
#include <string>
#include "absl/container/flat_hash_map.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/port.h"
namespace google {
@ -37,34 +38,6 @@ namespace protobuf {
namespace compiler {
namespace java {
// Generates code for an extension, which may be within the scope of some
// message or may be at file scope. This is much simpler than FieldGenerator
// since extensions are just simple identifiers with interesting types.
class ExtensionGenerator {
public:
explicit ExtensionGenerator() {}
ExtensionGenerator(const ExtensionGenerator&) = delete;
ExtensionGenerator& operator=(const ExtensionGenerator&) = delete;
virtual ~ExtensionGenerator() {}
virtual void Generate(io::Printer* printer) = 0;
// Returns an estimate of the number of bytes the printed code will compile
// to
virtual int GenerateNonNestedInitializationCode(io::Printer* printer) = 0;
// Returns an estimate of the number of bytes the printed code will compile
// to
virtual int GenerateRegistrationCode(io::Printer* printer) = 0;
protected:
static void InitTemplateVars(
const FieldDescriptor* descriptor, const std::string& scope,
bool immutable, ClassNameResolver* name_resolver,
absl::flat_hash_map<absl::string_view, std::string>* vars_pointer,
Context* context);
};
class ImmutableExtensionGenerator : public ExtensionGenerator {
public:
explicit ImmutableExtensionGenerator(const FieldDescriptor* descriptor,

View File

@ -0,0 +1,54 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__
#include <string>
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/io/printer.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
class ImmutableFieldGenerator : public FieldGenerator {
public:
ImmutableFieldGenerator() = default;
ImmutableFieldGenerator(const ImmutableFieldGenerator&) = delete;
ImmutableFieldGenerator& operator=(const ImmutableFieldGenerator&) = delete;
~ImmutableFieldGenerator() override = default;
virtual int GetMessageBitIndex() const = 0;
virtual int GetBuilderBitIndex() const = 0;
virtual int GetNumBitsForMessage() const = 0;
virtual int GetNumBitsForBuilder() const = 0;
virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
virtual void GenerateMembers(io::Printer* printer) const = 0;
virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0;
virtual void GenerateMergingCode(io::Printer* printer) const = 0;
virtual void GenerateBuildingCode(io::Printer* printer) const = 0;
virtual void GenerateBuilderParsingCode(io::Printer* printer) const = 0;
virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
virtual void GenerateFieldBuilderInitializationCode(
io::Printer* printer) const = 0;
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
virtual void GenerateBuilderParsingCodeFromPacked(
io::Printer* printer) const {
ReportUnexpectedPackedFieldsCall();
}
virtual void GenerateEqualsCode(io::Printer* printer) const = 0;
virtual void GenerateHashCode(io::Printer* printer) const = 0;
virtual std::string GetBoxedType() const = 0;
};
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__

View File

@ -0,0 +1,67 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: liujisi@google.com (Pherl Liu)
#include "google/protobuf/compiler/java/generator_factory.h"
#include <memory>
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/immutable/enum.h"
#include "google/protobuf/compiler/java/immutable/extension.h"
#include "google/protobuf/compiler/java/immutable/message.h"
#include "google/protobuf/compiler/java/immutable/service.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
// Factory that creates generators for immutable-default messages.
class ImmutableGeneratorFactory : public GeneratorFactory {
public:
explicit ImmutableGeneratorFactory(Context* context) : context_(context) {}
ImmutableGeneratorFactory(const ImmutableGeneratorFactory&) = delete;
ImmutableGeneratorFactory& operator=(const ImmutableGeneratorFactory&) =
delete;
~ImmutableGeneratorFactory() override = default;
std::unique_ptr<MessageGenerator> NewMessageGenerator(
const Descriptor* descriptor) const override {
return std::make_unique<ImmutableMessageGenerator>(descriptor, context_);
}
std::unique_ptr<EnumGenerator> NewEnumGenerator(
const EnumDescriptor* descriptor) const override {
return std::make_unique<EnumNonLiteGenerator>(descriptor, true, context_);
}
std::unique_ptr<ExtensionGenerator> NewExtensionGenerator(
const FieldDescriptor* descriptor) const override {
return std::make_unique<ImmutableExtensionGenerator>(descriptor, context_);
}
std::unique_ptr<ServiceGenerator> NewServiceGenerator(
const ServiceDescriptor* descriptor) const override {
return std::make_unique<ImmutableServiceGenerator>(descriptor, context_);
}
private:
Context* context_;
};
std::unique_ptr<GeneratorFactory> MakeImmutableGeneratorFactory(
Context* context) {
return std::make_unique<ImmutableGeneratorFactory>(context);
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google

View File

@ -0,0 +1,19 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__
#include "google/protobuf/compiler/java/generator_factory.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
std::unique_ptr<GeneratorFactory> MakeImmutableGeneratorFactory(
Context* context);
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__

View File

@ -0,0 +1,114 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include <memory>
#include <utility>
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/immutable/enum_field.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/compiler/java/immutable/map_field.h"
#include "google/protobuf/compiler/java/immutable/message_field.h"
#include "google/protobuf/compiler/java/immutable/primitive_field.h"
#include "google/protobuf/compiler/java/immutable/string_field.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
namespace {
std::unique_ptr<ImmutableFieldGenerator> MakeImmutableGenerator(
const FieldDescriptor* field, int messageBitIndex, int builderBitIndex,
Context* context) {
if (field->is_repeated()) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
if (IsMapEntry(field->message_type())) {
return std::make_unique<ImmutableMapFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
} else {
return std::make_unique<RepeatedImmutableMessageFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
}
case JAVATYPE_ENUM:
return std::make_unique<RepeatedImmutableEnumFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
case JAVATYPE_STRING:
return std::make_unique<RepeatedImmutableStringFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
default:
return std::make_unique<RepeatedImmutablePrimitiveFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
}
} else {
if (IsRealOneof(field)) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
return std::make_unique<ImmutableMessageOneofFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
case JAVATYPE_ENUM:
return std::make_unique<ImmutableEnumOneofFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
case JAVATYPE_STRING:
return std::make_unique<ImmutableStringOneofFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
default:
return std::make_unique<ImmutablePrimitiveOneofFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
}
} else {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
return std::make_unique<ImmutableMessageFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
case JAVATYPE_ENUM:
return std::make_unique<ImmutableEnumFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
case JAVATYPE_STRING:
return std::make_unique<ImmutableStringFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
default:
return std::make_unique<ImmutablePrimitiveFieldGenerator>(
field, messageBitIndex, builderBitIndex, context);
}
}
}
}
} // namespace
FieldGeneratorMap<ImmutableFieldGenerator> MakeImmutableFieldGenerators(
const Descriptor* descriptor, Context* context) {
// Construct all the FieldGenerators and assign them bit indices for their
// bit fields.
int messageBitIndex = 0;
int builderBitIndex = 0;
FieldGeneratorMap<ImmutableFieldGenerator> ret(descriptor);
for (int i = 0; i < descriptor->field_count(); i++) {
const FieldDescriptor* field = descriptor->field(i);
auto generator = MakeImmutableGenerator(field, messageBitIndex,
builderBitIndex, context);
messageBitIndex += generator->GetNumBitsForMessage();
builderBitIndex += generator->GetNumBitsForBuilder();
ret.Add(field, std::move(generator));
}
return ret;
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google

View File

@ -0,0 +1,29 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
FieldGeneratorMap<ImmutableFieldGenerator> MakeImmutableFieldGenerators(
const Descriptor* descriptor, Context* context);
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__

View File

@ -5,7 +5,7 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "google/protobuf/compiler/java/map_field.h"
#include "google/protobuf/compiler/java/immutable/map_field.h"
#include <string>
@ -13,7 +13,7 @@
#include "absl/strings/str_join.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"

View File

@ -8,7 +8,10 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_H__
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
namespace google {
namespace protobuf {

View File

@ -9,24 +9,29 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/message.h"
#include "google/protobuf/compiler/java/immutable/message.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_check.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/enum.h"
#include "google/protobuf/compiler/java/extension.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/message_builder.h"
#include "google/protobuf/compiler/java/message_builder_lite.h"
#include "google/protobuf/compiler/java/immutable/enum.h"
#include "google/protobuf/compiler/java/immutable/extension.h"
#include "google/protobuf/compiler/java/immutable/make_field_generators.h"
#include "google/protobuf/compiler/java/immutable/message_builder.h"
#include "google/protobuf/compiler/java/message_serialization.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.h"
@ -75,7 +80,7 @@ ImmutableMessageGenerator::ImmutableMessageGenerator(
: MessageGenerator(descriptor),
context_(context),
name_resolver_(context->GetNameResolver()),
field_generators_(descriptor, context_) {
field_generators_(MakeImmutableFieldGenerators(descriptor, context_)) {
ABSL_CHECK(HasDescriptorMethods(descriptor->file(), context->EnforceLite()))
<< "Generator factory error: A non-lite message generator is used to "
"generate lite messages.";
@ -362,7 +367,8 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
// Nested types
for (int i = 0; i < descriptor_->enum_type_count(); i++) {
EnumGenerator(descriptor_->enum_type(i), true, context_).Generate(printer);
EnumNonLiteGenerator(descriptor_->enum_type(i), true, context_)
.Generate(printer);
}
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
@ -593,8 +599,9 @@ void ImmutableMessageGenerator::GenerateMessageSerializationMethods(
}
}
GenerateSerializeFieldsAndExtensions(printer, field_generators_, descriptor_,
sorted_fields.get());
GenerateSerializeFieldsAndExtensions(printer,
field_generators_.field_generators(),
descriptor_, sorted_fields.get());
if (descriptor_->options().message_set_wire_format()) {
printer->Print("getUnknownFields().writeAsMessageSetTo(output);\n");

View File

@ -12,10 +12,13 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__
#include <memory>
#include <string>
#include <vector>
#include "absl/container/btree_map.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
@ -36,44 +39,6 @@ namespace protobuf {
namespace compiler {
namespace java {
static const int kMaxStaticSize = 1 << 15; // aka 32k
class MessageGenerator {
public:
explicit MessageGenerator(const Descriptor* descriptor);
MessageGenerator(const MessageGenerator&) = delete;
MessageGenerator& operator=(const MessageGenerator&) = delete;
virtual ~MessageGenerator();
// All static variables have to be declared at the top-level of the file
// so that we can control initialization order, which is important for
// DescriptorProto bootstrapping to work.
virtual void GenerateStaticVariables(io::Printer* printer,
int* bytecode_estimate) = 0;
// Output code which initializes the static variables generated by
// GenerateStaticVariables(). Returns an estimate of bytecode size.
virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0;
// Generate the class itself.
virtual void Generate(io::Printer* printer) = 0;
// Generates the base interface that both the class and its builder
// implement
virtual void GenerateInterface(io::Printer* printer) = 0;
// Generate code to register all contained extensions with an
// ExtensionRegistry.
virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0;
virtual void GenerateKotlinDsl(io::Printer* printer) const = 0;
virtual void GenerateKotlinMembers(io::Printer* printer) const = 0;
virtual void GenerateTopLevelKotlinMembers(io::Printer* printer) const = 0;
protected:
const Descriptor* descriptor_;
absl::btree_map<int, const OneofDescriptor*> oneofs_;
};
class ImmutableMessageGenerator : public MessageGenerator {
public:
ImmutableMessageGenerator(const Descriptor* descriptor, Context* context);

View File

@ -9,21 +9,28 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/message_builder.h"
#include "google/protobuf/compiler/java/immutable/message_builder.h"
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_check.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/enum.h"
#include "google/protobuf/compiler/java/extension.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/immutable/enum.h"
#include "google/protobuf/compiler/java/immutable/extension.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/compiler/java/immutable/make_field_generators.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/printer.h"
@ -73,7 +80,7 @@ MessageBuilderGenerator::MessageBuilderGenerator(const Descriptor* descriptor,
: descriptor_(descriptor),
context_(context),
name_resolver_(context->GetNameResolver()),
field_generators_(descriptor, context_) {
field_generators_(MakeImmutableFieldGenerators(descriptor, context_)) {
ABSL_CHECK(HasDescriptorMethods(descriptor->file(), context->EnforceLite()))
<< "Generator factory error: A non-lite message generator is used to "
"generate lite messages.";

View File

@ -12,10 +12,13 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_H__
#include <memory>
#include <string>
#include <vector>
#include "absl/container/btree_map.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/message_field.h"
#include "google/protobuf/compiler/java/immutable/message_field.h"
#include <string>
@ -17,10 +17,10 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/wire_format.h"
// Must be last.
#include "google/protobuf/port_def.inc"

View File

@ -14,7 +14,8 @@
#include <string>
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
namespace google {

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/primitive_field.h"
#include "google/protobuf/compiler/java/immutable/primitive_field.h"
#include <cstdint>
#include <string>
@ -19,6 +19,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.h"

View File

@ -14,7 +14,8 @@
#include <string>
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/service.h"
#include "google/protobuf/compiler/java/immutable/service.h"
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
@ -27,12 +27,6 @@ namespace protobuf {
namespace compiler {
namespace java {
ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor)
: descriptor_(descriptor) {}
ServiceGenerator::~ServiceGenerator() {}
// ===================================================================
ImmutableServiceGenerator::ImmutableServiceGenerator(
const ServiceDescriptor* descriptor, Context* context)
: ServiceGenerator(descriptor),

View File

@ -12,6 +12,7 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_H__
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/descriptor.h"
namespace google {
@ -33,22 +34,6 @@ namespace protobuf {
namespace compiler {
namespace java {
class ServiceGenerator {
public:
explicit ServiceGenerator(const ServiceDescriptor* descriptor);
ServiceGenerator(const ServiceGenerator&) = delete;
ServiceGenerator& operator=(const ServiceGenerator&) = delete;
virtual ~ServiceGenerator();
virtual void Generate(io::Printer* printer) = 0;
enum RequestOrResponse { REQUEST, RESPONSE };
enum IsAbstract { IS_ABSTRACT, IS_CONCRETE };
protected:
const ServiceDescriptor* descriptor_;
};
class ImmutableServiceGenerator : public ServiceGenerator {
public:
ImmutableServiceGenerator(const ServiceDescriptor* descriptor,

View File

@ -10,7 +10,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/string_field.h"
#include "google/protobuf/compiler/java/immutable/string_field.h"
#include <cstdint>
#include <string>
@ -20,6 +20,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"

View File

@ -15,7 +15,8 @@
#include <string>
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/immutable/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {

View File

@ -0,0 +1,77 @@
cc_library(
name = "field_generators",
srcs = [
"enum_field.cc",
"extension.cc",
"make_field_generators.cc",
"map_field.cc",
"message_field.cc",
"primitive_field.cc",
"string_field.cc",
],
hdrs = [
"field_generator.h",
"make_field_generators.h",
# We don't actually want to put the remaining headers in `hdrs`.
# They are logically private, and should be in srcs=[], but
# unfortunately `strip_include_prefix` doesn't have any effect
# on headers listed in `srcs.`
"map_field.h",
"enum_field.h",
"extension.h",
"message_field.h",
"primitive_field.h",
"string_field.h",
],
strip_include_prefix = "/src",
visibility = ["//src/google/protobuf/compiler/java:__subpackages__"],
deps = [
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/compiler/java:helpers",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "lite",
srcs = [
"enum.cc",
"extension.cc",
"generator_factory.cc",
"message.cc",
"message_builder.cc",
],
hdrs = [
"generator_factory.h",
# We don't actually want to put the remaining headers in `hdrs`.
# They are logically private, and should be in srcs=[], but
# unfortunately `strip_include_prefix` doesn't have any effect
# on headers listed in `srcs.`
"enum.h",
"extension.h",
"message.h",
"message_builder.h",
],
strip_include_prefix = "/src",
visibility = ["//src/google/protobuf/compiler/java:__subpackages__"],
deps = [
":field_generators",
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/compiler/java:helpers",
"//src/google/protobuf/compiler/java/immutable:service",
"//src/google/protobuf/io",
"//src/google/protobuf/io:printer",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/strings",
],
)

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/enum_lite.h"
#include "google/protobuf/compiler/java/lite/enum.h"
#include <string>

View File

@ -15,6 +15,7 @@
#include <string>
#include <vector>
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/descriptor.h"
namespace google {
@ -36,7 +37,7 @@ namespace protobuf {
namespace compiler {
namespace java {
class EnumLiteGenerator {
class EnumLiteGenerator : public EnumGenerator {
public:
EnumLiteGenerator(const EnumDescriptor* descriptor, bool immutable_api,
Context* context);
@ -44,7 +45,7 @@ class EnumLiteGenerator {
EnumLiteGenerator& operator=(const EnumLiteGenerator&) = delete;
~EnumLiteGenerator();
void Generate(io::Printer* printer);
void Generate(io::Printer* printer) override;
private:
const EnumDescriptor* descriptor_;

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/enum_field_lite.h"
#include "google/protobuf/compiler/java/lite/enum_field.h"
#include <cstdint>
#include <string>
@ -20,6 +20,7 @@
#include "absl/strings/str_join.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"

View File

@ -16,7 +16,8 @@
#include <string>
#include "absl/container/flat_hash_map.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {

View File

@ -5,7 +5,7 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "google/protobuf/compiler/java/extension_lite.h"
#include "google/protobuf/compiler/java/lite/extension.h"
#include <string>

View File

@ -10,7 +10,8 @@
#include <string>
#include "google/protobuf/compiler/java/extension.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/port.h"
namespace google {

View File

@ -0,0 +1,38 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__
#include <string>
#include "google/protobuf/io/printer.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
class ImmutableFieldLiteGenerator {
public:
ImmutableFieldLiteGenerator() = default;
ImmutableFieldLiteGenerator(const ImmutableFieldLiteGenerator&) = delete;
ImmutableFieldLiteGenerator& operator=(const ImmutableFieldLiteGenerator&) =
delete;
virtual ~ImmutableFieldLiteGenerator() = default;
virtual int GetNumBitsForMessage() const = 0;
virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
virtual void GenerateMembers(io::Printer* printer) const = 0;
virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
virtual void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const = 0;
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
virtual std::string GetBoxedType() const = 0;
};
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__

View File

@ -0,0 +1,70 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: liujisi@google.com (Pherl Liu)
#include "google/protobuf/compiler/java/generator_factory.h"
#include <memory>
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/immutable/service.h"
#include "google/protobuf/compiler/java/lite/enum.h"
#include "google/protobuf/compiler/java/lite/extension.h"
#include "google/protobuf/compiler/java/lite/message.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
// Factory that creates generators for immutable-default messages.
class ImmutableLiteGeneratorFactory : public GeneratorFactory {
public:
explicit ImmutableLiteGeneratorFactory(Context* context)
: context_(context) {}
ImmutableLiteGeneratorFactory(const ImmutableLiteGeneratorFactory&) = delete;
ImmutableLiteGeneratorFactory& operator=(
const ImmutableLiteGeneratorFactory&) = delete;
~ImmutableLiteGeneratorFactory() override = default;
std::unique_ptr<MessageGenerator> NewMessageGenerator(
const Descriptor* descriptor) const override {
return std::make_unique<ImmutableMessageLiteGenerator>(descriptor,
context_);
}
std::unique_ptr<EnumGenerator> NewEnumGenerator(
const EnumDescriptor* descriptor) const override {
return std::make_unique<EnumLiteGenerator>(descriptor, true, context_);
}
std::unique_ptr<ExtensionGenerator> NewExtensionGenerator(
const FieldDescriptor* descriptor) const override {
return std::make_unique<ImmutableExtensionLiteGenerator>(descriptor,
context_);
}
std::unique_ptr<ServiceGenerator> NewServiceGenerator(
const ServiceDescriptor* descriptor) const override {
return std::make_unique<ImmutableServiceGenerator>(descriptor, context_);
}
private:
Context* context_;
};
std::unique_ptr<GeneratorFactory> MakeImmutableLiteGeneratorFactory(
Context* context) {
return std::make_unique<ImmutableLiteGeneratorFactory>(context);
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google

View File

@ -0,0 +1,19 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__
#include "google/protobuf/compiler/java/generator_factory.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
std::unique_ptr<GeneratorFactory> MakeImmutableLiteGeneratorFactory(
Context* context);
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__

View File

@ -0,0 +1,110 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/lite/make_field_generators.h"
#include <memory>
#include <utility>
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/lite/enum_field.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
#include "google/protobuf/compiler/java/lite/map_field.h"
#include "google/protobuf/compiler/java/lite/message_field.h"
#include "google/protobuf/compiler/java/lite/primitive_field.h"
#include "google/protobuf/compiler/java/lite/string_field.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
namespace {
std::unique_ptr<ImmutableFieldLiteGenerator> CreateImmutableLiteGenerator(
const FieldDescriptor* field, int messageBitIndex, Context* context) {
if (field->is_repeated()) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
if (IsMapEntry(field->message_type())) {
return std::make_unique<ImmutableMapFieldLiteGenerator>(
field, messageBitIndex, context);
} else {
return std::make_unique<RepeatedImmutableMessageFieldLiteGenerator>(
field, messageBitIndex, context);
}
case JAVATYPE_ENUM:
return std::make_unique<RepeatedImmutableEnumFieldLiteGenerator>(
field, messageBitIndex, context);
case JAVATYPE_STRING:
return std::make_unique<RepeatedImmutableStringFieldLiteGenerator>(
field, messageBitIndex, context);
default:
return std::make_unique<RepeatedImmutablePrimitiveFieldLiteGenerator>(
field, messageBitIndex, context);
}
} else {
if (IsRealOneof(field)) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
return std::make_unique<ImmutableMessageOneofFieldLiteGenerator>(
field, messageBitIndex, context);
case JAVATYPE_ENUM:
return std::make_unique<ImmutableEnumOneofFieldLiteGenerator>(
field, messageBitIndex, context);
case JAVATYPE_STRING:
return std::make_unique<ImmutableStringOneofFieldLiteGenerator>(
field, messageBitIndex, context);
default:
return std::make_unique<ImmutablePrimitiveOneofFieldLiteGenerator>(
field, messageBitIndex, context);
}
} else {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
return std::make_unique<ImmutableMessageFieldLiteGenerator>(
field, messageBitIndex, context);
case JAVATYPE_ENUM:
return std::make_unique<ImmutableEnumFieldLiteGenerator>(
field, messageBitIndex, context);
case JAVATYPE_STRING:
return std::make_unique<ImmutableStringFieldLiteGenerator>(
field, messageBitIndex, context);
default:
return std::make_unique<ImmutablePrimitiveFieldLiteGenerator>(
field, messageBitIndex, context);
}
}
}
}
} // namespace
FieldGeneratorMap<ImmutableFieldLiteGenerator> MakeImmutableFieldLiteGenerators(
const Descriptor* descriptor, Context* context) {
int messageBitIndex = 0;
FieldGeneratorMap<ImmutableFieldLiteGenerator> field_generators(descriptor);
for (int i = 0; i < descriptor->field_count(); i++) {
const FieldDescriptor* field = descriptor->field(i);
auto generator =
CreateImmutableLiteGenerator(field, messageBitIndex, context);
messageBitIndex += generator->GetNumBitsForMessage();
field_generators.Add(field, std::move(generator));
}
return field_generators;
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google

View File

@ -0,0 +1,32 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__
#include <memory>
#include <vector>
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
FieldGeneratorMap<ImmutableFieldLiteGenerator> MakeImmutableFieldLiteGenerators(
const Descriptor* descriptor, Context* context);
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__

View File

@ -5,13 +5,14 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "google/protobuf/compiler/java/map_field_lite.h"
#include "google/protobuf/compiler/java/lite/map_field.h"
#include <cstdint>
#include <string>
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"

View File

@ -9,8 +9,14 @@
#define GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_LITE_H__
#include <cstdint>
#include <string>
#include <vector>
#include "google/protobuf/compiler/java/field.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {

View File

@ -9,27 +9,32 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/message_lite.h"
#include "google/protobuf/compiler/java/lite/message.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_check.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/enum_lite.h"
#include "google/protobuf/compiler/java/extension_lite.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/generator.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/message_builder.h"
#include "google/protobuf/compiler/java/message_builder_lite.h"
#include "google/protobuf/compiler/java/lite/enum.h"
#include "google/protobuf/compiler/java/lite/extension.h"
#include "google/protobuf/compiler/java/lite/make_field_generators.h"
#include "google/protobuf/compiler/java/lite/message_builder.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/coded_stream.h"
@ -53,7 +58,7 @@ ImmutableMessageLiteGenerator::ImmutableMessageLiteGenerator(
: MessageGenerator(descriptor),
context_(context),
name_resolver_(context->GetNameResolver()),
field_generators_(descriptor, context_) {
field_generators_(MakeImmutableFieldLiteGenerators(descriptor, context)) {
ABSL_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite()))
<< "Generator factory error: A lite message generator is used to "
"generate non-lite messages.";

View File

@ -12,8 +12,16 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_LITE_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_LITE_H__
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/message.h"
#include <memory>
#include <vector>
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
namespace google {
namespace protobuf {

View File

@ -9,27 +9,21 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/message_builder_lite.h"
#include "google/protobuf/compiler/java/lite/message_builder.h"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/enum.h"
#include "google/protobuf/compiler/java/extension.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/lite/enum.h"
#include "google/protobuf/compiler/java/lite/make_field_generators.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/wire_format.h"
// Must be last.
#include "google/protobuf/port_def.inc"
@ -44,7 +38,7 @@ MessageBuilderLiteGenerator::MessageBuilderLiteGenerator(
: descriptor_(descriptor),
context_(context),
name_resolver_(context->GetNameResolver()),
field_generators_(descriptor, context_) {
field_generators_(MakeImmutableFieldLiteGenerators(descriptor, context)) {
ABSL_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite()))
<< "Generator factory error: A lite message generator is used to "
"generate non-lite messages.";

View File

@ -15,7 +15,8 @@
#include <string>
#include "absl/container/btree_map.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
namespace google {
namespace protobuf {

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/message_field_lite.h"
#include "google/protobuf/compiler/java/lite/message_field.h"
#include <cstdint>
#include <string>
@ -17,6 +17,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"

View File

@ -14,8 +14,12 @@
#include <cstdint>
#include <string>
#include <vector>
#include "google/protobuf/compiler/java/field.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {

View File

@ -9,7 +9,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/primitive_field_lite.h"
#include "google/protobuf/compiler/java/lite/primitive_field.h"
#include <cstdint>
#include <string>
@ -19,6 +19,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/wire_format.h"

View File

@ -14,8 +14,12 @@
#include <cstdint>
#include <string>
#include <vector>
#include "google/protobuf/compiler/java/field.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {

View File

@ -10,7 +10,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/string_field_lite.h"
#include "google/protobuf/compiler/java/lite/string_field.h"
#include <cstdint>
#include <string>
@ -20,6 +20,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/wire_format.h"

View File

@ -15,8 +15,12 @@
#include <cstdint>
#include <string>
#include <vector>
#include "google/protobuf/compiler/java/field.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/java/lite/field_generator.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {

View File

@ -12,8 +12,8 @@
#include <cstddef>
#include <vector>
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "absl/types/span.h"
#include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
@ -29,12 +29,9 @@ void GenerateSerializeExtensionRange(io::Printer* printer,
// Generates code to serialize all fields and extension ranges for the specified
// message descriptor, sorting serialization calls in increasing order by field
// number.
//
// Templatized to support different field generator implementations.
template <typename FieldGenerator>
void GenerateSerializeFieldsAndExtensions(
inline void GenerateSerializeFieldsAndExtensions(
io::Printer* printer,
const FieldGeneratorMap<FieldGenerator>& field_generators,
const std::vector<const FieldGenerator*>& field_generators,
const Descriptor* descriptor, const FieldDescriptor** sorted_fields) {
std::vector<const Descriptor::ExtensionRange*> sorted_extensions;
sorted_extensions.reserve(descriptor->extension_range_count());
@ -62,7 +59,8 @@ void GenerateSerializeFieldsAndExtensions(
if (range != nullptr) {
GenerateSerializeExtensionRange(printer, range);
}
field_generators.get(field).GenerateSerializationCode(printer);
size_t idx = static_cast<size_t>(field->index());
field_generators[idx]->GenerateSerializationCode(printer);
}
// After serializing all fields, serialize any remaining extensions via a