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 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_CONTEXT_H__ 9 #define GOOGLE_PROTOBUF_COMPILER_JAVA_CONTEXT_H__ 10 11 #include <memory> 12 #include <vector> 13 14 #include "absl/container/flat_hash_map.h" 15 #include "google/protobuf/compiler/java/helpers.h" 16 #include "google/protobuf/compiler/java/options.h" 17 #include "google/protobuf/port.h" 18 19 namespace google { 20 namespace protobuf { 21 class FileDescriptor; 22 class FieldDescriptor; 23 class OneofDescriptor; 24 class Descriptor; 25 class EnumDescriptor; 26 namespace compiler { 27 namespace java { 28 class ClassNameResolver; // name_resolver.h 29 } 30 } // namespace compiler 31 } // namespace protobuf 32 } // namespace google 33 34 namespace google { 35 namespace protobuf { 36 namespace compiler { 37 namespace java { 38 39 struct FieldGeneratorInfo; 40 struct OneofGeneratorInfo; 41 // A context object holds the information that is shared among all code 42 // generators. 43 class Context { 44 public: 45 Context(const FileDescriptor* file, const Options& options); 46 Context(const Context&) = delete; 47 Context& operator=(const Context&) = delete; 48 ~Context(); 49 50 // Get the name resolver associated with this context. The resolver 51 // can be used to map descriptors to Java class names. 52 ClassNameResolver* GetNameResolver() const; 53 54 // Get the FieldGeneratorInfo for a given field. 55 const FieldGeneratorInfo* GetFieldGeneratorInfo( 56 const FieldDescriptor* field) const; 57 58 // Get the OneofGeneratorInfo for a given oneof. 59 const OneofGeneratorInfo* GetOneofGeneratorInfo( 60 const OneofDescriptor* oneof) const; 61 options()62 const Options& options() const { return options_; } 63 64 // Enforces all the files (including transitive dependencies) to use 65 // LiteRuntime. 66 EnforceLite()67 bool EnforceLite() const { return options_.enforce_lite; } 68 69 // Does this message class have generated parsing, serialization, and other 70 // standard methods for which reflection-based fallback implementations exist? 71 bool HasGeneratedMethods(const Descriptor* descriptor) const; 72 73 private: 74 void InitializeFieldGeneratorInfo(const FileDescriptor* file); 75 void InitializeFieldGeneratorInfoForMessage(const Descriptor* message); 76 void InitializeFieldGeneratorInfoForFields( 77 const std::vector<const FieldDescriptor*>& fields); 78 79 std::unique_ptr<ClassNameResolver> name_resolver_; 80 absl::flat_hash_map<const FieldDescriptor*, FieldGeneratorInfo> 81 field_generator_info_map_; 82 absl::flat_hash_map<const OneofDescriptor*, OneofGeneratorInfo> 83 oneof_generator_info_map_; 84 Options options_; 85 }; 86 87 template <typename Descriptor> 88 void MaybePrintGeneratedAnnotation(Context* context, io::Printer* printer, 89 Descriptor* descriptor, bool immutable, 90 const std::string& suffix = "") { 91 if (IsOwnFile(descriptor, immutable)) { 92 PrintGeneratedAnnotation(printer, '$', 93 context->options().annotate_code 94 ? AnnotationFileName(descriptor, suffix) 95 : "", 96 context->options()); 97 } 98 } 99 100 101 } // namespace java 102 } // namespace compiler 103 } // namespace protobuf 104 } // namespace google 105 106 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_CONTEXT_H__ 107