javanano_helpers.cc revision bf79e2de36143b8b617af136c403496515373bb6
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@google.com (Kenton Varda) 32// Based on original Protocol Buffers design by 33// Sanjay Ghemawat, Jeff Dean, and others. 34 35#include <vector> 36 37#include <google/protobuf/compiler/javanano/javanano_helpers.h> 38#include <google/protobuf/compiler/javanano/javanano_params.h> 39#include <google/protobuf/descriptor.pb.h> 40#include <google/protobuf/stubs/strutil.h> 41#include <google/protobuf/stubs/substitute.h> 42 43namespace google { 44namespace protobuf { 45namespace compiler { 46namespace javanano { 47 48const char kThickSeparator[] = 49 "// ===================================================================\n"; 50const char kThinSeparator[] = 51 "// -------------------------------------------------------------------\n"; 52 53namespace { 54 55const char* kDefaultPackage = ""; 56 57const string& FieldName(const FieldDescriptor* field) { 58 // Groups are hacky: The name of the field is just the lower-cased name 59 // of the group type. In Java, though, we would like to retain the original 60 // capitalization of the type name. 61 if (field->type() == FieldDescriptor::TYPE_GROUP) { 62 return field->message_type()->name(); 63 } else { 64 return field->name(); 65 } 66} 67 68string UnderscoresToCamelCaseImpl(const string& input, bool cap_next_letter) { 69 string result; 70 // Note: I distrust ctype.h due to locales. 71 for (int i = 0; i < input.size(); i++) { 72 if ('a' <= input[i] && input[i] <= 'z') { 73 if (cap_next_letter) { 74 result += input[i] + ('A' - 'a'); 75 } else { 76 result += input[i]; 77 } 78 cap_next_letter = false; 79 } else if ('A' <= input[i] && input[i] <= 'Z') { 80 if (i == 0 && !cap_next_letter) { 81 // Force first letter to lower-case unless explicitly told to 82 // capitalize it. 83 result += input[i] + ('a' - 'A'); 84 } else { 85 // Capital letters after the first are left as-is. 86 result += input[i]; 87 } 88 cap_next_letter = false; 89 } else if ('0' <= input[i] && input[i] <= '9') { 90 result += input[i]; 91 cap_next_letter = true; 92 } else { 93 cap_next_letter = true; 94 } 95 } 96 return result; 97} 98 99} // namespace 100 101string UnderscoresToCamelCase(const FieldDescriptor* field) { 102 return UnderscoresToCamelCaseImpl(FieldName(field), false); 103} 104 105string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field) { 106 return UnderscoresToCamelCaseImpl(FieldName(field), true); 107} 108 109string UnderscoresToCamelCase(const MethodDescriptor* method) { 110 return UnderscoresToCamelCaseImpl(method->name(), false); 111} 112 113string StripProto(const string& filename) { 114 if (HasSuffixString(filename, ".protodevel")) { 115 return StripSuffixString(filename, ".protodevel"); 116 } else { 117 return StripSuffixString(filename, ".proto"); 118 } 119} 120 121string FileClassName(const Params& params, const FileDescriptor* file) { 122 string name; 123 124 if (params.has_java_outer_classname(file->name())) { 125 name = params.java_outer_classname(file->name()); 126 } else { 127 if ((file->message_type_count() == 1) 128 || (file->enum_type_count() == 0)) { 129 // If no outer calls and only one message then 130 // use the message name as the file name 131 name = file->message_type(0)->name(); 132 } else { 133 // Use the filename it self with underscores removed 134 // and a CamelCase style name. 135 string basename; 136 string::size_type last_slash = file->name().find_last_of('/'); 137 if (last_slash == string::npos) { 138 basename = file->name(); 139 } else { 140 basename = file->name().substr(last_slash + 1); 141 } 142 name = UnderscoresToCamelCaseImpl(StripProto(basename), true); 143 } 144 } 145 146 return name; 147} 148 149string FileJavaPackage(const Params& params, const FileDescriptor* file) { 150 if (params.has_java_package(file->name())) { 151 return params.java_package(file->name()); 152 } else { 153 string result = kDefaultPackage; 154 if (!file->package().empty()) { 155 if (!result.empty()) result += '.'; 156 result += file->package(); 157 } 158 return result; 159 } 160} 161 162string ToJavaName(const Params& params, const string& full_name, 163 const FileDescriptor* file) { 164 string result; 165 if (params.java_multiple_files()) { 166 result = FileJavaPackage(params, file); 167 } else { 168 result = ClassName(params, file); 169 } 170 if (file->package().empty()) { 171 result += '.'; 172 result += full_name; 173 } else { 174 // Strip the proto package from full_name since we've replaced it with 175 // the Java package. If there isn't an outer classname then strip it too. 176 int sizeToSkipPackageName = file->package().size(); 177 int sizeToSkipOutClassName; 178 if (params.has_java_outer_classname(file->name())) { 179 sizeToSkipOutClassName = 0; 180 } else { 181 sizeToSkipOutClassName = 182 full_name.find_first_of('.', sizeToSkipPackageName + 1); 183 } 184 int sizeToSkip = sizeToSkipOutClassName > 0 ? 185 sizeToSkipOutClassName : sizeToSkipPackageName; 186 string class_name = full_name.substr(sizeToSkip + 1); 187 if (class_name == FileClassName(params, file)) { 188 // Done class_name is already present. 189 } else { 190 result += '.'; 191 result += class_name; 192 } 193 } 194 return result; 195} 196 197string ClassName(const Params& params, const FileDescriptor* descriptor) { 198 string result = FileJavaPackage(params, descriptor); 199 if (!result.empty()) result += '.'; 200 result += FileClassName(params, descriptor); 201 return result; 202} 203 204string ClassName(const Params& params, const EnumDescriptor* descriptor) { 205 string result; 206 const FileDescriptor* file = descriptor->file(); 207 const string file_name = file->name(); 208 const string full_name = descriptor->full_name(); 209 210 // Remove enum class name as we use int's for enums 211 int last_dot_in_name = full_name.find_last_of('.'); 212 string base_name = full_name.substr(0, last_dot_in_name); 213 214 if (!file->package().empty()) { 215 if (file->package() == base_name.substr(0, file->package().size())) { 216 // Remove package name leaving just the parent class of the enum 217 int offset = file->package().size(); 218 if (base_name.size() > offset) { 219 // Remove period between package and class name if there is a classname 220 offset += 1; 221 } 222 base_name = base_name.substr(offset); 223 } else { 224 GOOGLE_LOG(FATAL) << "Expected package name to start an enum"; 225 } 226 } 227 228 // Construct the path name from the package and outer class 229 230 // Add the java package name if it exists 231 if (params.has_java_package(file_name)) { 232 result += params.java_package(file_name); 233 } 234 235 // If the java_multiple_files option is present, we will generate enums into separate 236 // classes, each named after the original enum type. This takes precedence over 237 // any outer_classname. 238 if (params.java_multiple_files() && last_dot_in_name != string::npos) { 239 string enum_simple_name = full_name.substr(last_dot_in_name + 1); 240 if (!result.empty()) { 241 result += "."; 242 } 243 result += enum_simple_name; 244 } else if (params.has_java_outer_classname(file_name)) { 245 // Add the outer classname if it exists 246 if (!result.empty()) { 247 result += "."; 248 } 249 result += params.java_outer_classname(file_name); 250 } 251 252 // Create the full class name from the base and path 253 if (!base_name.empty()) { 254 if (!result.empty()) { 255 result += "."; 256 } 257 result += base_name; 258 } 259 return result; 260} 261 262string FieldConstantName(const FieldDescriptor *field) { 263 string name = field->name() + "_FIELD_NUMBER"; 264 UpperString(&name); 265 return name; 266} 267 268string FieldDefaultConstantName(const FieldDescriptor *field) { 269 string name = field->name() + "_DEFAULT"; 270 UpperString(&name); 271 return name; 272} 273 274JavaType GetJavaType(FieldDescriptor::Type field_type) { 275 switch (field_type) { 276 case FieldDescriptor::TYPE_INT32: 277 case FieldDescriptor::TYPE_UINT32: 278 case FieldDescriptor::TYPE_SINT32: 279 case FieldDescriptor::TYPE_FIXED32: 280 case FieldDescriptor::TYPE_SFIXED32: 281 return JAVATYPE_INT; 282 283 case FieldDescriptor::TYPE_INT64: 284 case FieldDescriptor::TYPE_UINT64: 285 case FieldDescriptor::TYPE_SINT64: 286 case FieldDescriptor::TYPE_FIXED64: 287 case FieldDescriptor::TYPE_SFIXED64: 288 return JAVATYPE_LONG; 289 290 case FieldDescriptor::TYPE_FLOAT: 291 return JAVATYPE_FLOAT; 292 293 case FieldDescriptor::TYPE_DOUBLE: 294 return JAVATYPE_DOUBLE; 295 296 case FieldDescriptor::TYPE_BOOL: 297 return JAVATYPE_BOOLEAN; 298 299 case FieldDescriptor::TYPE_STRING: 300 return JAVATYPE_STRING; 301 302 case FieldDescriptor::TYPE_BYTES: 303 return JAVATYPE_BYTES; 304 305 case FieldDescriptor::TYPE_ENUM: 306 return JAVATYPE_ENUM; 307 308 case FieldDescriptor::TYPE_GROUP: 309 case FieldDescriptor::TYPE_MESSAGE: 310 return JAVATYPE_MESSAGE; 311 312 // No default because we want the compiler to complain if any new 313 // types are added. 314 } 315 316 GOOGLE_LOG(FATAL) << "Can't get here."; 317 return JAVATYPE_INT; 318} 319 320const char* BoxedPrimitiveTypeName(JavaType type) { 321 switch (type) { 322 case JAVATYPE_INT : return "java.lang.Integer"; 323 case JAVATYPE_LONG : return "java.lang.Long"; 324 case JAVATYPE_FLOAT : return "java.lang.Float"; 325 case JAVATYPE_DOUBLE : return "java.lang.Double"; 326 case JAVATYPE_BOOLEAN: return "java.lang.Boolean"; 327 case JAVATYPE_STRING : return "java.lang.String"; 328 case JAVATYPE_BYTES : return "byte[]"; 329 case JAVATYPE_ENUM : return "java.lang.Integer"; 330 case JAVATYPE_MESSAGE: return NULL; 331 332 // No default because we want the compiler to complain if any new 333 // JavaTypes are added. 334 } 335 336 GOOGLE_LOG(FATAL) << "Can't get here."; 337 return NULL; 338} 339 340string EmptyArrayName(const Params& params, const FieldDescriptor* field) { 341 switch (GetJavaType(field)) { 342 case JAVATYPE_INT : return "com.google.protobuf.nano.WireFormatNano.EMPTY_INT_ARRAY"; 343 case JAVATYPE_LONG : return "com.google.protobuf.nano.WireFormatNano.EMPTY_LONG_ARRAY"; 344 case JAVATYPE_FLOAT : return "com.google.protobuf.nano.WireFormatNano.EMPTY_FLOAT_ARRAY"; 345 case JAVATYPE_DOUBLE : return "com.google.protobuf.nano.WireFormatNano.EMPTY_DOUBLE_ARRAY"; 346 case JAVATYPE_BOOLEAN: return "com.google.protobuf.nano.WireFormatNano.EMPTY_BOOLEAN_ARRAY"; 347 case JAVATYPE_STRING : return "com.google.protobuf.nano.WireFormatNano.EMPTY_STRING_ARRAY"; 348 case JAVATYPE_BYTES : return "com.google.protobuf.nano.WireFormatNano.EMPTY_BYTES_ARRAY"; 349 case JAVATYPE_ENUM : return "com.google.protobuf.nano.WireFormatNano.EMPTY_INT_ARRAY"; 350 case JAVATYPE_MESSAGE: return ClassName(params, field->message_type()) + ".EMPTY_ARRAY"; 351 352 // No default because we want the compiler to complain if any new 353 // JavaTypes are added. 354 } 355 356 GOOGLE_LOG(FATAL) << "Can't get here."; 357 return ""; 358} 359 360string DefaultValue(const Params& params, const FieldDescriptor* field) { 361 if (field->label() == FieldDescriptor::LABEL_REPEATED) { 362 return EmptyArrayName(params, field); 363 } 364 365 // Switch on cpp_type since we need to know which default_value_* method 366 // of FieldDescriptor to call. 367 switch (field->cpp_type()) { 368 case FieldDescriptor::CPPTYPE_INT32: 369 return SimpleItoa(field->default_value_int32()); 370 case FieldDescriptor::CPPTYPE_UINT32: 371 // Need to print as a signed int since Java has no unsigned. 372 return SimpleItoa(static_cast<int32>(field->default_value_uint32())); 373 case FieldDescriptor::CPPTYPE_INT64: 374 return SimpleItoa(field->default_value_int64()) + "L"; 375 case FieldDescriptor::CPPTYPE_UINT64: 376 return SimpleItoa(static_cast<int64>(field->default_value_uint64())) + 377 "L"; 378 case FieldDescriptor::CPPTYPE_DOUBLE: 379 return SimpleDtoa(field->default_value_double()) + "D"; 380 case FieldDescriptor::CPPTYPE_FLOAT: 381 return SimpleFtoa(field->default_value_float()) + "F"; 382 case FieldDescriptor::CPPTYPE_BOOL: 383 return field->default_value_bool() ? "true" : "false"; 384 case FieldDescriptor::CPPTYPE_STRING: 385 if (!field->default_value_string().empty()) { 386 // Point it to the static final in the generated code. 387 return FieldDefaultConstantName(field); 388 } else { 389 if (field->type() == FieldDescriptor::TYPE_BYTES) { 390 return "com.google.protobuf.nano.WireFormatNano.EMPTY_BYTES"; 391 } else { 392 return "\"\""; 393 } 394 } 395 396 case FieldDescriptor::CPPTYPE_ENUM: 397 return ClassName(params, field->enum_type()) + "." + 398 field->default_value_enum()->name(); 399 400 case FieldDescriptor::CPPTYPE_MESSAGE: 401 return "null"; 402 403 // No default because we want the compiler to complain if any new 404 // types are added. 405 } 406 407 GOOGLE_LOG(FATAL) << "Can't get here."; 408 return ""; 409} 410 411} // namespace javanano 412} // namespace compiler 413} // namespace protobuf 414} // namespace google 415