blob: 70177367a250761dd074ff6ede9b590e9e0ead2a [file] [log] [blame]
jieluo@google.com4de8f552014-07-18 00:47:59 +00001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
jieluo@google.com4de8f552014-07-18 00:47:59 +00004//
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>
Feng Xiao6ef984a2014-11-10 17:34:54 -080036#ifndef _SHARED_PTR_H
37#include <google/protobuf/stubs/shared_ptr.h>
38#endif
jieluo@google.com4de8f552014-07-18 00:47:59 +000039
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
49namespace google {
50namespace protobuf {
51namespace compiler {
52namespace java {
53
54SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file)
55 : name_resolver_(new ClassNameResolver), file_(file) {
56}
57
58SharedCodeGenerator::~SharedCodeGenerator() {
59}
60
61void SharedCodeGenerator::Generate(GeneratorContext* context,
62 vector<string>* file_list) {
63 string java_package = FileJavaPackage(file_);
64 string package_dir = JavaPackageToDir(java_package);
65
66 if (HasDescriptorMethods(file_)) {
67 // Generate descriptors.
68 string classname = name_resolver_->GetDescriptorClassName(file_);
69 string filename = package_dir + classname + ".java";
70 file_list->push_back(filename);
Feng Xiaof157a562014-11-14 11:50:31 -080071 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
72 google::protobuf::scoped_ptr<io::Printer> printer(new io::Printer(output.get(), '$'));
jieluo@google.com4de8f552014-07-18 00:47:59 +000073
74 printer->Print(
75 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
76 "// source: $filename$\n"
77 "\n",
78 "filename", file_->name());
79 if (!java_package.empty()) {
80 printer->Print(
81 "package $package$;\n"
82 "\n",
83 "package", java_package);
84 }
85 printer->Print(
jieluo@google.com7db9c092014-08-07 19:03:12 +000086 "public final class $classname$ {\n"
87 " public static com.google.protobuf.Descriptors.FileDescriptor\n"
88 " descriptor;\n"
89 " static {\n",
jieluo@google.com4de8f552014-07-18 00:47:59 +000090 "classname", classname);
91 printer->Indent();
jieluo@google.com7db9c092014-08-07 19:03:12 +000092 printer->Indent();
jieluo@google.com4de8f552014-07-18 00:47:59 +000093 GenerateDescriptors(printer.get());
94 printer->Outdent();
jieluo@google.com7db9c092014-08-07 19:03:12 +000095 printer->Outdent();
jieluo@google.com4de8f552014-07-18 00:47:59 +000096 printer->Print(
jieluo@google.com7db9c092014-08-07 19:03:12 +000097 " }\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +000098 "}\n");
99
100 printer.reset();
101 output.reset();
102 }
103}
104
105
106void 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 FileDescriptorProto file_proto;
118 file_->CopyTo(&file_proto);
119
120
121 string file_data;
122 file_proto.SerializeToString(&file_data);
123
124 printer->Print(
jieluo@google.com7db9c092014-08-07 19:03:12 +0000125 "java.lang.String[] descriptorData = {\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000126 printer->Indent();
127
128 // Only write 40 bytes per line.
129 static const int kBytesPerLine = 40;
130 for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
131 if (i > 0) {
132 // Every 400 lines, start a new string literal, in order to avoid the
133 // 64k length limit.
134 if (i % 400 == 0) {
135 printer->Print(",\n");
136 } else {
137 printer->Print(" +\n");
138 }
139 }
140 printer->Print("\"$data$\"",
141 "data", CEscape(file_data.substr(i, kBytesPerLine)));
142 }
143
144 printer->Outdent();
145 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 " };\n");
164
165 // -----------------------------------------------------------------
166 // Find out all dependencies.
167 vector<pair<string, string> > dependencies;
168 for (int i = 0; i < file_->dependency_count(); i++) {
169 if (ShouldIncludeDependency(file_->dependency(i))) {
170 string filename = file_->dependency(i)->name();
171 string classname = FileJavaPackage(file_->dependency(i)) + "." +
172 name_resolver_->GetDescriptorClassName(
173 file_->dependency(i));
Jisi Liu885b6122015-02-28 14:51:22 -0800174 dependencies.push_back(std::make_pair(filename, classname));
jieluo@google.com4de8f552014-07-18 00:47:59 +0000175 }
176 }
177
178 // -----------------------------------------------------------------
179 // Invoke internalBuildGeneratedFileFrom() to build the file.
180 printer->Print(
181 "com.google.protobuf.Descriptors.FileDescriptor\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800182 " .internalBuildGeneratedFileFrom(descriptorData,\n");
183 printer->Print(
jieluo@google.comd7339312014-08-07 22:22:00 +0000184 " new com.google.protobuf.Descriptors.FileDescriptor[] {\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000185
jieluo@google.com4de8f552014-07-18 00:47:59 +0000186 for (int i = 0; i < dependencies.size(); i++) {
187 const string& dependency = dependencies[i].second;
188 printer->Print(
jieluo@google.comd7339312014-08-07 22:22:00 +0000189 " $dependency$.getDescriptor(),\n",
jieluo@google.com4de8f552014-07-18 00:47:59 +0000190 "dependency", dependency);
191 }
192
193 printer->Print(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000194 " }, assigner);\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000195}
196
197bool SharedCodeGenerator::ShouldIncludeDependency(
198 const FileDescriptor* descriptor) {
199 return true;
200}
201
202} // namespace java
203} // namespace compiler
204} // namespace protobuf
205} // namespace google