• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FILE_H__
13 #define GOOGLE_PROTOBUF_COMPILER_JAVA_FILE_H__
14 
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include "google/protobuf/compiler/java/options.h"
20 #include "google/protobuf/port.h"
21 
22 namespace google {
23 namespace protobuf {
24 class FileDescriptor;  // descriptor.h
25 namespace io {
26 class Printer;  // printer.h
27 }
28 namespace compiler {
29 class GeneratorContext;  // code_generator.h
30 namespace java {
31 class Context;             // context.h
32 class MessageGenerator;    // message.h
33 class GeneratorFactory;    // generator_factory.h
34 class ExtensionGenerator;  // extension.h
35 class ClassNameResolver;   // name_resolver.h
36 }  // namespace java
37 }  // namespace compiler
38 }  // namespace protobuf
39 }  // namespace google
40 
41 namespace google {
42 namespace protobuf {
43 namespace compiler {
44 namespace java {
45 
46 class FileGenerator {
47  public:
48   FileGenerator(const FileDescriptor* file, const Options& options,
49                 bool immutable_api = true);
50   FileGenerator(const FileGenerator&) = delete;
51   FileGenerator& operator=(const FileGenerator&) = delete;
52   ~FileGenerator();
53 
54   // Checks for problems that would otherwise lead to cryptic compile errors.
55   // Returns true if there are no problems, or writes an error description to
56   // the given string and returns false otherwise.
57   bool Validate(std::string* error);
58 
59   void Generate(io::Printer* printer);
60 
61   // If we aren't putting everything into one file, this will write all the
62   // files other than the outer file (i.e. one for each message, enum, and
63   // service type).
64   void GenerateSiblings(const std::string& package_dir,
65                         GeneratorContext* generator_context,
66                         std::vector<std::string>* file_list,
67                         std::vector<std::string>* annotation_list);
68 
java_package()69   const std::string& java_package() { return java_package_; }
classname()70   const std::string& classname() { return classname_; }
71 
72  private:
73   void GenerateDescriptorInitializationCodeForImmutable(io::Printer* printer);
74 
75   bool ShouldIncludeDependency(const FileDescriptor* descriptor,
76                                bool immutable_api_);
77 
78   const FileDescriptor* file_;
79   std::string java_package_;
80   std::string classname_;
81 
82   std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
83   std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
84   std::unique_ptr<Context> context_;
85   std::unique_ptr<GeneratorFactory> generator_factory_;
86   ClassNameResolver* name_resolver_;
87   const Options options_;
88   bool immutable_api_;
89 };
90 
91 }  // namespace java
92 }  // namespace compiler
93 }  // namespace protobuf
94 }  // namespace google
95 
96 #endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_FILE_H__
97