| 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 18 matching lines...) Expand all Loading... |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | 34 |
| 35 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ | 35 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ |
| 36 #define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ | 36 #define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ |
| 37 | 37 |
| 38 #include <string> | 38 #include <string> |
| 39 #include <google/protobuf/compiler/java/java_context.h> | |
| 40 #include <google/protobuf/io/printer.h> | |
| 41 #include <google/protobuf/descriptor.pb.h> | 39 #include <google/protobuf/descriptor.pb.h> |
| 42 #include <google/protobuf/descriptor.h> | 40 #include <google/protobuf/descriptor.h> |
| 43 | 41 |
| 44 namespace google { | 42 namespace google { |
| 45 namespace protobuf { | 43 namespace protobuf { |
| 46 namespace compiler { | 44 namespace compiler { |
| 47 namespace java { | 45 namespace java { |
| 48 | 46 |
| 49 // Commonly-used separator comments. Thick is a line of '=', thin is a line | 47 // Commonly-used separator comments. Thick is a line of '=', thin is a line |
| 50 // of '-'. | 48 // of '-'. |
| 51 extern const char kThickSeparator[]; | 49 extern const char kThickSeparator[]; |
| 52 extern const char kThinSeparator[]; | 50 extern const char kThinSeparator[]; |
| 53 | 51 |
| 54 // If annotation_file is non-empty, prints a javax.annotation.Generated | |
| 55 // annotation to the given Printer. annotation_file will be referenced in the | |
| 56 // annotation's comments field. delimiter should be the Printer's delimiter | |
| 57 // character. annotation_file will be included verbatim into a Java literal | |
| 58 // string, so it should not contain quotes or invalid Java escape sequences; | |
| 59 // however, these are unlikely to appear in practice, as the value of | |
| 60 // annotation_file should be generated from the filename of the source file | |
| 61 // being annotated (which in turn must be a Java identifier plus ".java"). | |
| 62 void PrintGeneratedAnnotation(io::Printer* printer, char delimiter = '$', | |
| 63 const string& annotation_file = ""); | |
| 64 | |
| 65 // Converts a name to camel-case. If cap_first_letter is true, capitalize the | 52 // Converts a name to camel-case. If cap_first_letter is true, capitalize the |
| 66 // first letter. | 53 // first letter. |
| 67 string UnderscoresToCamelCase(const string& name, bool cap_first_letter); | 54 string UnderscoresToCamelCase(const string& name, bool cap_first_letter); |
| 68 // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes | 55 // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes |
| 69 // "fooBarBaz" or "FooBarBaz", respectively. | 56 // "fooBarBaz" or "FooBarBaz", respectively. |
| 70 string UnderscoresToCamelCase(const FieldDescriptor* field); | 57 string UnderscoresToCamelCase(const FieldDescriptor* field); |
| 71 string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); | 58 string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); |
| 72 | 59 |
| 73 // Similar, but for method names. (Typically, this merely has the effect | 60 // Similar, but for method names. (Typically, this merely has the effect |
| 74 // of lower-casing the first letter of the name.) | 61 // of lower-casing the first letter of the name.) |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 return descriptor->name(); | 119 return descriptor->name(); |
| 133 } | 120 } |
| 134 | 121 |
| 135 | 122 |
| 136 // Whether we should generate multiple java files for messages. | 123 // Whether we should generate multiple java files for messages. |
| 137 inline bool MultipleJavaFiles( | 124 inline bool MultipleJavaFiles( |
| 138 const FileDescriptor* descriptor, bool immutable) { | 125 const FileDescriptor* descriptor, bool immutable) { |
| 139 return descriptor->options().java_multiple_files(); | 126 return descriptor->options().java_multiple_files(); |
| 140 } | 127 } |
| 141 | 128 |
| 142 // Returns true if `descriptor` will be written to its own .java file. | |
| 143 // `immutable` should be set to true if we're generating for the immutable API. | |
| 144 template <typename Descriptor> | |
| 145 bool IsOwnFile(const Descriptor* descriptor, bool immutable) { | |
| 146 return descriptor->containing_type() == NULL && | |
| 147 MultipleJavaFiles(descriptor->file(), immutable); | |
| 148 } | |
| 149 | |
| 150 template <> | |
| 151 inline bool IsOwnFile(const ServiceDescriptor* descriptor, bool immutable) { | |
| 152 return MultipleJavaFiles(descriptor->file(), immutable); | |
| 153 } | |
| 154 | |
| 155 // If `descriptor` describes an object with its own .java file, | |
| 156 // returns the name (relative to that .java file) of the file that stores | |
| 157 // annotation data for that descriptor. `suffix` is usually empty, but may | |
| 158 // (e.g.) be "OrBuilder" for some generated interfaces. | |
| 159 template <typename Descriptor> | |
| 160 string AnnotationFileName(const Descriptor* descriptor, const string& suffix) { | |
| 161 return descriptor->name() + suffix + ".java.pb.meta"; | |
| 162 } | |
| 163 | |
| 164 template <typename Descriptor> | |
| 165 void MaybePrintGeneratedAnnotation(Context* context, io::Printer* printer, | |
| 166 Descriptor* descriptor, bool immutable, | |
| 167 const string& suffix = "") { | |
| 168 if (context->options().annotate_code && IsOwnFile(descriptor, immutable)) { | |
| 169 PrintGeneratedAnnotation(printer, '$', | |
| 170 AnnotationFileName(descriptor, suffix)); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 // Get the unqualified name that should be used for a field's field | 129 // Get the unqualified name that should be used for a field's field |
| 175 // number constant. | 130 // number constant. |
| 176 string FieldConstantName(const FieldDescriptor *field); | 131 string FieldConstantName(const FieldDescriptor *field); |
| 177 | 132 |
| 178 // Returns the type of the FieldDescriptor. | 133 // Returns the type of the FieldDescriptor. |
| 179 // This does nothing interesting for the open source release, but is used for | 134 // This does nothing interesting for the open source release, but is used for |
| 180 // hacks that improve compatibility with version 1 protocol buffers at Google. | 135 // hacks that improve compatibility with version 1 protocol buffers at Google. |
| 181 FieldDescriptor::Type GetType(const FieldDescriptor* field); | 136 FieldDescriptor::Type GetType(const FieldDescriptor* field); |
| 182 | 137 |
| 183 enum JavaType { | 138 enum JavaType { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 207 const char* FieldTypeName(const FieldDescriptor::Type field_type); | 162 const char* FieldTypeName(const FieldDescriptor::Type field_type); |
| 208 | 163 |
| 209 class ClassNameResolver; | 164 class ClassNameResolver; |
| 210 string DefaultValue(const FieldDescriptor* field, bool immutable, | 165 string DefaultValue(const FieldDescriptor* field, bool immutable, |
| 211 ClassNameResolver* name_resolver); | 166 ClassNameResolver* name_resolver); |
| 212 inline string ImmutableDefaultValue(const FieldDescriptor* field, | 167 inline string ImmutableDefaultValue(const FieldDescriptor* field, |
| 213 ClassNameResolver* name_resolver) { | 168 ClassNameResolver* name_resolver) { |
| 214 return DefaultValue(field, true, name_resolver); | 169 return DefaultValue(field, true, name_resolver); |
| 215 } | 170 } |
| 216 bool IsDefaultValueJavaDefault(const FieldDescriptor* field); | 171 bool IsDefaultValueJavaDefault(const FieldDescriptor* field); |
| 217 bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field); | 172 |
| 173 // Does this message have specialized equals() and hashCode() methods? |
| 174 inline bool HasEqualsAndHashCode(const Descriptor* descriptor) { |
| 175 return descriptor->file()->options().java_generate_equals_and_hash(); |
| 176 } |
| 218 | 177 |
| 219 // Does this message class have descriptor and reflection methods? | 178 // Does this message class have descriptor and reflection methods? |
| 220 inline bool HasDescriptorMethods(const Descriptor* descriptor, | 179 inline bool HasDescriptorMethods(const Descriptor* descriptor, |
| 221 bool enforce_lite) { | 180 bool enforce_lite) { |
| 222 return !enforce_lite && | 181 return !enforce_lite && |
| 223 descriptor->file()->options().optimize_for() != | 182 descriptor->file()->options().optimize_for() != |
| 224 FileOptions::LITE_RUNTIME; | 183 FileOptions::LITE_RUNTIME; |
| 225 } | 184 } |
| 226 inline bool HasDescriptorMethods(const EnumDescriptor* descriptor, | 185 inline bool HasDescriptorMethods(const EnumDescriptor* descriptor, |
| 227 bool enforce_lite) { | 186 bool enforce_lite) { |
| 228 return !enforce_lite && | 187 return !enforce_lite && |
| 229 descriptor->file()->options().optimize_for() != | 188 descriptor->file()->options().optimize_for() != |
| 230 FileOptions::LITE_RUNTIME; | 189 FileOptions::LITE_RUNTIME; |
| 231 } | 190 } |
| 232 inline bool HasDescriptorMethods(const FileDescriptor* descriptor, | 191 inline bool HasDescriptorMethods(const FileDescriptor* descriptor, |
| 233 bool enforce_lite) { | 192 bool enforce_lite) { |
| 234 return !enforce_lite && | 193 return !enforce_lite && |
| 235 descriptor->options().optimize_for() != FileOptions::LITE_RUNTIME; | 194 descriptor->options().optimize_for() != FileOptions::LITE_RUNTIME; |
| 236 } | 195 } |
| 237 | 196 |
| 238 // Should we generate generic services for this file? | 197 // Should we generate generic services for this file? |
| 239 inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) { | 198 inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) { |
| 240 return file->service_count() > 0 && | 199 return file->service_count() > 0 && |
| 241 HasDescriptorMethods(file, enforce_lite) && | 200 HasDescriptorMethods(file, enforce_lite) && |
| 242 file->options().java_generic_services(); | 201 file->options().java_generic_services(); |
| 243 } | 202 } |
| 244 | 203 |
| 245 inline bool IsLazy(const FieldDescriptor* descriptor, bool enforce_lite) { | 204 inline bool IsLazy(const FieldDescriptor* descriptor, bool enforce_lite) { |
| 246 // Currently, the proto-lite version supports lazy field. | 205 // Currently, the proto-lite version suports lazy field. |
| 247 // TODO(niwasaki): Support lazy fields also for other proto runtimes. | 206 // TODO(niwasaki): Support lazy fields also for other proto runtimes. |
| 248 if (HasDescriptorMethods(descriptor->file(), enforce_lite)) { | 207 if (HasDescriptorMethods(descriptor->file(), enforce_lite)) { |
| 249 return false; | 208 return false; |
| 250 } | 209 } |
| 251 return descriptor->options().lazy(); | 210 return descriptor->options().lazy(); |
| 252 } | 211 } |
| 253 | 212 |
| 254 // Methods for shared bitfields. | 213 // Methods for shared bitfields. |
| 255 | 214 |
| 256 // Gets the name of the shared bitfield for the given index. | 215 // Gets the name of the shared bitfield for the given index. |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 | 337 |
| 379 inline bool IsAnyMessage(const Descriptor* descriptor) { | 338 inline bool IsAnyMessage(const Descriptor* descriptor) { |
| 380 return descriptor->full_name() == "google.protobuf.Any"; | 339 return descriptor->full_name() == "google.protobuf.Any"; |
| 381 } | 340 } |
| 382 | 341 |
| 383 inline bool CheckUtf8(const FieldDescriptor* descriptor) { | 342 inline bool CheckUtf8(const FieldDescriptor* descriptor) { |
| 384 return descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 || | 343 return descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 || |
| 385 descriptor->file()->options().java_string_check_utf8(); | 344 descriptor->file()->options().java_string_check_utf8(); |
| 386 } | 345 } |
| 387 | 346 |
| 388 inline string GeneratedCodeVersionSuffix() { | |
| 389 return "V3"; | |
| 390 } | |
| 391 } // namespace java | 347 } // namespace java |
| 392 } // namespace compiler | 348 } // namespace compiler |
| 393 } // namespace protobuf | 349 } // namespace protobuf |
| 394 | 350 |
| 395 } // namespace google | 351 } // namespace google |
| 396 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ | 352 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ |
| OLD | NEW |