Skip to content

Commit 5737cd2

Browse files
Expose NestedInFileClass naming helpers for Java immutable. (#24401)
This matches our GeneratorNames APIs, and it's become clear that this information is necessary for certain classes of code-generators to be able to predict where our generated code will end up. PiperOrigin-RevId: 829216363 Co-authored-by: Mike Kruskal <[email protected]>
1 parent 59db671 commit 5737cd2

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

src/google/protobuf/compiler/java/full/message.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "google/protobuf/compiler/java/full/message_builder.h"
3838
#include "google/protobuf/compiler/java/message_serialization.h"
3939
#include "google/protobuf/compiler/java/name_resolver.h"
40+
#include "google/protobuf/compiler/java/names.h"
4041
#include "google/protobuf/descriptor.h"
4142
#include "google/protobuf/descriptor.pb.h"
4243
#include "google/protobuf/io/coded_stream.h"
@@ -104,7 +105,7 @@ void ImmutableMessageGenerator::GenerateStaticVariables(
104105
if (descriptor_->containing_type() != nullptr) {
105106
vars["parent"] = UniqueFileScopeIdentifier(descriptor_->containing_type());
106107
}
107-
if (NestedInFileClass(*descriptor_, /* immutable = */ true)) {
108+
if (NestedInFileClass(*descriptor_)) {
108109
vars["private"] = "private ";
109110
} else {
110111
// We can only make these package-private since the classes that use them
@@ -179,7 +180,7 @@ void ImmutableMessageGenerator::GenerateFieldAccessorTable(
179180
io::Printer* printer, int* bytecode_estimate) {
180181
absl::flat_hash_map<absl::string_view, std::string> vars;
181182
vars["identifier"] = UniqueFileScopeIdentifier(descriptor_);
182-
if (NestedInFileClass(*descriptor_, /* immutable = */ true)) {
183+
if (NestedInFileClass(*descriptor_)) {
183184
vars["private"] = "private ";
184185
} else {
185186
// We can only make these package-private since the classes that use them

src/google/protobuf/compiler/java/helpers.cc

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -927,29 +927,12 @@ const FieldDescriptor* MapValueField(const FieldDescriptor* descriptor) {
927927

928928
namespace {
929929

930-
// Gets the value of `nest_in_file_class` feature and returns whether the
931-
// generated class should be nested in the generated proto file Java class.
932-
template <typename Descriptor>
933-
inline bool NestInFileClass(const Descriptor& descriptor) {
934-
auto nest_in_file_class =
935-
JavaGenerator::GetResolvedSourceFeatureExtension(descriptor, pb::java)
936-
.nest_in_file_class();
937-
ABSL_CHECK(
938-
nest_in_file_class !=
939-
pb::JavaFeatures::NestInFileClassFeature::NEST_IN_FILE_CLASS_UNKNOWN);
940-
941-
if (nest_in_file_class == pb::JavaFeatures::NestInFileClassFeature::LEGACY) {
942-
return !descriptor.file()->options().java_multiple_files();
943-
}
944-
return nest_in_file_class == pb::JavaFeatures::NestInFileClassFeature::YES;
945-
}
946-
947930
// Returns whether the type should be nested in the file class for the given
948931
// descriptor, depending on different Protobuf Java API versions.
949932
template <typename Descriptor>
950933
bool NestInFileClass(const Descriptor& descriptor, bool immutable) {
951934
(void)immutable;
952-
return NestInFileClass(descriptor);
935+
return NestedInFileClass(descriptor);
953936
}
954937

955938
template <typename Descriptor>

src/google/protobuf/compiler/java/names.cc

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
#include <string>
1515

1616
#include "absl/container/flat_hash_set.h"
17+
#include "absl/log/absl_check.h"
1718
#include "absl/strings/str_cat.h"
1819
#include "absl/strings/string_view.h"
20+
#include "google/protobuf/compiler/java/java_features.pb.h"
21+
#include "google/protobuf/compiler/java/generator.h"
1922
#include "google/protobuf/compiler/java/helpers.h"
2023
#include "google/protobuf/compiler/java/name_resolver.h"
2124
#include "google/protobuf/compiler/java/names_internal.h"
@@ -94,6 +97,21 @@ std::string FieldName(const FieldDescriptor* field) {
9497
return field_name;
9598
}
9699

100+
template <typename Descriptor>
101+
bool NestedInFileClassImpl(const Descriptor& descriptor) {
102+
auto nest_in_file_class =
103+
JavaGenerator::GetResolvedSourceFeatureExtension(descriptor, pb::java)
104+
.nest_in_file_class();
105+
ABSL_CHECK(
106+
nest_in_file_class !=
107+
pb::JavaFeatures::NestInFileClassFeature::NEST_IN_FILE_CLASS_UNKNOWN);
108+
109+
if (nest_in_file_class == pb::JavaFeatures::NestInFileClassFeature::LEGACY) {
110+
return !descriptor.file()->options().java_multiple_files();
111+
}
112+
return nest_in_file_class == pb::JavaFeatures::NestInFileClassFeature::YES;
113+
}
114+
97115
} // namespace
98116

99117
std::string QualifiedClassName(const Descriptor* descriptor) {
@@ -161,24 +179,34 @@ std::string UnderscoresToCamelCaseCheckReserved(const FieldDescriptor* field) {
161179
return name;
162180
}
163181

164-
PROTOC_EXPORT std::string KotlinFactoryName(const Descriptor* descriptor) {
182+
std::string KotlinFactoryName(const Descriptor* descriptor) {
165183
ClassNameResolver name_resolver;
166184
return name_resolver.GetKotlinFactoryName(descriptor);
167185
}
168186

169-
PROTOC_EXPORT std::string FullyQualifiedKotlinFactoryName(
170-
const Descriptor* descriptor) {
187+
std::string FullyQualifiedKotlinFactoryName(const Descriptor* descriptor) {
171188
ClassNameResolver name_resolver;
172189
return name_resolver.GetFullyQualifiedKotlinFactoryName(descriptor);
173190
}
174191

175-
PROTOC_EXPORT std::string KotlinExtensionsClassName(
176-
const Descriptor* descriptor) {
192+
std::string KotlinExtensionsClassName(const Descriptor* descriptor) {
177193
ClassNameResolver name_resolver;
178194
return name_resolver.GetKotlinExtensionsClassName(descriptor);
179195
}
180196

181197

198+
bool NestedInFileClass(const Descriptor& message) {
199+
return NestedInFileClassImpl(message);
200+
}
201+
202+
bool NestedInFileClass(const EnumDescriptor& enm) {
203+
return NestedInFileClassImpl(enm);
204+
}
205+
206+
bool NestedInFileClass(const ServiceDescriptor& service) {
207+
return NestedInFileClassImpl(service);
208+
}
209+
182210
} // namespace java
183211
} // namespace compiler
184212
} // namespace protobuf

src/google/protobuf/compiler/java/names.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,27 @@ PROTOC_EXPORT std::string KotlinExtensionsClassName(
187187
const Descriptor* descriptor);
188188

189189

190+
// Requires:
191+
// descriptor != NULL
192+
// Returns:
193+
// True if the generated message class should be nested in the generated proto
194+
// file Java class.
195+
PROTOC_EXPORT bool NestedInFileClass(const Descriptor& message);
196+
197+
// Requires:
198+
// descriptor != NULL
199+
// Returns:
200+
// True if the generated enum class should be nested in the generated proto
201+
// file Java class.
202+
PROTOC_EXPORT bool NestedInFileClass(const EnumDescriptor& enm);
203+
204+
// Requires:
205+
// descriptor != NULL
206+
// Returns:
207+
// True if the generated service class should be nested in the generated proto
208+
// file Java class.
209+
PROTOC_EXPORT bool NestedInFileClass(const ServiceDescriptor& service);
210+
190211
} // namespace java
191212
} // namespace compiler
192213
} // namespace protobuf

0 commit comments

Comments
 (0)