| 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> |
| 39 #include <google/protobuf/descriptor.pb.h> | 41 #include <google/protobuf/descriptor.pb.h> |
| 40 #include <google/protobuf/descriptor.h> | 42 #include <google/protobuf/descriptor.h> |
| 41 | 43 |
| 42 namespace google { | 44 namespace google { |
| 43 namespace protobuf { | 45 namespace protobuf { |
| 44 namespace compiler { | 46 namespace compiler { |
| 45 namespace java { | 47 namespace java { |
| 46 | 48 |
| 47 // Commonly-used separator comments. Thick is a line of '=', thin is a line | 49 // Commonly-used separator comments. Thick is a line of '=', thin is a line |
| 48 // of '-'. | 50 // of '-'. |
| 49 extern const char kThickSeparator[]; | 51 extern const char kThickSeparator[]; |
| 50 extern const char kThinSeparator[]; | 52 extern const char kThinSeparator[]; |
| 51 | 53 |
| 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 |
| 52 // Converts a name to camel-case. If cap_first_letter is true, capitalize the | 65 // Converts a name to camel-case. If cap_first_letter is true, capitalize the |
| 53 // first letter. | 66 // first letter. |
| 54 string UnderscoresToCamelCase(const string& name, bool cap_first_letter); | 67 string UnderscoresToCamelCase(const string& name, bool cap_first_letter); |
| 55 // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes | 68 // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes |
| 56 // "fooBarBaz" or "FooBarBaz", respectively. | 69 // "fooBarBaz" or "FooBarBaz", respectively. |
| 57 string UnderscoresToCamelCase(const FieldDescriptor* field); | 70 string UnderscoresToCamelCase(const FieldDescriptor* field); |
| 58 string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); | 71 string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); |
| 59 | 72 |
| 60 // Similar, but for method names. (Typically, this merely has the effect | 73 // Similar, but for method names. (Typically, this merely has the effect |
| 61 // of lower-casing the first letter of the name.) | 74 // of lower-casing the first letter of the name.) |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 return descriptor->name(); | 132 return descriptor->name(); |
| 120 } | 133 } |
| 121 | 134 |
| 122 | 135 |
| 123 // Whether we should generate multiple java files for messages. | 136 // Whether we should generate multiple java files for messages. |
| 124 inline bool MultipleJavaFiles( | 137 inline bool MultipleJavaFiles( |
| 125 const FileDescriptor* descriptor, bool immutable) { | 138 const FileDescriptor* descriptor, bool immutable) { |
| 126 return descriptor->options().java_multiple_files(); | 139 return descriptor->options().java_multiple_files(); |
| 127 } | 140 } |
| 128 | 141 |
| 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 |
| 129 // Get the unqualified name that should be used for a field's field | 174 // Get the unqualified name that should be used for a field's field |
| 130 // number constant. | 175 // number constant. |
| 131 string FieldConstantName(const FieldDescriptor *field); | 176 string FieldConstantName(const FieldDescriptor *field); |
| 132 | 177 |
| 133 // Returns the type of the FieldDescriptor. | 178 // Returns the type of the FieldDescriptor. |
| 134 // This does nothing interesting for the open source release, but is used for | 179 // This does nothing interesting for the open source release, but is used for |
| 135 // hacks that improve compatibility with version 1 protocol buffers at Google. | 180 // hacks that improve compatibility with version 1 protocol buffers at Google. |
| 136 FieldDescriptor::Type GetType(const FieldDescriptor* field); | 181 FieldDescriptor::Type GetType(const FieldDescriptor* field); |
| 137 | 182 |
| 138 enum JavaType { | 183 enum JavaType { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 162 const char* FieldTypeName(const FieldDescriptor::Type field_type); | 207 const char* FieldTypeName(const FieldDescriptor::Type field_type); |
| 163 | 208 |
| 164 class ClassNameResolver; | 209 class ClassNameResolver; |
| 165 string DefaultValue(const FieldDescriptor* field, bool immutable, | 210 string DefaultValue(const FieldDescriptor* field, bool immutable, |
| 166 ClassNameResolver* name_resolver); | 211 ClassNameResolver* name_resolver); |
| 167 inline string ImmutableDefaultValue(const FieldDescriptor* field, | 212 inline string ImmutableDefaultValue(const FieldDescriptor* field, |
| 168 ClassNameResolver* name_resolver) { | 213 ClassNameResolver* name_resolver) { |
| 169 return DefaultValue(field, true, name_resolver); | 214 return DefaultValue(field, true, name_resolver); |
| 170 } | 215 } |
| 171 bool IsDefaultValueJavaDefault(const FieldDescriptor* field); | 216 bool IsDefaultValueJavaDefault(const FieldDescriptor* field); |
| 172 | 217 bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field); |
| 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 } | |
| 177 | 218 |
| 178 // Does this message class have descriptor and reflection methods? | 219 // Does this message class have descriptor and reflection methods? |
| 179 inline bool HasDescriptorMethods(const Descriptor* descriptor, | 220 inline bool HasDescriptorMethods(const Descriptor* descriptor, |
| 180 bool enforce_lite) { | 221 bool enforce_lite) { |
| 181 return !enforce_lite && | 222 return !enforce_lite && |
| 182 descriptor->file()->options().optimize_for() != | 223 descriptor->file()->options().optimize_for() != |
| 183 FileOptions::LITE_RUNTIME; | 224 FileOptions::LITE_RUNTIME; |
| 184 } | 225 } |
| 185 inline bool HasDescriptorMethods(const EnumDescriptor* descriptor, | 226 inline bool HasDescriptorMethods(const EnumDescriptor* descriptor, |
| 186 bool enforce_lite) { | 227 bool enforce_lite) { |
| 187 return !enforce_lite && | 228 return !enforce_lite && |
| 188 descriptor->file()->options().optimize_for() != | 229 descriptor->file()->options().optimize_for() != |
| 189 FileOptions::LITE_RUNTIME; | 230 FileOptions::LITE_RUNTIME; |
| 190 } | 231 } |
| 191 inline bool HasDescriptorMethods(const FileDescriptor* descriptor, | 232 inline bool HasDescriptorMethods(const FileDescriptor* descriptor, |
| 192 bool enforce_lite) { | 233 bool enforce_lite) { |
| 193 return !enforce_lite && | 234 return !enforce_lite && |
| 194 descriptor->options().optimize_for() != FileOptions::LITE_RUNTIME; | 235 descriptor->options().optimize_for() != FileOptions::LITE_RUNTIME; |
| 195 } | 236 } |
| 196 | 237 |
| 197 // Should we generate generic services for this file? | 238 // Should we generate generic services for this file? |
| 198 inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) { | 239 inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) { |
| 199 return file->service_count() > 0 && | 240 return file->service_count() > 0 && |
| 200 HasDescriptorMethods(file, enforce_lite) && | 241 HasDescriptorMethods(file, enforce_lite) && |
| 201 file->options().java_generic_services(); | 242 file->options().java_generic_services(); |
| 202 } | 243 } |
| 203 | 244 |
| 204 inline bool IsLazy(const FieldDescriptor* descriptor, bool enforce_lite) { | 245 inline bool IsLazy(const FieldDescriptor* descriptor, bool enforce_lite) { |
| 205 // Currently, the proto-lite version suports lazy field. | 246 // Currently, the proto-lite version supports lazy field. |
| 206 // TODO(niwasaki): Support lazy fields also for other proto runtimes. | 247 // TODO(niwasaki): Support lazy fields also for other proto runtimes. |
| 207 if (HasDescriptorMethods(descriptor->file(), enforce_lite)) { | 248 if (HasDescriptorMethods(descriptor->file(), enforce_lite)) { |
| 208 return false; | 249 return false; |
| 209 } | 250 } |
| 210 return descriptor->options().lazy(); | 251 return descriptor->options().lazy(); |
| 211 } | 252 } |
| 212 | 253 |
| 213 // Methods for shared bitfields. | 254 // Methods for shared bitfields. |
| 214 | 255 |
| 215 // Gets the name of the shared bitfield for the given index. | 256 // Gets the name of the shared bitfield for the given index. |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 378 |
| 338 inline bool IsAnyMessage(const Descriptor* descriptor) { | 379 inline bool IsAnyMessage(const Descriptor* descriptor) { |
| 339 return descriptor->full_name() == "google.protobuf.Any"; | 380 return descriptor->full_name() == "google.protobuf.Any"; |
| 340 } | 381 } |
| 341 | 382 |
| 342 inline bool CheckUtf8(const FieldDescriptor* descriptor) { | 383 inline bool CheckUtf8(const FieldDescriptor* descriptor) { |
| 343 return descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 || | 384 return descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 || |
| 344 descriptor->file()->options().java_string_check_utf8(); | 385 descriptor->file()->options().java_string_check_utf8(); |
| 345 } | 386 } |
| 346 | 387 |
| 388 inline string GeneratedCodeVersionSuffix() { |
| 389 return "V3"; |
| 390 } |
| 347 } // namespace java | 391 } // namespace java |
| 348 } // namespace compiler | 392 } // namespace compiler |
| 349 } // namespace protobuf | 393 } // namespace protobuf |
| 350 | 394 |
| 351 } // namespace google | 395 } // namespace google |
| 352 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ | 396 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ |
| OLD | NEW |