1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // Author: kenton@google.com (Kenton Varda)
9 // Based on original Protocol Buffers design by
10 // Sanjay Ghemawat, Jeff Dean, and others.
11
12 #include "google/protobuf/compiler/java/file.h"
13
14 #include <memory>
15 #include <string>
16 #include <vector>
17
18 #include "absl/container/btree_set.h"
19 #include "absl/log/absl_check.h"
20 #include "absl/log/absl_log.h"
21 #include "absl/strings/str_cat.h"
22 #include "absl/strings/string_view.h"
23 #include "google/protobuf/compiler/code_generator.h"
24 #include "google/protobuf/compiler/java/context.h"
25 #include "google/protobuf/compiler/java/generator_common.h"
26 #include "google/protobuf/compiler/java/generator_factory.h"
27 #include "google/protobuf/compiler/java/helpers.h"
28 #include "google/protobuf/compiler/java/full/generator_factory.h"
29 #include "google/protobuf/compiler/java/lite/generator_factory.h"
30 #include "google/protobuf/compiler/java/name_resolver.h"
31 #include "google/protobuf/compiler/java/options.h"
32 #include "google/protobuf/compiler/java/shared_code_generator.h"
33 #include "google/protobuf/compiler/retention.h"
34 #include "google/protobuf/compiler/versions.h"
35 #include "google/protobuf/descriptor.pb.h"
36 #include "google/protobuf/dynamic_message.h"
37 #include "google/protobuf/io/printer.h"
38 #include "google/protobuf/io/zero_copy_stream.h"
39
40
41 // Must be last.
42 #include "google/protobuf/port_def.inc"
43
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace java {
48
49 namespace {
50
51 struct FieldDescriptorCompare {
operator ()google::protobuf::compiler::java::__anon8e3acb160111::FieldDescriptorCompare52 bool operator()(const FieldDescriptor* f1, const FieldDescriptor* f2) const {
53 if (f1 == nullptr) {
54 return false;
55 }
56 if (f2 == nullptr) {
57 return true;
58 }
59 return f1->full_name() < f2->full_name();
60 }
61 };
62
63 using FieldDescriptorSet =
64 absl::btree_set<const FieldDescriptor*, FieldDescriptorCompare>;
65
66 // Recursively searches the given message to collect extensions.
67 // Returns true if all the extensions can be recognized. The extensions will be
68 // appended in to the extensions parameter.
69 // Returns false when there are unknown fields, in which case the data in the
70 // extensions output parameter is not reliable and should be discarded.
CollectExtensions(const Message & message,FieldDescriptorSet * extensions)71 bool CollectExtensions(const Message& message, FieldDescriptorSet* extensions) {
72 const Reflection* reflection = message.GetReflection();
73
74 // There are unknown fields that could be extensions, thus this call fails.
75 if (reflection->GetUnknownFields(message).field_count() > 0) return false;
76
77 std::vector<const FieldDescriptor*> fields;
78 reflection->ListFields(message, &fields);
79
80 for (int i = 0; i < fields.size(); i++) {
81 if (fields[i]->is_extension()) {
82 extensions->insert(fields[i]);
83 }
84
85 if (GetJavaType(fields[i]) == JAVATYPE_MESSAGE) {
86 if (fields[i]->is_repeated()) {
87 int size = reflection->FieldSize(message, fields[i]);
88 for (int j = 0; j < size; j++) {
89 const Message& sub_message =
90 reflection->GetRepeatedMessage(message, fields[i], j);
91 if (!CollectExtensions(sub_message, extensions)) return false;
92 }
93 } else {
94 const Message& sub_message = reflection->GetMessage(message, fields[i]);
95 if (!CollectExtensions(sub_message, extensions)) return false;
96 }
97 }
98 }
99
100 return true;
101 }
102
103 // Finds all extensions in the given message and its sub-messages. If the
104 // message contains unknown fields (which could be extensions), then those
105 // extensions are defined in alternate_pool.
106 // The message will be converted to a DynamicMessage backed by alternate_pool
107 // in order to handle this case.
CollectExtensions(const FileDescriptorProto & file_proto,const DescriptorPool & alternate_pool,FieldDescriptorSet * extensions,const std::string & file_data)108 void CollectExtensions(const FileDescriptorProto& file_proto,
109 const DescriptorPool& alternate_pool,
110 FieldDescriptorSet* extensions,
111 const std::string& file_data) {
112 if (!CollectExtensions(file_proto, extensions)) {
113 // There are unknown fields in the file_proto, which are probably
114 // extensions. We need to parse the data into a dynamic message based on the
115 // builder-pool to find out all extensions.
116 const Descriptor* file_proto_desc = alternate_pool.FindMessageTypeByName(
117 file_proto.GetDescriptor()->full_name());
118 ABSL_CHECK(file_proto_desc)
119 << "Find unknown fields in FileDescriptorProto when building "
120 << file_proto.name()
121 << ". It's likely that those fields are custom options, however, "
122 "descriptor.proto is not in the transitive dependencies. "
123 "This normally should not happen. Please report a bug.";
124 DynamicMessageFactory factory;
125 std::unique_ptr<Message> dynamic_file_proto(
126 factory.GetPrototype(file_proto_desc)->New());
127 ABSL_CHECK(dynamic_file_proto.get() != nullptr);
128 ABSL_CHECK(dynamic_file_proto->ParseFromString(file_data));
129
130 // Collect the extensions again from the dynamic message. There should be no
131 // more unknown fields this time, i.e. all the custom options should be
132 // parsed as extensions now.
133 extensions->clear();
134 ABSL_CHECK(CollectExtensions(*dynamic_file_proto, extensions))
135 << "Find unknown fields in FileDescriptorProto when building "
136 << file_proto.name()
137 << ". It's likely that those fields are custom options, however, "
138 "those options cannot be recognized in the builder pool. "
139 "This normally should not happen. Please report a bug.";
140 }
141 }
142
143 // Our static initialization methods can become very, very large.
144 // So large that if we aren't careful we end up blowing the JVM's
145 // 64K bytes of bytecode/method. Fortunately, since these static
146 // methods are executed only once near the beginning of a program,
147 // there's usually plenty of stack space available and we can
148 // extend our methods by simply chaining them to another method
149 // with a tail call. This inserts the sequence call-next-method,
150 // end this one, begin-next-method as needed.
MaybeRestartJavaMethod(io::Printer * printer,int * bytecode_estimate,int * method_num,const char * chain_statement,const char * method_decl)151 void MaybeRestartJavaMethod(io::Printer* printer, int* bytecode_estimate,
152 int* method_num, const char* chain_statement,
153 const char* method_decl) {
154 // The goal here is to stay under 64K bytes of jvm bytecode/method,
155 // since otherwise we hit a hardcoded limit in the jvm and javac will
156 // then fail with the error "code too large". This limit lets our
157 // estimates be off by a factor of two and still we're okay.
158 static const int bytesPerMethod = kMaxStaticSize;
159
160 if ((*bytecode_estimate) > bytesPerMethod) {
161 ++(*method_num);
162 printer->Print(chain_statement, "method_num", absl::StrCat(*method_num));
163 printer->Outdent();
164 printer->Print("}\n");
165 printer->Print(method_decl, "method_num", absl::StrCat(*method_num));
166 printer->Indent();
167 *bytecode_estimate = 0;
168 }
169 }
170
CreateGeneratorFactory(const FileDescriptor * file,const Options & options,Context * context,bool immutable_api)171 std::unique_ptr<GeneratorFactory> CreateGeneratorFactory(
172 const FileDescriptor* file, const Options& options, Context* context,
173 bool immutable_api) {
174 ABSL_CHECK(immutable_api)
175 << "Open source release does not support the mutable API";
176 if (HasDescriptorMethods(file, context->EnforceLite())) {
177 return MakeImmutableGeneratorFactory(context);
178 } else {
179 return MakeImmutableLiteGeneratorFactory(context);
180 }
181 }
182 } // namespace
183
FileGenerator(const FileDescriptor * file,const Options & options,bool immutable_api)184 FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options,
185 bool immutable_api)
186 : file_(file),
187 java_package_(FileJavaPackage(file, immutable_api, options)),
188 message_generators_(file->message_type_count()),
189 extension_generators_(file->extension_count()),
190 context_(new Context(file, options)),
191 generator_factory_(
192 CreateGeneratorFactory(file, options, context_.get(), immutable_api)),
193 name_resolver_(context_->GetNameResolver()),
194 options_(options),
195 immutable_api_(immutable_api) {
196 classname_ = name_resolver_->GetFileClassName(file, immutable_api);
197 for (int i = 0; i < file_->message_type_count(); ++i) {
198 message_generators_[i] =
199 generator_factory_->NewMessageGenerator(file_->message_type(i));
200 }
201 for (int i = 0; i < file_->extension_count(); ++i) {
202 extension_generators_[i] =
203 generator_factory_->NewExtensionGenerator(file_->extension(i));
204 }
205 }
206
~FileGenerator()207 FileGenerator::~FileGenerator() {}
208
Validate(std::string * error)209 bool FileGenerator::Validate(std::string* error) {
210 // Check that no class name matches the file's class name. This is a common
211 // problem that leads to Java compile errors that can be hard to understand.
212 // It's especially bad when using the java_multiple_files, since we would
213 // end up overwriting the outer class with one of the inner ones.
214 if (name_resolver_->HasConflictingClassName(file_, classname_,
215 NameEquality::EXACT_EQUAL)) {
216 error->assign(file_->name());
217 error->append(
218 ": Cannot generate Java output because the file's outer class name, "
219 "\"");
220 error->append(classname_);
221 error->append(
222 "\", matches the name of one of the types declared inside it. "
223 "Please either rename the type or use the java_outer_classname "
224 "option to specify a different outer class name for the .proto file.");
225 return false;
226 }
227 // Similar to the check above, but ignore the case this time. This is not a
228 // problem on Linux, but will lead to Java compile errors on Windows / Mac
229 // because filenames are case-insensitive on those platforms.
230 if (name_resolver_->HasConflictingClassName(
231 file_, classname_, NameEquality::EQUAL_IGNORE_CASE)) {
232 ABSL_LOG(WARNING)
233 << file_->name() << ": The file's outer class name, \"" << classname_
234 << "\", matches the name of one of the types declared inside it when "
235 << "case is ignored. This can cause compilation issues on Windows / "
236 << "MacOS. Please either rename the type or use the "
237 << "java_outer_classname option to specify a different outer class "
238 << "name for the .proto file to be safe.";
239 }
240
241 // Print a warning if optimize_for = LITE_RUNTIME is used.
242 if (file_->options().optimize_for() == FileOptions::LITE_RUNTIME &&
243 !options_.enforce_lite) {
244 ABSL_LOG(WARNING)
245 << "The optimize_for = LITE_RUNTIME option is no longer supported by "
246 << "protobuf Java code generator and is ignored--protoc will always "
247 << "generate full runtime code for Java. To use Java Lite runtime, "
248 << "users should use the Java Lite plugin instead. See:\n"
249 << " "
250 "https://github.com/protocolbuffers/protobuf/blob/main/java/"
251 "lite.md";
252 }
253 return true;
254 }
255
Generate(io::Printer * printer)256 void FileGenerator::Generate(io::Printer* printer) {
257 // We don't import anything because we refer to all classes by their
258 // fully-qualified names in the generated source.
259 printer->Print(
260 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
261 "// NO CHECKED-IN PROTOBUF "
262 // Intentional line breaker
263 "GENCODE\n"
264 "// source: $filename$\n",
265 "filename", file_->name());
266 if (options_.opensource_runtime) {
267 printer->Print("// Protobuf Java Version: $protobuf_java_version$\n",
268 "protobuf_java_version", PROTOBUF_JAVA_VERSION_STRING);
269 }
270 printer->Print("\n");
271 if (!java_package_.empty()) {
272 printer->Print(
273 "package $package$;\n"
274 "\n",
275 "package", java_package_);
276 }
277 PrintGeneratedAnnotation(
278 printer, '$',
279 options_.annotate_code ? absl::StrCat(classname_, ".java.pb.meta") : "",
280 options_);
281
282 if (!options_.opensource_runtime) {
283 printer->Print("@com.google.protobuf.Internal.ProtoNonnullApi\n");
284 }
285 printer->Print(
286 "$deprecation$public final class $classname$ {\n"
287 " private $ctor$() {}\n",
288 "deprecation",
289 file_->options().deprecated() ? "@java.lang.Deprecated " : "",
290 "classname", classname_, "ctor", classname_);
291 printer->Annotate("classname", file_->name());
292 printer->Indent();
293
294 if (!context_->EnforceLite()) {
295 printer->Print("static {\n");
296 printer->Indent();
297 PrintGencodeVersionValidator(printer, options_.opensource_runtime,
298 classname_);
299 printer->Outdent();
300 printer->Print("}\n");
301 }
302
303 // -----------------------------------------------------------------
304
305 printer->Print(
306 "public static void registerAllExtensions(\n"
307 " com.google.protobuf.ExtensionRegistryLite registry) {\n");
308
309 printer->Indent();
310
311 for (int i = 0; i < file_->extension_count(); i++) {
312 extension_generators_[i]->GenerateRegistrationCode(printer);
313 }
314
315 for (int i = 0; i < file_->message_type_count(); i++) {
316 message_generators_[i]->GenerateExtensionRegistrationCode(printer);
317 }
318
319 printer->Outdent();
320 printer->Print("}\n");
321 if (HasDescriptorMethods(file_, context_->EnforceLite())) {
322 // Overload registerAllExtensions for the non-lite usage to
323 // redundantly maintain the original signature (this is
324 // redundant because ExtensionRegistryLite now invokes
325 // ExtensionRegistry in the non-lite usage). Intent is
326 // to remove this in the future.
327 printer->Print(
328 "\n"
329 "public static void registerAllExtensions(\n"
330 " com.google.protobuf.ExtensionRegistry registry) {\n"
331 " registerAllExtensions(\n"
332 " (com.google.protobuf.ExtensionRegistryLite) registry);\n"
333 "}\n");
334 }
335
336 // -----------------------------------------------------------------
337
338 if (!MultipleJavaFiles(file_, immutable_api_)) {
339 for (int i = 0; i < file_->enum_type_count(); i++) {
340 generator_factory_->NewEnumGenerator(file_->enum_type(i))
341 ->Generate(printer);
342 }
343 for (int i = 0; i < file_->message_type_count(); i++) {
344 message_generators_[i]->GenerateInterface(printer);
345 message_generators_[i]->Generate(printer);
346 }
347 if (HasGenericServices(file_, context_->EnforceLite())) {
348 for (int i = 0; i < file_->service_count(); i++) {
349 std::unique_ptr<ServiceGenerator> generator(
350 generator_factory_->NewServiceGenerator(file_->service(i)));
351 generator->Generate(printer);
352 }
353 }
354 }
355
356 // Extensions must be generated in the outer class since they are values,
357 // not classes.
358 for (int i = 0; i < file_->extension_count(); i++) {
359 extension_generators_[i]->Generate(printer);
360 }
361
362 // Static variables. We'd like them to be final if possible, but due to
363 // the JVM's 64k size limit on static blocks, we have to initialize some
364 // of them in methods; thus they cannot be final.
365 int static_block_bytecode_estimate = 0;
366 for (int i = 0; i < file_->message_type_count(); i++) {
367 message_generators_[i]->GenerateStaticVariables(
368 printer, &static_block_bytecode_estimate);
369 }
370
371 printer->Print("\n");
372
373 if (HasDescriptorMethods(file_, context_->EnforceLite())) {
374 if (immutable_api_) {
375 GenerateDescriptorInitializationCodeForImmutable(printer);
376 }
377 } else {
378 printer->Print("static {\n");
379 printer->Indent();
380 int bytecode_estimate = 0;
381 int method_num = 0;
382
383 for (int i = 0; i < file_->message_type_count(); i++) {
384 bytecode_estimate +=
385 message_generators_[i]->GenerateStaticVariableInitializers(printer);
386 MaybeRestartJavaMethod(
387 printer, &bytecode_estimate, &method_num,
388 "_clinit_autosplit_$method_num$();\n",
389 "private static void _clinit_autosplit_$method_num$() {\n");
390 }
391
392 printer->Outdent();
393 printer->Print("}\n");
394 }
395
396 printer->Print(
397 "\n"
398 "// @@protoc_insertion_point(outer_class_scope)\n");
399
400 printer->Outdent();
401 printer->Print("}\n");
402 }
403
GenerateDescriptorInitializationCodeForImmutable(io::Printer * printer)404 void FileGenerator::GenerateDescriptorInitializationCodeForImmutable(
405 io::Printer* printer) {
406 printer->Print(
407 "public static com.google.protobuf.Descriptors.FileDescriptor\n"
408 " getDescriptor() {\n"
409 " return descriptor;\n"
410 "}\n"
411 "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n"
412 " descriptor;\n"
413 "static {\n",
414 // TODO: Mark this as final.
415 "final", options_.opensource_runtime ? "" : "final");
416 printer->Indent();
417
418 if (options_.opensource_runtime) {
419 SharedCodeGenerator shared_code_generator(file_, options_);
420 shared_code_generator.GenerateDescriptors(printer);
421 } else {
422 }
423
424 int bytecode_estimate = 0;
425 int method_num = 0;
426
427 for (int i = 0; i < file_->message_type_count(); i++) {
428 bytecode_estimate +=
429 message_generators_[i]->GenerateStaticVariableInitializers(printer);
430 MaybeRestartJavaMethod(
431 printer, &bytecode_estimate, &method_num,
432 "_clinit_autosplit_dinit_$method_num$();\n",
433 "private static void _clinit_autosplit_dinit_$method_num$() {\n");
434 }
435 for (int i = 0; i < file_->extension_count(); i++) {
436 bytecode_estimate +=
437 extension_generators_[i]->GenerateNonNestedInitializationCode(printer);
438 MaybeRestartJavaMethod(
439 printer, &bytecode_estimate, &method_num,
440 "_clinit_autosplit_dinit_$method_num$();\n",
441 "private static void _clinit_autosplit_dinit_$method_num$() {\n");
442 }
443 // Feature resolution for Java features uses extension registry
444 // which must happen after internalInit() from
445 // GenerateNonNestedInitializationCode
446 printer->Print("descriptor.resolveAllFeaturesImmutable();\n");
447
448 // Proto compiler builds a DescriptorPool, which holds all the descriptors to
449 // generate, when processing the ".proto" files. We call this DescriptorPool
450 // the parsed pool (a.k.a. file_->pool()).
451 //
452 // Note that when users try to extend the (.*)DescriptorProto in their
453 // ".proto" files, it does not affect the pre-built FileDescriptorProto class
454 // in proto compiler. When we put the descriptor data in the file_proto, those
455 // extensions become unknown fields.
456 //
457 // Now we need to find out all the extension value to the (.*)DescriptorProto
458 // in the file_proto message, and prepare an ExtensionRegistry to return.
459 //
460 // To find those extensions, we need to parse the data into a dynamic message
461 // of the FileDescriptor based on the builder-pool, then we can use
462 // reflections to find all extension fields
463 FileDescriptorProto file_proto = StripSourceRetentionOptions(*file_);
464 std::string file_data;
465 file_proto.SerializeToString(&file_data);
466 FieldDescriptorSet extensions;
467 CollectExtensions(file_proto, *file_->pool(), &extensions, file_data);
468
469 if (options_.strip_nonfunctional_codegen) {
470 // Skip feature extensions, which are a visible (but non-functional)
471 // deviation between editions and legacy syntax.
472 absl::erase_if(extensions, [](const FieldDescriptor* field) {
473 return field->containing_type()->full_name() == "google.protobuf.FeatureSet";
474 });
475 }
476
477 // Force descriptor initialization of all dependencies.
478 for (int i = 0; i < file_->dependency_count(); i++) {
479 if (ShouldIncludeDependency(file_->dependency(i), true)) {
480 std::string dependency =
481 name_resolver_->GetImmutableClassName(file_->dependency(i));
482 printer->Print("$dependency$.getDescriptor();\n", "dependency",
483 dependency);
484 }
485 }
486
487 if (!extensions.empty()) {
488 // Must construct an ExtensionRegistry containing all existing extensions
489 // and use it to parse the descriptor data again to recognize extensions.
490 printer->Print(
491 "com.google.protobuf.ExtensionRegistry registry =\n"
492 " com.google.protobuf.ExtensionRegistry.newInstance();\n");
493 FieldDescriptorSet::iterator it;
494 for (const FieldDescriptor* field : extensions) {
495 std::unique_ptr<ExtensionGenerator> generator(
496 generator_factory_->NewExtensionGenerator(field));
497 bytecode_estimate += generator->GenerateRegistrationCode(printer);
498 MaybeRestartJavaMethod(
499 printer, &bytecode_estimate, &method_num,
500 "_clinit_autosplit_dinit_$method_num$(registry);\n",
501 "private static void _clinit_autosplit_dinit_$method_num$(\n"
502 " com.google.protobuf.ExtensionRegistry registry) {\n");
503 }
504 printer->Print(
505 "com.google.protobuf.Descriptors.FileDescriptor\n"
506 " .internalUpdateFileDescriptor(descriptor, registry);\n");
507 }
508
509 printer->Outdent();
510 printer->Print("}\n");
511 }
512
513
514 template <typename GeneratorClass, typename DescriptorClass>
GenerateSibling(const std::string & package_dir,const std::string & java_package,const DescriptorClass * descriptor,GeneratorContext * context,std::vector<std::string> * file_list,bool annotate_code,std::vector<std::string> * annotation_list,const std::string & name_suffix,GeneratorClass * generator,bool opensource_runtime,void (GeneratorClass::* pfn)(io::Printer * printer))515 static void GenerateSibling(
516 const std::string& package_dir, const std::string& java_package,
517 const DescriptorClass* descriptor, GeneratorContext* context,
518 std::vector<std::string>* file_list, bool annotate_code,
519 std::vector<std::string>* annotation_list, const std::string& name_suffix,
520 GeneratorClass* generator, bool opensource_runtime,
521 void (GeneratorClass::*pfn)(io::Printer* printer)) {
522 std::string filename =
523 absl::StrCat(package_dir, descriptor->name(), name_suffix, ".java");
524 file_list->push_back(filename);
525 std::string info_full_path = absl::StrCat(filename, ".pb.meta");
526 GeneratedCodeInfo annotations;
527 io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
528 &annotations);
529
530 std::unique_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
531 io::Printer printer(output.get(), '$',
532 annotate_code ? &annotation_collector : nullptr);
533
534 printer.Print(
535 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
536 "// NO CHECKED-IN PROTOBUF "
537 // Intentional line breaker
538 "GENCODE\n"
539 "// source: $filename$\n",
540 "filename", descriptor->file()->name());
541 if (opensource_runtime) {
542 printer.Print("// Protobuf Java Version: $protobuf_java_version$\n",
543 "protobuf_java_version", PROTOBUF_JAVA_VERSION_STRING);
544 }
545 printer.Print("\n");
546 if (!java_package.empty()) {
547 printer.Print(
548 "package $package$;\n"
549 "\n",
550 "package", java_package);
551 }
552
553 (generator->*pfn)(&printer);
554
555 if (annotate_code) {
556 std::unique_ptr<io::ZeroCopyOutputStream> info_output(
557 context->Open(info_full_path));
558 annotations.SerializeToZeroCopyStream(info_output.get());
559 annotation_list->push_back(info_full_path);
560 }
561 }
562
GenerateSiblings(const std::string & package_dir,GeneratorContext * context,std::vector<std::string> * file_list,std::vector<std::string> * annotation_list)563 void FileGenerator::GenerateSiblings(
564 const std::string& package_dir, GeneratorContext* context,
565 std::vector<std::string>* file_list,
566 std::vector<std::string>* annotation_list) {
567 if (MultipleJavaFiles(file_, immutable_api_)) {
568 for (int i = 0; i < file_->enum_type_count(); i++) {
569 std::unique_ptr<EnumGenerator> generator(
570 generator_factory_->NewEnumGenerator(file_->enum_type(i)));
571 GenerateSibling<EnumGenerator>(
572 package_dir, java_package_, file_->enum_type(i), context, file_list,
573 options_.annotate_code, annotation_list, "", generator.get(),
574 options_.opensource_runtime, &EnumGenerator::Generate);
575 }
576 for (int i = 0; i < file_->message_type_count(); i++) {
577 if (immutable_api_) {
578 GenerateSibling<MessageGenerator>(
579 package_dir, java_package_, file_->message_type(i), context,
580 file_list, options_.annotate_code, annotation_list, "OrBuilder",
581 message_generators_[i].get(), options_.opensource_runtime,
582 &MessageGenerator::GenerateInterface);
583 }
584 GenerateSibling<MessageGenerator>(
585 package_dir, java_package_, file_->message_type(i), context,
586 file_list, options_.annotate_code, annotation_list, "",
587 message_generators_[i].get(), options_.opensource_runtime,
588 &MessageGenerator::Generate);
589 }
590 if (HasGenericServices(file_, context_->EnforceLite())) {
591 for (int i = 0; i < file_->service_count(); i++) {
592 std::unique_ptr<ServiceGenerator> generator(
593 generator_factory_->NewServiceGenerator(file_->service(i)));
594 GenerateSibling<ServiceGenerator>(
595 package_dir, java_package_, file_->service(i), context, file_list,
596 options_.annotate_code, annotation_list, "", generator.get(),
597 options_.opensource_runtime, &ServiceGenerator::Generate);
598 }
599 }
600 }
601 }
602
ShouldIncludeDependency(const FileDescriptor * descriptor,bool immutable_api)603 bool FileGenerator::ShouldIncludeDependency(const FileDescriptor* descriptor,
604 bool immutable_api) {
605 // Skip feature imports, which are a visible (but non-functional) deviation
606 // between editions and legacy syntax.
607 if (options_.strip_nonfunctional_codegen &&
608 IsKnownFeatureProto(descriptor->name())) {
609 return false;
610 }
611
612 return true;
613 }
614
615 } // namespace java
616 } // namespace compiler
617 } // namespace protobuf
618 } // namespace google
619
620 #include "google/protobuf/port_undef.inc"
621