1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // http://code.google.com/p/protobuf/ 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: kenton (at) google.com (Kenton Varda) 32 // Based on original Protocol Buffers design by 33 // Sanjay Ghemawat, Jeff Dean, and others. 34 35 #include <iostream> 36 37 #include <google/protobuf/compiler/javanano/javanano_file.h> 38 #include <google/protobuf/compiler/javanano/javanano_enum.h> 39 #include <google/protobuf/compiler/javanano/javanano_extension.h> 40 #include <google/protobuf/compiler/javanano/javanano_helpers.h> 41 #include <google/protobuf/compiler/javanano/javanano_message.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/stubs/strutil.h> 47 48 namespace google { 49 namespace protobuf { 50 namespace compiler { 51 namespace javanano { 52 53 namespace { 54 55 // Recursively searches the given message to see if it contains any extensions. 56 bool UsesExtensions(const Message& message) { 57 const Reflection* reflection = message.GetReflection(); 58 59 // We conservatively assume that unknown fields are extensions. 60 if (reflection->GetUnknownFields(message).field_count() > 0) return true; 61 62 vector<const FieldDescriptor*> fields; 63 reflection->ListFields(message, &fields); 64 65 for (int i = 0; i < fields.size(); i++) { 66 if (fields[i]->is_extension()) return true; 67 68 if (fields[i]->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 69 if (fields[i]->is_repeated()) { 70 int size = reflection->FieldSize(message, fields[i]); 71 for (int j = 0; j < size; j++) { 72 const Message& sub_message = 73 reflection->GetRepeatedMessage(message, fields[i], j); 74 if (UsesExtensions(sub_message)) return true; 75 } 76 } else { 77 const Message& sub_message = reflection->GetMessage(message, fields[i]); 78 if (UsesExtensions(sub_message)) return true; 79 } 80 } 81 } 82 83 return false; 84 } 85 86 } // namespace 87 88 FileGenerator::FileGenerator(const FileDescriptor* file, const Params& params) 89 : file_(file), 90 params_(params), 91 java_package_(FileJavaPackage(params, file)), 92 classname_(FileClassName(params, file)) {} 93 94 FileGenerator::~FileGenerator() {} 95 96 bool FileGenerator::Validate(string* error) { 97 // Check for extensions 98 FileDescriptorProto file_proto; 99 file_->CopyTo(&file_proto); 100 if (UsesExtensions(file_proto) && !params_.store_unknown_fields()) { 101 error->assign(file_->name()); 102 error->append( 103 ": Java NANO_RUNTIME only supports extensions when the " 104 "'store_unknown_fields' generator option is 'true'."); 105 return false; 106 } 107 108 if (file_->service_count() != 0) { 109 error->assign(file_->name()); 110 error->append( 111 ": Java NANO_RUNTIME does not support services\""); 112 return false; 113 } 114 115 if (!IsOuterClassNeeded(params_, file_)) { 116 return true; 117 } 118 119 // Check whether legacy javanano generator would omit the outer class. 120 if (!params_.has_java_outer_classname(file_->name()) 121 && file_->message_type_count() == 1 122 && file_->enum_type_count() == 0 && file_->extension_count() == 0) { 123 cout << "INFO: " << file_->name() << ":" << endl; 124 cout << "Javanano generator has changed to align with java generator. " 125 "An outer class will be created for this file and the single message " 126 "in the file will become a nested class. Use java_multiple_files to " 127 "skip generating the outer class, or set an explicit " 128 "java_outer_classname to suppress this message." << endl; 129 } 130 131 // Check that no class name matches the file's class name. This is a common 132 // problem that leads to Java compile errors that can be hard to understand. 133 // It's especially bad when using the java_multiple_files, since we would 134 // end up overwriting the outer class with one of the inner ones. 135 bool found_conflict = false; 136 for (int i = 0; !found_conflict && i < file_->message_type_count(); i++) { 137 if (file_->message_type(i)->name() == classname_) { 138 found_conflict = true; 139 } 140 } 141 if (found_conflict) { 142 error->assign(file_->name()); 143 error->append( 144 ": Cannot generate Java output because the file's outer class name, \""); 145 error->append(classname_); 146 error->append( 147 "\", matches the name of one of the types declared inside it. " 148 "Please either rename the type or use the java_outer_classname " 149 "option to specify a different outer class name for the .proto file."); 150 return false; 151 } 152 return true; 153 } 154 155 void FileGenerator::Generate(io::Printer* printer) { 156 // We don't import anything because we refer to all classes by their 157 // fully-qualified names in the generated source. 158 printer->Print( 159 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" 160 "\n"); 161 if (!java_package_.empty()) { 162 printer->Print( 163 "package $package$;\n" 164 "\n", 165 "package", java_package_); 166 } 167 168 printer->Print( 169 "public final class $classname$ {\n" 170 " private $classname$() {}\n", 171 "classname", classname_); 172 printer->Indent(); 173 174 // ----------------------------------------------------------------- 175 176 // Extensions. 177 for (int i = 0; i < file_->extension_count(); i++) { 178 ExtensionGenerator(file_->extension(i), params_).Generate(printer); 179 } 180 181 // Enums. 182 for (int i = 0; i < file_->enum_type_count(); i++) { 183 EnumGenerator(file_->enum_type(i), params_).Generate(printer); 184 } 185 186 // Messages. 187 if (!params_.java_multiple_files(file_->name())) { 188 for (int i = 0; i < file_->message_type_count(); i++) { 189 MessageGenerator(file_->message_type(i), params_).Generate(printer); 190 } 191 } 192 193 // Static variables. 194 for (int i = 0; i < file_->message_type_count(); i++) { 195 // TODO(kenton): Reuse MessageGenerator objects? 196 MessageGenerator(file_->message_type(i), params_).GenerateStaticVariables(printer); 197 } 198 199 printer->Outdent(); 200 printer->Print( 201 "}\n"); 202 } 203 204 template<typename GeneratorClass, typename DescriptorClass> 205 static void GenerateSibling(const string& package_dir, 206 const string& java_package, 207 const DescriptorClass* descriptor, 208 OutputDirectory* output_directory, 209 vector<string>* file_list, 210 const Params& params) { 211 string filename = package_dir + descriptor->name() + ".java"; 212 file_list->push_back(filename); 213 214 scoped_ptr<io::ZeroCopyOutputStream> output( 215 output_directory->Open(filename)); 216 io::Printer printer(output.get(), '$'); 217 218 printer.Print( 219 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" 220 "\n"); 221 if (!java_package.empty()) { 222 printer.Print( 223 "package $package$;\n" 224 "\n", 225 "package", java_package); 226 } 227 228 GeneratorClass(descriptor, params).Generate(&printer); 229 } 230 231 void FileGenerator::GenerateSiblings(const string& package_dir, 232 OutputDirectory* output_directory, 233 vector<string>* file_list) { 234 if (params_.java_multiple_files(file_->name())) { 235 for (int i = 0; i < file_->message_type_count(); i++) { 236 GenerateSibling<MessageGenerator>(package_dir, java_package_, 237 file_->message_type(i), 238 output_directory, file_list, params_); 239 } 240 } 241 } 242 243 } // namespace javanano 244 } // namespace compiler 245 } // namespace protobuf 246 } // namespace google 247