| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include <google/protobuf/compiler/java/java_generator.h> | 35 #include <google/protobuf/compiler/java/java_generator.h> |
| 36 | 36 |
| 37 #include <memory> | 37 #include <memory> |
| 38 #ifndef _SHARED_PTR_H | 38 #ifndef _SHARED_PTR_H |
| 39 #include <google/protobuf/stubs/shared_ptr.h> | 39 #include <google/protobuf/stubs/shared_ptr.h> |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 #include <google/protobuf/compiler/java/java_file.h> | 42 #include <google/protobuf/compiler/java/java_file.h> |
| 43 #include <google/protobuf/compiler/java/java_generator_factory.h> | 43 #include <google/protobuf/compiler/java/java_generator_factory.h> |
| 44 #include <google/protobuf/compiler/java/java_helpers.h> | 44 #include <google/protobuf/compiler/java/java_helpers.h> |
| 45 #include <google/protobuf/compiler/java/java_options.h> |
| 45 #include <google/protobuf/compiler/java/java_shared_code_generator.h> | 46 #include <google/protobuf/compiler/java/java_shared_code_generator.h> |
| 46 #include <google/protobuf/io/printer.h> | 47 #include <google/protobuf/io/printer.h> |
| 47 #include <google/protobuf/io/zero_copy_stream.h> | 48 #include <google/protobuf/io/zero_copy_stream.h> |
| 48 #include <google/protobuf/descriptor.pb.h> | 49 #include <google/protobuf/descriptor.pb.h> |
| 49 #include <google/protobuf/stubs/strutil.h> | 50 #include <google/protobuf/stubs/strutil.h> |
| 50 | 51 |
| 51 namespace google { | 52 namespace google { |
| 52 namespace protobuf { | 53 namespace protobuf { |
| 53 namespace compiler { | 54 namespace compiler { |
| 54 namespace java { | 55 namespace java { |
| 55 | 56 |
| 56 | 57 |
| 57 JavaGenerator::JavaGenerator() {} | 58 JavaGenerator::JavaGenerator() {} |
| 58 JavaGenerator::~JavaGenerator() {} | 59 JavaGenerator::~JavaGenerator() {} |
| 59 | 60 |
| 60 bool JavaGenerator::Generate(const FileDescriptor* file, | 61 bool JavaGenerator::Generate(const FileDescriptor* file, |
| 61 const string& parameter, | 62 const string& parameter, |
| 62 GeneratorContext* context, | 63 GeneratorContext* context, |
| 63 string* error) const { | 64 string* error) const { |
| 64 // ----------------------------------------------------------------- | 65 // ----------------------------------------------------------------- |
| 65 // parse generator options | 66 // parse generator options |
| 66 | 67 |
| 67 // Name a file where we will write a list of generated file names, one | |
| 68 // per line. | |
| 69 string output_list_file; | |
| 70 | 68 |
| 69 std::vector<std::pair<string, string> > options; |
| 70 ParseGeneratorParameter(parameter, &options); |
| 71 Options file_options; |
| 71 | 72 |
| 72 vector<pair<string, string> > options; | |
| 73 ParseGeneratorParameter(parameter, &options); | |
| 74 | |
| 75 bool generate_immutable_code = false; | |
| 76 bool generate_mutable_code = false; | |
| 77 bool generate_shared_code = false; | |
| 78 bool enforce_lite = false; | |
| 79 for (int i = 0; i < options.size(); i++) { | 73 for (int i = 0; i < options.size(); i++) { |
| 80 if (options[i].first == "output_list_file") { | 74 if (options[i].first == "output_list_file") { |
| 81 output_list_file = options[i].second; | 75 file_options.output_list_file = options[i].second; |
| 82 } else if (options[i].first == "immutable") { | 76 } else if (options[i].first == "immutable") { |
| 83 generate_immutable_code = true; | 77 file_options.generate_immutable_code = true; |
| 84 } else if (options[i].first == "mutable") { | 78 } else if (options[i].first == "mutable") { |
| 85 generate_mutable_code = true; | 79 file_options.generate_mutable_code = true; |
| 86 } else if (options[i].first == "shared") { | 80 } else if (options[i].first == "shared") { |
| 87 generate_shared_code = true; | 81 file_options.generate_shared_code = true; |
| 88 } else if (options[i].first == "lite") { | 82 } else if (options[i].first == "annotate_code") { |
| 89 // When set, the protoc will generate the current files and all the | 83 file_options.annotate_code = true; |
| 90 // transitive dependencies as lite runtime. | 84 } else if (options[i].first == "annotation_list_file") { |
| 91 enforce_lite = true; | 85 file_options.annotation_list_file = options[i].second; |
| 92 } else { | 86 } else { |
| 93 *error = "Unknown generator option: " + options[i].first; | 87 *error = "Unknown generator option: " + options[i].first; |
| 94 return false; | 88 return false; |
| 95 } | 89 } |
| 96 } | 90 } |
| 97 | 91 |
| 98 if (enforce_lite && generate_mutable_code) { | 92 if (file_options.enforce_lite && file_options.generate_mutable_code) { |
| 99 *error = "lite runtime generator option cannot be used with mutable API."; | 93 *error = "lite runtime generator option cannot be used with mutable API."; |
| 100 return false; | 94 return false; |
| 101 } | 95 } |
| 102 | 96 |
| 103 // By default we generate immutable code and shared code for immutable API. | 97 // By default we generate immutable code and shared code for immutable API. |
| 104 if (!generate_immutable_code && !generate_mutable_code && | 98 if (!file_options.generate_immutable_code && |
| 105 !generate_shared_code) { | 99 !file_options.generate_mutable_code && |
| 106 generate_immutable_code = true; | 100 !file_options.generate_shared_code) { |
| 107 generate_shared_code = true; | 101 file_options.generate_immutable_code = true; |
| 102 file_options.generate_shared_code = true; |
| 108 } | 103 } |
| 109 | 104 |
| 110 // ----------------------------------------------------------------- | 105 // ----------------------------------------------------------------- |
| 111 | 106 |
| 112 | 107 |
| 113 vector<string> all_files; | 108 std::vector<string> all_files; |
| 109 std::vector<string> all_annotations; |
| 114 | 110 |
| 115 | 111 |
| 116 vector<FileGenerator*> file_generators; | 112 std::vector<FileGenerator*> file_generators; |
| 117 if (generate_immutable_code) { | 113 if (file_options.generate_immutable_code) { |
| 118 file_generators.push_back( | 114 file_generators.push_back(new FileGenerator(file, file_options, |
| 119 new FileGenerator(file, /* immutable = */ true, enforce_lite)); | 115 /* immutable = */ true)); |
| 120 } | 116 } |
| 121 if (generate_mutable_code) { | 117 if (file_options.generate_mutable_code) { |
| 122 file_generators.push_back( | 118 file_generators.push_back(new FileGenerator(file, file_options, |
| 123 new FileGenerator(file, /* mutable = */ false, enforce_lite)); | 119 /* mutable = */ false)); |
| 124 } | 120 } |
| 125 for (int i = 0; i < file_generators.size(); ++i) { | 121 for (int i = 0; i < file_generators.size(); ++i) { |
| 126 if (!file_generators[i]->Validate(error)) { | 122 if (!file_generators[i]->Validate(error)) { |
| 127 for (int j = 0; j < file_generators.size(); ++j) { | 123 for (int j = 0; j < file_generators.size(); ++j) { |
| 128 delete file_generators[j]; | 124 delete file_generators[j]; |
| 129 } | 125 } |
| 130 return false; | 126 return false; |
| 131 } | 127 } |
| 132 } | 128 } |
| 133 | 129 |
| 134 for (int i = 0; i < file_generators.size(); ++i) { | 130 for (int i = 0; i < file_generators.size(); ++i) { |
| 135 FileGenerator* file_generator = file_generators[i]; | 131 FileGenerator* file_generator = file_generators[i]; |
| 136 | 132 |
| 137 string package_dir = JavaPackageToDir(file_generator->java_package()); | 133 string package_dir = JavaPackageToDir(file_generator->java_package()); |
| 138 | 134 |
| 139 string java_filename = package_dir; | 135 string java_filename = package_dir; |
| 140 java_filename += file_generator->classname(); | 136 java_filename += file_generator->classname(); |
| 141 java_filename += ".java"; | 137 java_filename += ".java"; |
| 142 all_files.push_back(java_filename); | 138 all_files.push_back(java_filename); |
| 139 string info_full_path = java_filename + ".pb.meta"; |
| 140 if (file_options.annotate_code) { |
| 141 all_annotations.push_back(info_full_path); |
| 142 } |
| 143 | 143 |
| 144 // Generate main java file. | 144 // Generate main java file. |
| 145 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( | 145 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( |
| 146 context->Open(java_filename)); | 146 context->Open(java_filename)); |
| 147 io::Printer printer(output.get(), '$'); | 147 GeneratedCodeInfo annotations; |
| 148 io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( |
| 149 &annotations); |
| 150 io::Printer printer(output.get(), '$', file_options.annotate_code |
| 151 ? &annotation_collector |
| 152 : NULL); |
| 153 |
| 148 file_generator->Generate(&printer); | 154 file_generator->Generate(&printer); |
| 149 | 155 |
| 150 // Generate sibling files. | 156 // Generate sibling files. |
| 151 file_generator->GenerateSiblings(package_dir, context, &all_files); | 157 file_generator->GenerateSiblings(package_dir, context, &all_files, |
| 158 &all_annotations); |
| 159 |
| 160 if (file_options.annotate_code) { |
| 161 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> info_output( |
| 162 context->Open(info_full_path)); |
| 163 annotations.SerializeToZeroCopyStream(info_output.get()); |
| 164 } |
| 152 } | 165 } |
| 153 | 166 |
| 154 for (int i = 0; i < file_generators.size(); ++i) { | 167 for (int i = 0; i < file_generators.size(); ++i) { |
| 155 delete file_generators[i]; | 168 delete file_generators[i]; |
| 156 } | 169 } |
| 157 file_generators.clear(); | 170 file_generators.clear(); |
| 158 | 171 |
| 159 // Generate output list if requested. | 172 // Generate output list if requested. |
| 160 if (!output_list_file.empty()) { | 173 if (!file_options.output_list_file.empty()) { |
| 161 // Generate output list. This is just a simple text file placed in a | 174 // Generate output list. This is just a simple text file placed in a |
| 162 // deterministic location which lists the .java files being generated. | 175 // deterministic location which lists the .java files being generated. |
| 163 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( | 176 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( |
| 164 context->Open(output_list_file)); | 177 context->Open(file_options.output_list_file)); |
| 165 io::Printer srclist_printer(srclist_raw_output.get(), '$'); | 178 io::Printer srclist_printer(srclist_raw_output.get(), '$'); |
| 166 for (int i = 0; i < all_files.size(); i++) { | 179 for (int i = 0; i < all_files.size(); i++) { |
| 167 srclist_printer.Print("$filename$\n", "filename", all_files[i]); | 180 srclist_printer.Print("$filename$\n", "filename", all_files[i]); |
| 168 } | 181 } |
| 169 } | 182 } |
| 170 | 183 |
| 184 if (!file_options.annotation_list_file.empty()) { |
| 185 // Generate output list. This is just a simple text file placed in a |
| 186 // deterministic location which lists the .java files being generated. |
| 187 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> annotation_list_raw_o
utput( |
| 188 context->Open(file_options.annotation_list_file)); |
| 189 io::Printer annotation_list_printer(annotation_list_raw_output.get(), '$'); |
| 190 for (int i = 0; i < all_annotations.size(); i++) { |
| 191 annotation_list_printer.Print("$filename$\n", "filename", |
| 192 all_annotations[i]); |
| 193 } |
| 194 } |
| 195 |
| 171 return true; | 196 return true; |
| 172 } | 197 } |
| 173 | 198 |
| 174 } // namespace java | 199 } // namespace java |
| 175 } // namespace compiler | 200 } // namespace compiler |
| 176 } // namespace protobuf | 201 } // namespace protobuf |
| 177 } // namespace google | 202 } // namespace google |
| OLD | NEW |