Line data Source code
1 : // Protocol Buffers - Google's data interchange format
2 : // Copyright 2008 Google Inc. All rights reserved.
3 : // https://developers.google.com/protocol-buffers/
4 : //
5 : // Redistribution and use in source and binary forms, with or without
6 : // modification, are permitted provided that the following conditions are
7 : // met:
8 : //
9 : // * 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.
18 : //
19 : // 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.
30 :
31 : // Author: xiaofeng@google.com (Feng Xiao)
32 :
33 : #include <google/protobuf/compiler/java/java_shared_code_generator.h>
34 :
35 : #include <memory>
36 : #ifndef _SHARED_PTR_H
37 : #include <google/protobuf/stubs/shared_ptr.h>
38 : #endif
39 :
40 : #include <google/protobuf/compiler/java/java_helpers.h>
41 : #include <google/protobuf/compiler/java/java_name_resolver.h>
42 : #include <google/protobuf/compiler/code_generator.h>
43 : #include <google/protobuf/io/printer.h>
44 : #include <google/protobuf/io/zero_copy_stream.h>
45 : #include <google/protobuf/descriptor.pb.h>
46 : #include <google/protobuf/descriptor.h>
47 : #include <google/protobuf/stubs/strutil.h>
48 :
49 : namespace google {
50 : namespace protobuf {
51 : namespace compiler {
52 : namespace java {
53 :
54 0 : SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file)
55 0 : : name_resolver_(new ClassNameResolver), file_(file) {
56 0 : }
57 :
58 0 : SharedCodeGenerator::~SharedCodeGenerator() {
59 0 : }
60 :
61 0 : void SharedCodeGenerator::Generate(GeneratorContext* context,
62 : vector<string>* file_list) {
63 0 : string java_package = FileJavaPackage(file_);
64 0 : string package_dir = JavaPackageToDir(java_package);
65 :
66 0 : if (HasDescriptorMethods(file_)) {
67 : // Generate descriptors.
68 0 : string classname = name_resolver_->GetDescriptorClassName(file_);
69 0 : string filename = package_dir + classname + ".java";
70 0 : file_list->push_back(filename);
71 0 : google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
72 0 : google::protobuf::scoped_ptr<io::Printer> printer(new io::Printer(output.get(), '$'));
73 :
74 : printer->Print(
75 : "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
76 : "// source: $filename$\n"
77 : "\n",
78 0 : "filename", file_->name());
79 0 : if (!java_package.empty()) {
80 : printer->Print(
81 : "package $package$;\n"
82 : "\n",
83 0 : "package", java_package);
84 : }
85 : printer->Print(
86 : "public final class $classname$ {\n"
87 : " public static com.google.protobuf.Descriptors.FileDescriptor\n"
88 : " descriptor;\n"
89 : " static {\n",
90 0 : "classname", classname);
91 0 : printer->Indent();
92 0 : printer->Indent();
93 0 : GenerateDescriptors(printer.get());
94 0 : printer->Outdent();
95 0 : printer->Outdent();
96 : printer->Print(
97 : " }\n"
98 0 : "}\n");
99 :
100 0 : printer.reset();
101 : output.reset();
102 : }
103 0 : }
104 :
105 :
106 0 : void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) {
107 : // Embed the descriptor. We simply serialize the entire FileDescriptorProto
108 : // and embed it as a string literal, which is parsed and built into real
109 : // descriptors at initialization time. We unfortunately have to put it in
110 : // a string literal, not a byte array, because apparently using a literal
111 : // byte array causes the Java compiler to generate *instructions* to
112 : // initialize each and every byte of the array, e.g. as if you typed:
113 : // b[0] = 123; b[1] = 456; b[2] = 789;
114 : // This makes huge bytecode files and can easily hit the compiler's internal
115 : // code size limits (error "code to large"). String literals are apparently
116 : // embedded raw, which is what we want.
117 0 : FileDescriptorProto file_proto;
118 0 : file_->CopyTo(&file_proto);
119 :
120 :
121 : string file_data;
122 0 : file_proto.SerializeToString(&file_data);
123 :
124 : printer->Print(
125 0 : "java.lang.String[] descriptorData = {\n");
126 0 : printer->Indent();
127 :
128 : // Only write 40 bytes per line.
129 : static const int kBytesPerLine = 40;
130 0 : for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
131 0 : if (i > 0) {
132 : // Every 400 lines, start a new string literal, in order to avoid the
133 : // 64k length limit.
134 0 : if (i % 400 == 0) {
135 0 : printer->Print(",\n");
136 : } else {
137 0 : printer->Print(" +\n");
138 : }
139 : }
140 : printer->Print("\"$data$\"",
141 0 : "data", CEscape(file_data.substr(i, kBytesPerLine)));
142 : }
143 :
144 0 : printer->Outdent();
145 0 : printer->Print("\n};\n");
146 :
147 : // -----------------------------------------------------------------
148 : // Create the InternalDescriptorAssigner.
149 :
150 : printer->Print(
151 : "com.google.protobuf.Descriptors.FileDescriptor."
152 : "InternalDescriptorAssigner assigner =\n"
153 : " new com.google.protobuf.Descriptors.FileDescriptor."
154 : " InternalDescriptorAssigner() {\n"
155 : " public com.google.protobuf.ExtensionRegistry assignDescriptors(\n"
156 : " com.google.protobuf.Descriptors.FileDescriptor root) {\n"
157 : " descriptor = root;\n"
158 : // Custom options will be handled when immutable messages' outer class is
159 : // loaded. Here we just return null and let custom options be unknown
160 : // fields.
161 : " return null;\n"
162 : " }\n"
163 0 : " };\n");
164 :
165 : // -----------------------------------------------------------------
166 : // Find out all dependencies.
167 0 : vector<pair<string, string> > dependencies;
168 0 : for (int i = 0; i < file_->dependency_count(); i++) {
169 0 : if (ShouldIncludeDependency(file_->dependency(i))) {
170 0 : string filename = file_->dependency(i)->name();
171 0 : string classname = FileJavaPackage(file_->dependency(i)) + "." +
172 : name_resolver_->GetDescriptorClassName(
173 0 : file_->dependency(i));
174 0 : dependencies.push_back(std::make_pair(filename, classname));
175 : }
176 : }
177 :
178 : // -----------------------------------------------------------------
179 : // Invoke internalBuildGeneratedFileFrom() to build the file.
180 : printer->Print(
181 : "com.google.protobuf.Descriptors.FileDescriptor\n"
182 0 : " .internalBuildGeneratedFileFrom(descriptorData,\n");
183 : printer->Print(
184 0 : " new com.google.protobuf.Descriptors.FileDescriptor[] {\n");
185 :
186 0 : for (int i = 0; i < dependencies.size(); i++) {
187 0 : const string& dependency = dependencies[i].second;
188 : printer->Print(
189 : " $dependency$.getDescriptor(),\n",
190 0 : "dependency", dependency);
191 : }
192 :
193 : printer->Print(
194 0 : " }, assigner);\n");
195 0 : }
196 :
197 0 : bool SharedCodeGenerator::ShouldIncludeDependency(
198 : const FileDescriptor* descriptor) {
199 0 : return true;
200 : }
201 :
202 : } // namespace java
203 : } // namespace compiler
204 : } // namespace protobuf
205 : } // namespace google
|