javamicro_helpers.cc revision ede38fe9b9f93888e6e41afc7abb09525f44da95
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/javamicro/javamicro_helpers.h> 38#include <google/protobuf/compiler/javamicro/javamicro_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 javamicro { 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 int last_period = full_name.find_last_of('.'); 210 int first_period = full_name.find_first_of('.'); 211 212 // Remove class_name as we're using public static final int's not enums 213 string base_name = full_name.substr(0, full_name.find_last_of('.')); 214 215 if (!file->package().empty()) { 216 // Remove package name. 217 int offset = first_period; 218 if (last_period > first_period) { 219 // There was two or more periods so we need to remove this one too. 220 offset += 1; 221 } 222 base_name = base_name.substr(offset); 223 } 224 225 if (params.has_java_package(file_name)) { 226 result += params.java_package(file_name); 227 } 228 if (params.has_java_outer_classname(file_name)) { 229 result += "."; 230 result += params.java_outer_classname(file_name); 231 } 232 if (!base_name.empty()) { 233 result += "."; 234 result += base_name; 235 } 236 return result; 237} 238 239string FieldConstantName(const FieldDescriptor *field) { 240 string name = field->name() + "_FIELD_NUMBER"; 241 UpperString(&name); 242 return name; 243} 244 245JavaType GetJavaType(FieldDescriptor::Type field_type) { 246 switch (field_type) { 247 case FieldDescriptor::TYPE_INT32: 248 case FieldDescriptor::TYPE_UINT32: 249 case FieldDescriptor::TYPE_SINT32: 250 case FieldDescriptor::TYPE_FIXED32: 251 case FieldDescriptor::TYPE_SFIXED32: 252 return JAVATYPE_INT; 253 254 case FieldDescriptor::TYPE_INT64: 255 case FieldDescriptor::TYPE_UINT64: 256 case FieldDescriptor::TYPE_SINT64: 257 case FieldDescriptor::TYPE_FIXED64: 258 case FieldDescriptor::TYPE_SFIXED64: 259 return JAVATYPE_LONG; 260 261 case FieldDescriptor::TYPE_FLOAT: 262 return JAVATYPE_FLOAT; 263 264 case FieldDescriptor::TYPE_DOUBLE: 265 return JAVATYPE_DOUBLE; 266 267 case FieldDescriptor::TYPE_BOOL: 268 return JAVATYPE_BOOLEAN; 269 270 case FieldDescriptor::TYPE_STRING: 271 return JAVATYPE_STRING; 272 273 case FieldDescriptor::TYPE_BYTES: 274 return JAVATYPE_BYTES; 275 276 case FieldDescriptor::TYPE_ENUM: 277 return JAVATYPE_ENUM; 278 279 case FieldDescriptor::TYPE_GROUP: 280 case FieldDescriptor::TYPE_MESSAGE: 281 return JAVATYPE_MESSAGE; 282 283 // No default because we want the compiler to complain if any new 284 // types are added. 285 } 286 287 GOOGLE_LOG(FATAL) << "Can't get here."; 288 return JAVATYPE_INT; 289} 290 291const char* BoxedPrimitiveTypeName(JavaType type) { 292 switch (type) { 293 case JAVATYPE_INT : return "java.lang.Integer"; 294 case JAVATYPE_LONG : return "java.lang.Long"; 295 case JAVATYPE_FLOAT : return "java.lang.Float"; 296 case JAVATYPE_DOUBLE : return "java.lang.Double"; 297 case JAVATYPE_BOOLEAN: return "java.lang.Boolean"; 298 case JAVATYPE_STRING : return "java.lang.String"; 299 case JAVATYPE_BYTES : return "com.google.protobuf.micro.ByteStringMicro"; 300 case JAVATYPE_ENUM : return "java.lang.Integer"; 301 case JAVATYPE_MESSAGE: return NULL; 302 303 // No default because we want the compiler to complain if any new 304 // JavaTypes are added. 305 } 306 307 GOOGLE_LOG(FATAL) << "Can't get here."; 308 return NULL; 309} 310 311bool AllAscii(const string& text) { 312 for (int i = 0; i < text.size(); i++) { 313 if ((text[i] & 0x80) != 0) { 314 return false; 315 } 316 } 317 return true; 318} 319 320string DefaultValue(const Params& params, const FieldDescriptor* field) { 321 // Switch on cpp_type since we need to know which default_value_* method 322 // of FieldDescriptor to call. 323 switch (field->cpp_type()) { 324 case FieldDescriptor::CPPTYPE_INT32: 325 return SimpleItoa(field->default_value_int32()); 326 case FieldDescriptor::CPPTYPE_UINT32: 327 // Need to print as a signed int since Java has no unsigned. 328 return SimpleItoa(static_cast<int32>(field->default_value_uint32())); 329 case FieldDescriptor::CPPTYPE_INT64: 330 return SimpleItoa(field->default_value_int64()) + "L"; 331 case FieldDescriptor::CPPTYPE_UINT64: 332 return SimpleItoa(static_cast<int64>(field->default_value_uint64())) + 333 "L"; 334 case FieldDescriptor::CPPTYPE_DOUBLE: 335 return SimpleDtoa(field->default_value_double()) + "D"; 336 case FieldDescriptor::CPPTYPE_FLOAT: 337 return SimpleFtoa(field->default_value_float()) + "F"; 338 case FieldDescriptor::CPPTYPE_BOOL: 339 return field->default_value_bool() ? "true" : "false"; 340 case FieldDescriptor::CPPTYPE_STRING: 341 if (field->type() == FieldDescriptor::TYPE_BYTES) { 342 if (field->has_default_value()) { 343 // See comments in Internal.java for gory details. 344 return strings::Substitute( 345 "com.google.protobuf.micro.ByteStringMicro.copyFromUtf8(\"$0\")", 346 CEscape(field->default_value_string())); 347 } else { 348 return "com.google.protobuf.micro.ByteStringMicro.EMPTY"; 349 } 350 } else { 351 if (AllAscii(field->default_value_string())) { 352 // All chars are ASCII. In this case CEscape() works fine. 353 return "\"" + CEscape(field->default_value_string()) + "\""; 354 } else { 355 // See comments in Internal.java for gory details. 356 // BUG: Internal NOT SUPPORTED need to fix!! 357 return strings::Substitute( 358 "com.google.protobuf.micro.Internal.stringDefaultValue(\"$0\")", 359 CEscape(field->default_value_string())); 360 } 361 } 362 363 case FieldDescriptor::CPPTYPE_ENUM: 364 return ClassName(params, field->enum_type()) + "." + 365 field->default_value_enum()->name(); 366 367 case FieldDescriptor::CPPTYPE_MESSAGE: 368 return ClassName(params, field->message_type()) + ".getDefaultInstance()"; 369 370 // No default because we want the compiler to complain if any new 371 // types are added. 372 } 373 374 GOOGLE_LOG(FATAL) << "Can't get here."; 375 return ""; 376} 377 378} // namespace javamicro 379} // namespace compiler 380} // namespace protobuf 381} // namespace google 382