• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
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 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
36 #define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
37 
38 #include <string>
39 #include <google/protobuf/compiler/java/java_context.h>
40 #include <google/protobuf/descriptor.pb.h>
41 #include <google/protobuf/io/printer.h>
42 #include <google/protobuf/descriptor.h>
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace java {
48 
49 // Commonly-used separator comments.  Thick is a line of '=', thin is a line
50 // of '-'.
51 extern const char kThickSeparator[];
52 extern const char kThinSeparator[];
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 std::string& annotation_file = "");
64 
65 // If a GeneratedMessageLite contains non-lite enums, then its verifier
66 // must be instantiated inline, rather than retrieved from the enum class.
67 void PrintEnumVerifierLogic(io::Printer* printer,
68                             const FieldDescriptor* descriptor,
69                             const std::map<std::string, std::string>& variables,
70                             const char* var_name,
71                             const char* terminating_string, bool enforce_lite);
72 
73 // Converts a name to camel-case. If cap_first_letter is true, capitalize the
74 // first letter.
75 std::string UnderscoresToCamelCase(const std::string& name,
76                                    bool cap_first_letter);
77 // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes
78 // "fooBarBaz" or "FooBarBaz", respectively.
79 std::string UnderscoresToCamelCase(const FieldDescriptor* field);
80 std::string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field);
81 
82 // Similar, but for method names.  (Typically, this merely has the effect
83 // of lower-casing the first letter of the name.)
84 std::string UnderscoresToCamelCase(const MethodDescriptor* method);
85 
86 // Same as UnderscoresToCamelCase, but checks for reserved keywords
87 std::string UnderscoresToCamelCaseCheckReserved(const FieldDescriptor* field);
88 
89 // Similar to UnderscoresToCamelCase, but guarentees that the result is a
90 // complete Java identifier by adding a _ if needed.
91 std::string CamelCaseFieldName(const FieldDescriptor* field);
92 
93 // Get an identifier that uniquely identifies this type within the file.
94 // This is used to declare static variables related to this type at the
95 // outermost file scope.
96 std::string UniqueFileScopeIdentifier(const Descriptor* descriptor);
97 
98 // Strips ".proto" or ".protodevel" from the end of a filename.
99 std::string StripProto(const std::string& filename);
100 
101 // Gets the unqualified class name for the file.  For each .proto file, there
102 // will be one Java class containing all the immutable messages and another
103 // Java class containing all the mutable messages.
104 // TODO(xiaofeng): remove the default value after updating client code.
105 std::string FileClassName(const FileDescriptor* file, bool immutable = true);
106 
107 // Returns the file's Java package name.
108 std::string FileJavaPackage(const FileDescriptor* file);
109 std::string FileJavaPackage(const FileDescriptor* file, bool immutable);
110 
111 // Returns output directory for the given package name.
112 std::string JavaPackageToDir(std::string package_name);
113 
114 // TODO(xiaofeng): the following methods are kept for they are exposed
115 // publicly in //net/proto2/compiler/java/public/names.h. They return
116 // immutable names only and should be removed after mutable API is
117 // integrated into google3.
118 std::string ClassName(const Descriptor* descriptor);
119 std::string ClassName(const EnumDescriptor* descriptor);
120 std::string ClassName(const ServiceDescriptor* descriptor);
121 std::string ClassName(const FileDescriptor* descriptor);
122 
123 // Comma-separate list of option-specified interfaces implemented by the
124 // Message, to follow the "implements" declaration of the Message definition.
125 std::string ExtraMessageInterfaces(const Descriptor* descriptor);
126 // Comma-separate list of option-specified interfaces implemented by the
127 // MutableMessage, to follow the "implements" declaration of the MutableMessage
128 // definition.
129 std::string ExtraMutableMessageInterfaces(const Descriptor* descriptor);
130 // Comma-separate list of option-specified interfaces implemented by the
131 // Builder, to follow the "implements" declaration of the Builder definition.
132 std::string ExtraBuilderInterfaces(const Descriptor* descriptor);
133 // Comma-separate list of option-specified interfaces extended by the
134 // MessageOrBuilder, to follow the "extends" declaration of the
135 // MessageOrBuilder definition.
136 std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor);
137 
138 // Get the unqualified Java class name for mutable messages. i.e. without
139 // package or outer classnames.
ShortMutableJavaClassName(const Descriptor * descriptor)140 inline std::string ShortMutableJavaClassName(const Descriptor* descriptor) {
141   return descriptor->name();
142 }
143 
144 // Whether the given descriptor is for one of the core descriptor protos. We
145 // cannot currently use the new runtime with core protos since there is a
146 // bootstrapping problem with obtaining their descriptors.
IsDescriptorProto(const Descriptor * descriptor)147 inline bool IsDescriptorProto(const Descriptor* descriptor) {
148   return descriptor->file()->name() == "net/proto2/proto/descriptor.proto" ||
149          descriptor->file()->name() == "google/protobuf/descriptor.proto";
150 }
151 
152 // Returns the stored type string used by the experimental runtime for oneof
153 // fields.
154 std::string GetOneofStoredType(const FieldDescriptor* field);
155 
156 
157 // Whether we should generate multiple java files for messages.
MultipleJavaFiles(const FileDescriptor * descriptor,bool immutable)158 inline bool MultipleJavaFiles(const FileDescriptor* descriptor,
159                               bool immutable) {
160   return descriptor->options().java_multiple_files();
161 }
162 
163 // Returns true if `descriptor` will be written to its own .java file.
164 // `immutable` should be set to true if we're generating for the immutable API.
165 template <typename Descriptor>
IsOwnFile(const Descriptor * descriptor,bool immutable)166 bool IsOwnFile(const Descriptor* descriptor, bool immutable) {
167   return descriptor->containing_type() == NULL &&
168          MultipleJavaFiles(descriptor->file(), immutable);
169 }
170 
171 template <>
IsOwnFile(const ServiceDescriptor * descriptor,bool immutable)172 inline bool IsOwnFile(const ServiceDescriptor* descriptor, bool immutable) {
173   return MultipleJavaFiles(descriptor->file(), immutable);
174 }
175 
176 // If `descriptor` describes an object with its own .java file,
177 // returns the name (relative to that .java file) of the file that stores
178 // annotation data for that descriptor. `suffix` is usually empty, but may
179 // (e.g.) be "OrBuilder" for some generated interfaces.
180 template <typename Descriptor>
AnnotationFileName(const Descriptor * descriptor,const std::string & suffix)181 std::string AnnotationFileName(const Descriptor* descriptor,
182                                const std::string& suffix) {
183   return descriptor->name() + suffix + ".java.pb.meta";
184 }
185 
186 template <typename Descriptor>
187 void MaybePrintGeneratedAnnotation(Context* context, io::Printer* printer,
188                                    Descriptor* descriptor, bool immutable,
189                                    const std::string& suffix = "") {
190   if (context->options().annotate_code && IsOwnFile(descriptor, immutable)) {
191     PrintGeneratedAnnotation(printer, '$',
192                              AnnotationFileName(descriptor, suffix));
193   }
194 }
195 
196 // Get the unqualified name that should be used for a field's field
197 // number constant.
198 std::string FieldConstantName(const FieldDescriptor* field);
199 
200 // Returns the type of the FieldDescriptor.
201 // This does nothing interesting for the open source release, but is used for
202 // hacks that improve compatibility with version 1 protocol buffers at Google.
203 FieldDescriptor::Type GetType(const FieldDescriptor* field);
204 
205 enum JavaType {
206   JAVATYPE_INT,
207   JAVATYPE_LONG,
208   JAVATYPE_FLOAT,
209   JAVATYPE_DOUBLE,
210   JAVATYPE_BOOLEAN,
211   JAVATYPE_STRING,
212   JAVATYPE_BYTES,
213   JAVATYPE_ENUM,
214   JAVATYPE_MESSAGE
215 };
216 
217 JavaType GetJavaType(const FieldDescriptor* field);
218 
219 const char* PrimitiveTypeName(JavaType type);
220 
221 // Get the fully-qualified class name for a boxed primitive type, e.g.
222 // "java.lang.Integer" for JAVATYPE_INT.  Returns NULL for enum and message
223 // types.
224 const char* BoxedPrimitiveTypeName(JavaType type);
225 
226 // Get the name of the java enum constant representing this type. E.g.,
227 // "INT32" for FieldDescriptor::TYPE_INT32. The enum constant's full
228 // name is "com.google.protobuf.WireFormat.FieldType.INT32".
229 const char* FieldTypeName(const FieldDescriptor::Type field_type);
230 
231 class ClassNameResolver;
232 std::string DefaultValue(const FieldDescriptor* field, bool immutable,
233                          ClassNameResolver* name_resolver);
ImmutableDefaultValue(const FieldDescriptor * field,ClassNameResolver * name_resolver)234 inline std::string ImmutableDefaultValue(const FieldDescriptor* field,
235                                          ClassNameResolver* name_resolver) {
236   return DefaultValue(field, true, name_resolver);
237 }
238 bool IsDefaultValueJavaDefault(const FieldDescriptor* field);
239 bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field);
240 
241 // Does this message class have descriptor and reflection methods?
HasDescriptorMethods(const Descriptor * descriptor,bool enforce_lite)242 inline bool HasDescriptorMethods(const Descriptor* descriptor,
243                                  bool enforce_lite) {
244   return !enforce_lite;
245 }
HasDescriptorMethods(const EnumDescriptor * descriptor,bool enforce_lite)246 inline bool HasDescriptorMethods(const EnumDescriptor* descriptor,
247                                  bool enforce_lite) {
248   return !enforce_lite;
249 }
HasDescriptorMethods(const FileDescriptor * descriptor,bool enforce_lite)250 inline bool HasDescriptorMethods(const FileDescriptor* descriptor,
251                                  bool enforce_lite) {
252   return !enforce_lite;
253 }
254 
255 // Should we generate generic services for this file?
HasGenericServices(const FileDescriptor * file,bool enforce_lite)256 inline bool HasGenericServices(const FileDescriptor* file, bool enforce_lite) {
257   return file->service_count() > 0 &&
258          HasDescriptorMethods(file, enforce_lite) &&
259          file->options().java_generic_services();
260 }
261 
262 // Methods for shared bitfields.
263 
264 // Gets the name of the shared bitfield for the given index.
265 std::string GetBitFieldName(int index);
266 
267 // Gets the name of the shared bitfield for the given bit index.
268 // Effectively, GetBitFieldName(bitIndex / 32)
269 std::string GetBitFieldNameForBit(int bitIndex);
270 
271 // Generates the java code for the expression that returns the boolean value
272 // of the bit of the shared bitfields for the given bit index.
273 // Example: "((bitField1_ & 0x04) == 0x04)"
274 std::string GenerateGetBit(int bitIndex);
275 
276 // Generates the java code for the expression that sets the bit of the shared
277 // bitfields for the given bit index.
278 // Example: "bitField1_ = (bitField1_ | 0x04)"
279 std::string GenerateSetBit(int bitIndex);
280 
281 // Generates the java code for the expression that clears the bit of the shared
282 // bitfields for the given bit index.
283 // Example: "bitField1_ = (bitField1_ & ~0x04)"
284 std::string GenerateClearBit(int bitIndex);
285 
286 // Does the same as GenerateGetBit but operates on the bit field on a local
287 // variable. This is used by the builder to copy the value in the builder to
288 // the message.
289 // Example: "((from_bitField1_ & 0x04) == 0x04)"
290 std::string GenerateGetBitFromLocal(int bitIndex);
291 
292 // Does the same as GenerateSetBit but operates on the bit field on a local
293 // variable. This is used by the builder to copy the value in the builder to
294 // the message.
295 // Example: "to_bitField1_ = (to_bitField1_ | 0x04)"
296 std::string GenerateSetBitToLocal(int bitIndex);
297 
298 // Does the same as GenerateGetBit but operates on the bit field on a local
299 // variable. This is used by the parsing constructor to record if a repeated
300 // field is mutable.
301 // Example: "((mutable_bitField1_ & 0x04) == 0x04)"
302 std::string GenerateGetBitMutableLocal(int bitIndex);
303 
304 // Does the same as GenerateSetBit but operates on the bit field on a local
305 // variable. This is used by the parsing constructor to record if a repeated
306 // field is mutable.
307 // Example: "mutable_bitField1_ = (mutable_bitField1_ | 0x04)"
308 std::string GenerateSetBitMutableLocal(int bitIndex);
309 
310 // Returns whether the JavaType is a reference type.
311 bool IsReferenceType(JavaType type);
312 
313 // Returns the capitalized name for calling relative functions in
314 // CodedInputStream
315 const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable);
316 
317 // For encodings with fixed sizes, returns that size in bytes.  Otherwise
318 // returns -1.
319 int FixedSize(FieldDescriptor::Type type);
320 
321 // Comparators used to sort fields in MessageGenerator
322 struct FieldOrderingByNumber {
operatorFieldOrderingByNumber323   inline bool operator()(const FieldDescriptor* a,
324                          const FieldDescriptor* b) const {
325     return a->number() < b->number();
326   }
327 };
328 
329 struct ExtensionRangeOrdering {
operatorExtensionRangeOrdering330   bool operator()(const Descriptor::ExtensionRange* a,
331                   const Descriptor::ExtensionRange* b) const {
332     return a->start < b->start;
333   }
334 };
335 
336 // Sort the fields of the given Descriptor by number into a new[]'d array
337 // and return it. The caller should delete the returned array.
338 const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor);
339 
340 // Does this message class have any packed fields?
HasPackedFields(const Descriptor * descriptor)341 inline bool HasPackedFields(const Descriptor* descriptor) {
342   for (int i = 0; i < descriptor->field_count(); i++) {
343     if (descriptor->field(i)->is_packed()) {
344       return true;
345     }
346   }
347   return false;
348 }
349 
350 // Check a message type and its sub-message types recursively to see if any of
351 // them has a required field. Return true if a required field is found.
352 bool HasRequiredFields(const Descriptor* descriptor);
353 
354 // Whether a .proto file supports field presence test for non-message types.
SupportFieldPresence(const FileDescriptor * descriptor)355 inline bool SupportFieldPresence(const FileDescriptor* descriptor) {
356   return descriptor->syntax() != FileDescriptor::SYNTAX_PROTO3;
357 }
358 
359 // Whether generate classes expose public PARSER instances.
ExposePublicParser(const FileDescriptor * descriptor)360 inline bool ExposePublicParser(const FileDescriptor* descriptor) {
361   // TODO(liujisi): Mark the PARSER private in 3.1.x releases.
362   return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO2;
363 }
364 
365 // Whether unknown enum values are kept (i.e., not stored in UnknownFieldSet
366 // but in the message and can be queried using additional getters that return
367 // ints.
SupportUnknownEnumValue(const FileDescriptor * descriptor)368 inline bool SupportUnknownEnumValue(const FileDescriptor* descriptor) {
369   return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO3;
370 }
371 
372 // Check whether a mesasge has repeated fields.
373 bool HasRepeatedFields(const Descriptor* descriptor);
374 
IsMapEntry(const Descriptor * descriptor)375 inline bool IsMapEntry(const Descriptor* descriptor) {
376   return descriptor->options().map_entry();
377 }
378 
IsMapField(const FieldDescriptor * descriptor)379 inline bool IsMapField(const FieldDescriptor* descriptor) {
380   return descriptor->is_map();
381 }
382 
IsAnyMessage(const Descriptor * descriptor)383 inline bool IsAnyMessage(const Descriptor* descriptor) {
384   return descriptor->full_name() == "google.protobuf.Any";
385 }
386 
IsWrappersProtoFile(const FileDescriptor * descriptor)387 inline bool IsWrappersProtoFile(const FileDescriptor* descriptor) {
388   return descriptor->name() == "google/protobuf/wrappers.proto";
389 }
390 
CheckUtf8(const FieldDescriptor * descriptor)391 inline bool CheckUtf8(const FieldDescriptor* descriptor) {
392   return descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 ||
393          descriptor->file()->options().java_string_check_utf8();
394 }
395 
GeneratedCodeVersionSuffix()396 inline std::string GeneratedCodeVersionSuffix() {
397   return "V3";
398 }
399 
400 void WriteUInt32ToUtf16CharSequence(uint32 number, std::vector<uint16>* output);
401 
WriteIntToUtf16CharSequence(int value,std::vector<uint16> * output)402 inline void WriteIntToUtf16CharSequence(int value,
403                                         std::vector<uint16>* output) {
404   WriteUInt32ToUtf16CharSequence(static_cast<uint32>(value), output);
405 }
406 
407 // Escape a UTF-16 character so it can be embedded in a Java string literal.
408 void EscapeUtf16ToString(uint16 code, std::string* output);
409 
410 // Only the lowest two bytes of the return value are used. The lowest byte
411 // is the integer value of a j/c/g/protobuf/FieldType enum. For the other
412 // byte:
413 //    bit 0: whether the field is required.
414 //    bit 1: whether the field requires UTF-8 validation.
415 //    bit 2: whether the field needs isInitialized check.
416 //    bit 3: whether the field is a map field with proto2 enum value.
417 //    bits 4-7: unused
418 int GetExperimentalJavaFieldType(const FieldDescriptor* field);
419 
420 // To get the total number of entries need to be built for experimental runtime
421 // and the first field number that are not in the table part
422 std::pair<int, int> GetTableDrivenNumberOfEntriesAndLookUpStartFieldNumber(
423     const FieldDescriptor** fields, int count);
424 }  // namespace java
425 }  // namespace compiler
426 }  // namespace protobuf
427 }  // namespace google
428 
429 #endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
430