temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 35 | #include <google/protobuf/compiler/java/java_file.h> |
| 36 | #include <google/protobuf/compiler/java/java_enum.h> |
| 37 | #include <google/protobuf/compiler/java/java_service.h> |
| 38 | #include <google/protobuf/compiler/java/java_extension.h> |
| 39 | #include <google/protobuf/compiler/java/java_helpers.h> |
| 40 | #include <google/protobuf/compiler/java/java_message.h> |
| 41 | #include <google/protobuf/compiler/code_generator.h> |
| 42 | #include <google/protobuf/io/printer.h> |
| 43 | #include <google/protobuf/io/zero_copy_stream.h> |
| 44 | #include <google/protobuf/descriptor.pb.h> |
| 45 | #include <google/protobuf/stubs/strutil.h> |
| 46 | |
| 47 | namespace google { |
| 48 | namespace protobuf { |
| 49 | namespace compiler { |
| 50 | namespace java { |
| 51 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | |
| 54 | // Recursively searches the given message to see if it contains any extensions. |
| 55 | bool UsesExtensions(const Message& message) { |
| 56 | const Reflection* reflection = message.GetReflection(); |
| 57 | |
| 58 | // We conservatively assume that unknown fields are extensions. |
| 59 | if (reflection->GetUnknownFields(message).field_count() > 0) return true; |
| 60 | |
| 61 | vector<const FieldDescriptor*> fields; |
| 62 | reflection->ListFields(message, &fields); |
| 63 | |
| 64 | for (int i = 0; i < fields.size(); i++) { |
| 65 | if (fields[i]->is_extension()) return true; |
| 66 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 67 | if (GetJavaType(fields[i]) == JAVATYPE_MESSAGE) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 68 | if (fields[i]->is_repeated()) { |
| 69 | int size = reflection->FieldSize(message, fields[i]); |
| 70 | for (int j = 0; j < size; j++) { |
| 71 | const Message& sub_message = |
| 72 | reflection->GetRepeatedMessage(message, fields[i], j); |
| 73 | if (UsesExtensions(sub_message)) return true; |
| 74 | } |
| 75 | } else { |
| 76 | const Message& sub_message = reflection->GetMessage(message, fields[i]); |
| 77 | if (UsesExtensions(sub_message)) return true; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 85 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 86 | } // namespace |
| 87 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 88 | FileGenerator::FileGenerator(const FileDescriptor* file) |
| 89 | : file_(file), |
| 90 | java_package_(FileJavaPackage(file)), |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 91 | classname_(FileClassName(file)) { |
| 92 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 93 | |
| 94 | FileGenerator::~FileGenerator() {} |
| 95 | |
| 96 | bool FileGenerator::Validate(string* error) { |
| 97 | // Check that no class name matches the file's class name. This is a common |
| 98 | // problem that leads to Java compile errors that can be hard to understand. |
| 99 | // It's especially bad when using the java_multiple_files, since we would |
| 100 | // end up overwriting the outer class with one of the inner ones. |
| 101 | |
| 102 | bool found_conflict = false; |
| 103 | for (int i = 0; i < file_->enum_type_count() && !found_conflict; i++) { |
| 104 | if (file_->enum_type(i)->name() == classname_) { |
| 105 | found_conflict = true; |
| 106 | } |
| 107 | } |
| 108 | for (int i = 0; i < file_->message_type_count() && !found_conflict; i++) { |
| 109 | if (file_->message_type(i)->name() == classname_) { |
| 110 | found_conflict = true; |
| 111 | } |
| 112 | } |
| 113 | for (int i = 0; i < file_->service_count() && !found_conflict; i++) { |
| 114 | if (file_->service(i)->name() == classname_) { |
| 115 | found_conflict = true; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (found_conflict) { |
| 120 | error->assign(file_->name()); |
| 121 | error->append( |
| 122 | ": Cannot generate Java output because the file's outer class name, \""); |
| 123 | error->append(classname_); |
| 124 | error->append( |
| 125 | "\", matches the name of one of the types declared inside it. " |
| 126 | "Please either rename the type or use the java_outer_classname " |
| 127 | "option to specify a different outer class name for the .proto file."); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | void FileGenerator::Generate(io::Printer* printer) { |
| 135 | // We don't import anything because we refer to all classes by their |
| 136 | // fully-qualified names in the generated source. |
| 137 | printer->Print( |
| 138 | "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 139 | "// source: $filename$\n" |
| 140 | "\n", |
| 141 | "filename", file_->name()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 142 | if (!java_package_.empty()) { |
| 143 | printer->Print( |
| 144 | "package $package$;\n" |
| 145 | "\n", |
| 146 | "package", java_package_); |
| 147 | } |
| 148 | printer->Print( |
| 149 | "public final class $classname$ {\n" |
| 150 | " private $classname$() {}\n", |
| 151 | "classname", classname_); |
| 152 | printer->Indent(); |
| 153 | |
| 154 | // ----------------------------------------------------------------- |
| 155 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 156 | printer->Print( |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 157 | "public static void registerAllExtensions(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 158 | " com.google.protobuf.ExtensionRegistry$lite$ registry) {\n", |
| 159 | "lite", HasDescriptorMethods(file_) ? "" : "Lite"); |
| 160 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 161 | printer->Indent(); |
| 162 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 163 | for (int i = 0; i < file_->extension_count(); i++) { |
| 164 | ExtensionGenerator(file_->extension(i)).GenerateRegistrationCode(printer); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 165 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 166 | |
| 167 | for (int i = 0; i < file_->message_type_count(); i++) { |
| 168 | MessageGenerator(file_->message_type(i)) |
| 169 | .GenerateExtensionRegistrationCode(printer); |
| 170 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 171 | |
| 172 | printer->Outdent(); |
| 173 | printer->Print( |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 174 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 175 | |
| 176 | // ----------------------------------------------------------------- |
| 177 | |
| 178 | if (!file_->options().java_multiple_files()) { |
| 179 | for (int i = 0; i < file_->enum_type_count(); i++) { |
| 180 | EnumGenerator(file_->enum_type(i)).Generate(printer); |
| 181 | } |
| 182 | for (int i = 0; i < file_->message_type_count(); i++) { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 183 | MessageGenerator messageGenerator(file_->message_type(i)); |
| 184 | messageGenerator.GenerateInterface(printer); |
| 185 | messageGenerator.Generate(printer); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 186 | } |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 187 | if (HasGenericServices(file_)) { |
| 188 | for (int i = 0; i < file_->service_count(); i++) { |
| 189 | ServiceGenerator(file_->service(i)).Generate(printer); |
| 190 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | // Extensions must be generated in the outer class since they are values, |
| 195 | // not classes. |
| 196 | for (int i = 0; i < file_->extension_count(); i++) { |
| 197 | ExtensionGenerator(file_->extension(i)).Generate(printer); |
| 198 | } |
| 199 | |
| 200 | // Static variables. |
| 201 | for (int i = 0; i < file_->message_type_count(); i++) { |
| 202 | // TODO(kenton): Reuse MessageGenerator objects? |
| 203 | MessageGenerator(file_->message_type(i)).GenerateStaticVariables(printer); |
| 204 | } |
| 205 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 206 | printer->Print("\n"); |
| 207 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 208 | if (HasDescriptorMethods(file_)) { |
| 209 | GenerateEmbeddedDescriptor(printer); |
| 210 | } else { |
| 211 | printer->Print( |
| 212 | "static {\n"); |
| 213 | printer->Indent(); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 214 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 215 | for (int i = 0; i < file_->message_type_count(); i++) { |
| 216 | // TODO(kenton): Reuse MessageGenerator objects? |
| 217 | MessageGenerator(file_->message_type(i)) |
| 218 | .GenerateStaticVariableInitializers(printer); |
| 219 | } |
| 220 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 221 | printer->Outdent(); |
| 222 | printer->Print( |
| 223 | "}\n"); |
| 224 | } |
| 225 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 226 | printer->Print( |
| 227 | "\n" |
| 228 | "// @@protoc_insertion_point(outer_class_scope)\n"); |
| 229 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 230 | printer->Outdent(); |
| 231 | printer->Print("}\n"); |
| 232 | } |
| 233 | |
| 234 | void FileGenerator::GenerateEmbeddedDescriptor(io::Printer* printer) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 235 | // Embed the descriptor. We simply serialize the entire FileDescriptorProto |
| 236 | // and embed it as a string literal, which is parsed and built into real |
| 237 | // descriptors at initialization time. We unfortunately have to put it in |
| 238 | // a string literal, not a byte array, because apparently using a literal |
| 239 | // byte array causes the Java compiler to generate *instructions* to |
| 240 | // initialize each and every byte of the array, e.g. as if you typed: |
| 241 | // b[0] = 123; b[1] = 456; b[2] = 789; |
| 242 | // This makes huge bytecode files and can easily hit the compiler's internal |
| 243 | // code size limits (error "code to large"). String literals are apparently |
| 244 | // embedded raw, which is what we want. |
| 245 | FileDescriptorProto file_proto; |
| 246 | file_->CopyTo(&file_proto); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 247 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 248 | string file_data; |
| 249 | file_proto.SerializeToString(&file_data); |
| 250 | |
| 251 | printer->Print( |
| 252 | "public static com.google.protobuf.Descriptors.FileDescriptor\n" |
| 253 | " getDescriptor() {\n" |
| 254 | " return descriptor;\n" |
| 255 | "}\n" |
| 256 | "private static com.google.protobuf.Descriptors.FileDescriptor\n" |
| 257 | " descriptor;\n" |
| 258 | "static {\n" |
kenton@google.com | 68996fc | 2009-08-07 21:02:02 +0000 | [diff] [blame] | 259 | " java.lang.String[] descriptorData = {\n"); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 260 | printer->Indent(); |
| 261 | printer->Indent(); |
| 262 | |
| 263 | // Only write 40 bytes per line. |
| 264 | static const int kBytesPerLine = 40; |
| 265 | for (int i = 0; i < file_data.size(); i += kBytesPerLine) { |
kenton@google.com | 68996fc | 2009-08-07 21:02:02 +0000 | [diff] [blame] | 266 | if (i > 0) { |
| 267 | // Every 400 lines, start a new string literal, in order to avoid the |
| 268 | // 64k length limit. |
| 269 | if (i % 400 == 0) { |
| 270 | printer->Print(",\n"); |
| 271 | } else { |
| 272 | printer->Print(" +\n"); |
| 273 | } |
| 274 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 275 | printer->Print("\"$data$\"", |
| 276 | "data", CEscape(file_data.substr(i, kBytesPerLine))); |
| 277 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 278 | |
| 279 | printer->Outdent(); |
kenton@google.com | 68996fc | 2009-08-07 21:02:02 +0000 | [diff] [blame] | 280 | printer->Print("\n};\n"); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 281 | |
| 282 | // ----------------------------------------------------------------- |
| 283 | // Create the InternalDescriptorAssigner. |
| 284 | |
| 285 | printer->Print( |
| 286 | "com.google.protobuf.Descriptors.FileDescriptor." |
| 287 | "InternalDescriptorAssigner assigner =\n" |
| 288 | " new com.google.protobuf.Descriptors.FileDescriptor." |
| 289 | "InternalDescriptorAssigner() {\n" |
| 290 | " public com.google.protobuf.ExtensionRegistry assignDescriptors(\n" |
| 291 | " com.google.protobuf.Descriptors.FileDescriptor root) {\n" |
| 292 | " descriptor = root;\n"); |
| 293 | |
| 294 | printer->Indent(); |
| 295 | printer->Indent(); |
| 296 | printer->Indent(); |
| 297 | |
| 298 | for (int i = 0; i < file_->message_type_count(); i++) { |
| 299 | // TODO(kenton): Reuse MessageGenerator objects? |
| 300 | MessageGenerator(file_->message_type(i)) |
| 301 | .GenerateStaticVariableInitializers(printer); |
| 302 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 303 | for (int i = 0; i < file_->extension_count(); i++) { |
| 304 | // TODO(kenton): Reuse ExtensionGenerator objects? |
| 305 | ExtensionGenerator(file_->extension(i)) |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 306 | .GenerateNonNestedInitializationCode(printer); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (UsesExtensions(file_proto)) { |
| 310 | // Must construct an ExtensionRegistry containing all possible extensions |
| 311 | // and return it. |
| 312 | printer->Print( |
| 313 | "com.google.protobuf.ExtensionRegistry registry =\n" |
| 314 | " com.google.protobuf.ExtensionRegistry.newInstance();\n" |
| 315 | "registerAllExtensions(registry);\n"); |
| 316 | for (int i = 0; i < file_->dependency_count(); i++) { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 317 | if (ShouldIncludeDependency(file_->dependency(i))) { |
| 318 | printer->Print( |
| 319 | "$dependency$.registerAllExtensions(registry);\n", |
| 320 | "dependency", ClassName(file_->dependency(i))); |
| 321 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 322 | } |
| 323 | printer->Print( |
| 324 | "return registry;\n"); |
| 325 | } else { |
| 326 | printer->Print( |
| 327 | "return null;\n"); |
| 328 | } |
| 329 | |
| 330 | printer->Outdent(); |
| 331 | printer->Outdent(); |
| 332 | printer->Outdent(); |
| 333 | |
| 334 | printer->Print( |
| 335 | " }\n" |
| 336 | " };\n"); |
| 337 | |
| 338 | // ----------------------------------------------------------------- |
| 339 | // Invoke internalBuildGeneratedFileFrom() to build the file. |
| 340 | |
| 341 | printer->Print( |
| 342 | "com.google.protobuf.Descriptors.FileDescriptor\n" |
| 343 | " .internalBuildGeneratedFileFrom(descriptorData,\n" |
| 344 | " new com.google.protobuf.Descriptors.FileDescriptor[] {\n"); |
| 345 | |
| 346 | for (int i = 0; i < file_->dependency_count(); i++) { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 347 | if (ShouldIncludeDependency(file_->dependency(i))) { |
| 348 | printer->Print( |
| 349 | " $dependency$.getDescriptor(),\n", |
| 350 | "dependency", ClassName(file_->dependency(i))); |
| 351 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | printer->Print( |
| 355 | " }, assigner);\n"); |
| 356 | |
| 357 | printer->Outdent(); |
| 358 | printer->Print( |
| 359 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | template<typename GeneratorClass, typename DescriptorClass> |
| 363 | static void GenerateSibling(const string& package_dir, |
| 364 | const string& java_package, |
| 365 | const DescriptorClass* descriptor, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 366 | GeneratorContext* context, |
| 367 | vector<string>* file_list, |
| 368 | const string& name_suffix, |
| 369 | void (GeneratorClass::*pfn)(io::Printer* printer)) { |
| 370 | string filename = package_dir + descriptor->name() + name_suffix + ".java"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 371 | file_list->push_back(filename); |
| 372 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 373 | scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 374 | io::Printer printer(output.get(), '$'); |
| 375 | |
| 376 | printer.Print( |
| 377 | "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" |
| 378 | "\n"); |
| 379 | if (!java_package.empty()) { |
| 380 | printer.Print( |
| 381 | "package $package$;\n" |
| 382 | "\n", |
| 383 | "package", java_package); |
| 384 | } |
| 385 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 386 | GeneratorClass generator(descriptor); |
| 387 | (generator.*pfn)(&printer); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void FileGenerator::GenerateSiblings(const string& package_dir, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 391 | GeneratorContext* context, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 392 | vector<string>* file_list) { |
| 393 | if (file_->options().java_multiple_files()) { |
| 394 | for (int i = 0; i < file_->enum_type_count(); i++) { |
| 395 | GenerateSibling<EnumGenerator>(package_dir, java_package_, |
| 396 | file_->enum_type(i), |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 397 | context, file_list, "", |
| 398 | &EnumGenerator::Generate); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 399 | } |
| 400 | for (int i = 0; i < file_->message_type_count(); i++) { |
| 401 | GenerateSibling<MessageGenerator>(package_dir, java_package_, |
| 402 | file_->message_type(i), |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 403 | context, file_list, "OrBuilder", |
| 404 | &MessageGenerator::GenerateInterface); |
| 405 | GenerateSibling<MessageGenerator>(package_dir, java_package_, |
| 406 | file_->message_type(i), |
| 407 | context, file_list, "", |
| 408 | &MessageGenerator::Generate); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 409 | } |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 410 | if (HasGenericServices(file_)) { |
| 411 | for (int i = 0; i < file_->service_count(); i++) { |
| 412 | GenerateSibling<ServiceGenerator>(package_dir, java_package_, |
| 413 | file_->service(i), |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 414 | context, file_list, "", |
| 415 | &ServiceGenerator::Generate); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 416 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 421 | bool FileGenerator::ShouldIncludeDependency(const FileDescriptor* descriptor) { |
| 422 | return true; |
| 423 | } |
| 424 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 425 | } // namespace java |
| 426 | } // namespace compiler |
| 427 | } // namespace protobuf |
| 428 | } // namespace google |