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: liujisi@google.com (Pherl Liu) 9 10 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__ 11 #define GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__ 12 13 #include <memory> 14 #include <string> 15 16 #include "absl/container/btree_map.h" 17 #include "absl/container/flat_hash_map.h" 18 #include "absl/strings/string_view.h" 19 #include "google/protobuf/compiler/java/context.h" 20 #include "google/protobuf/descriptor.h" 21 22 namespace google { 23 namespace protobuf { 24 namespace compiler { 25 namespace java { 26 27 class MessageGenerator { 28 public: 29 explicit MessageGenerator(const Descriptor* descriptor); 30 MessageGenerator(const MessageGenerator&) = delete; 31 MessageGenerator& operator=(const MessageGenerator&) = delete; 32 virtual ~MessageGenerator(); 33 34 // All static variables have to be declared at the top-level of the file 35 // so that we can control initialization order, which is important for 36 // DescriptorProto bootstrapping to work. 37 virtual void GenerateStaticVariables(io::Printer* printer, 38 int* bytecode_estimate) = 0; 39 40 // Output code which initializes the static variables generated by 41 // GenerateStaticVariables(). Returns an estimate of bytecode size. 42 virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0; 43 44 // Generate the class itself. 45 virtual void Generate(io::Printer* printer) = 0; 46 47 // Generates the base interface that both the class and its builder 48 // implement 49 virtual void GenerateInterface(io::Printer* printer) = 0; 50 51 // Generate code to register all contained extensions with an 52 // ExtensionRegistry. 53 virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0; 54 55 protected: 56 const Descriptor* descriptor_; 57 absl::btree_map<int, const OneofDescriptor*> oneofs_; 58 }; 59 60 class EnumGenerator { 61 public: 62 virtual ~EnumGenerator() = default; 63 virtual void Generate(io::Printer* printer) = 0; 64 }; 65 66 // Generates code for an extension, which may be within the scope of some 67 // message or may be at file scope. This is much simpler than FieldGenerator 68 // since extensions are just simple identifiers with interesting types. 69 class ExtensionGenerator { 70 public: 71 virtual ~ExtensionGenerator() = default; 72 73 virtual void Generate(io::Printer* printer) = 0; 74 75 // Returns an estimate of the number of bytes the printed code will compile 76 // to 77 virtual int GenerateNonNestedInitializationCode(io::Printer* printer) = 0; 78 79 // Returns an estimate of the number of bytes the printed code will compile 80 // to 81 virtual int GenerateRegistrationCode(io::Printer* printer) = 0; 82 83 protected: 84 static void InitTemplateVars( 85 const FieldDescriptor* descriptor, const std::string& scope, 86 bool immutable, ClassNameResolver* name_resolver, 87 absl::flat_hash_map<absl::string_view, std::string>* vars_pointer, 88 Context* context); 89 }; 90 91 class ServiceGenerator { 92 public: ServiceGenerator(const ServiceDescriptor * descriptor)93 explicit ServiceGenerator(const ServiceDescriptor* descriptor) 94 : descriptor_(descriptor) {} 95 virtual ~ServiceGenerator() = default; 96 97 ServiceGenerator(const ServiceGenerator&) = delete; 98 ServiceGenerator& operator=(const ServiceGenerator&) = delete; 99 100 virtual void Generate(io::Printer* printer) = 0; 101 102 enum RequestOrResponse { REQUEST, RESPONSE }; 103 enum IsAbstract { IS_ABSTRACT, IS_CONCRETE }; 104 105 protected: 106 const ServiceDescriptor* descriptor_; 107 }; 108 109 // An interface to create generators for a given descriptor. This interface 110 // is implemented for every variant of the Java API (Mutable, Immutable, Lite). 111 class GeneratorFactory { 112 public: 113 virtual ~GeneratorFactory() = default; 114 115 virtual std::unique_ptr<MessageGenerator> NewMessageGenerator( 116 const Descriptor* descriptor) const = 0; 117 118 virtual std::unique_ptr<EnumGenerator> NewEnumGenerator( 119 const EnumDescriptor* descriptor) const = 0; 120 121 virtual std::unique_ptr<ExtensionGenerator> NewExtensionGenerator( 122 const FieldDescriptor* descriptor) const = 0; 123 124 virtual std::unique_ptr<ServiceGenerator> NewServiceGenerator( 125 const ServiceDescriptor* descriptor) const = 0; 126 }; 127 128 } // namespace java 129 } // namespace compiler 130 } // namespace protobuf 131 } // namespace google 132 133 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__ 134