blob: 080b342412ee8e99ccdadb363ae671b31f55cb7b [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@google.com (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34
35#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FILE_H__
36#define GOOGLE_PROTOBUF_COMPILER_JAVA_FILE_H__
37
jieluo@google.com4de8f552014-07-18 00:47:59 +000038#include <memory>
Feng Xiao6ef984a2014-11-10 17:34:54 -080039#ifndef _SHARED_PTR_H
40#include <google/protobuf/stubs/shared_ptr.h>
41#endif
temporal40ee5512008-07-10 02:12:20 +000042#include <string>
43#include <vector>
44#include <google/protobuf/stubs/common.h>
45
46namespace google {
47namespace protobuf {
jieluo@google.com4de8f552014-07-18 00:47:59 +000048 class FileDescriptor; // descriptor.h
temporal40ee5512008-07-10 02:12:20 +000049 namespace io {
jieluo@google.com4de8f552014-07-18 00:47:59 +000050 class Printer; // printer.h
temporal40ee5512008-07-10 02:12:20 +000051 }
52 namespace compiler {
jieluo@google.com4de8f552014-07-18 00:47:59 +000053 class GeneratorContext; // code_generator.h
54 namespace java {
55 class Context; // context.h
56 class MessageGenerator; // message.h
57 class GeneratorFactory; // generator_factory.h
58 class ExtensionGenerator; // extension.h
59 class ClassNameResolver; // name_resolver.h
60 }
temporal40ee5512008-07-10 02:12:20 +000061 }
62}
63
64namespace protobuf {
65namespace compiler {
66namespace java {
67
68class FileGenerator {
69 public:
jieluo@google.com4de8f552014-07-18 00:47:59 +000070 FileGenerator(const FileDescriptor* file, bool immutable_api = true);
temporal40ee5512008-07-10 02:12:20 +000071 ~FileGenerator();
72
73 // Checks for problems that would otherwise lead to cryptic compile errors.
74 // Returns true if there are no problems, or writes an error description to
75 // the given string and returns false otherwise.
76 bool Validate(string* error);
77
78 void Generate(io::Printer* printer);
79
80 // If we aren't putting everything into one file, this will write all the
81 // files other than the outer file (i.e. one for each message, enum, and
82 // service type).
83 void GenerateSiblings(const string& package_dir,
liujisi@google.com33165fe2010-11-02 13:14:58 +000084 GeneratorContext* generator_context,
temporal40ee5512008-07-10 02:12:20 +000085 vector<string>* file_list);
86
87 const string& java_package() { return java_package_; }
88 const string& classname() { return classname_; }
89
liujisi@google.com33165fe2010-11-02 13:14:58 +000090
temporal40ee5512008-07-10 02:12:20 +000091 private:
jieluo@google.com4de8f552014-07-18 00:47:59 +000092 void GenerateDescriptorInitializationCodeForImmutable(io::Printer* printer);
93 void GenerateDescriptorInitializationCodeForMutable(io::Printer* printer);
94
95 bool ShouldIncludeDependency(const FileDescriptor* descriptor,
96 bool immutable_api_);
kenton@google.comfccb1462009-12-18 02:11:36 +000097
temporal40ee5512008-07-10 02:12:20 +000098 const FileDescriptor* file_;
99 string java_package_;
100 string classname_;
101
Feng Xiaof157a562014-11-14 11:50:31 -0800102 google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > message_generators_;
103 google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_;
104 google::protobuf::scoped_ptr<GeneratorFactory> generator_factory_;
105 google::protobuf::scoped_ptr<Context> context_;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000106 ClassNameResolver* name_resolver_;
107 bool immutable_api_;
liujisi@google.com33165fe2010-11-02 13:14:58 +0000108
kenton@google.com80b1d622009-07-29 01:13:20 +0000109
temporal40ee5512008-07-10 02:12:20 +0000110 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator);
111};
112
113} // namespace java
114} // namespace compiler
115} // namespace protobuf
116
117} // namespace google
118#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FILE_H__