webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
java_helpers.h
Go to the documentation of this file.
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>
41 
42 namespace google {
43 namespace protobuf {
44 namespace compiler {
45 namespace java {
46 
47 // Commonly-used separator comments. Thick is a line of '=', thin is a line
48 // of '-'.
49 extern const char kThickSeparator[];
50 extern const char kThinSeparator[];
51 
52 // Converts a name to camel-case. If cap_first_letter is true, capitalize the
53 // first letter.
54 string UnderscoresToCamelCase(const string& name, bool cap_first_letter);
55 // Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes
56 // "fooBarBaz" or "FooBarBaz", respectively.
59 
60 // Similar, but for method names. (Typically, this merely has the effect
61 // of lower-casing the first letter of the name.)
62 string UnderscoresToCamelCase(const MethodDescriptor* method);
63 
64 // Get an identifier that uniquely identifies this type within the file.
65 // This is used to declare static variables related to this type at the
66 // outermost file scope.
68 
69 // Strips ".proto" or ".protodevel" from the end of a filename.
70 string StripProto(const string& filename);
71 
72 // Gets the unqualified class name for the file. For each .proto file, there
73 // will be one Java class containing all the immutable messages and another
74 // Java class containing all the mutable messages.
75 // TODO(xiaofeng): remove the default value after updating client code.
76 string FileClassName(const FileDescriptor* file, bool immutable = true);
77 
78 // Returns the file's Java package name.
79 string FileJavaPackage(const FileDescriptor* file, bool immutable = true);
80 
81 // Returns output directory for the given package name.
82 string JavaPackageToDir(string package_name);
83 
84 // Converts the given fully-qualified name in the proto namespace to its
85 // fully-qualified name in the Java namespace, given that it is in the given
86 // file.
87 // TODO(xiaofeng): this method is deprecated and should be removed in the
88 // future.
89 string ToJavaName(const string& full_name,
90  const FileDescriptor* file);
91 
92 // TODO(xiaofeng): the following methods are kept for they are exposed
93 // publicly in //google/protobuf/compiler/java/names.h. They return
94 // immutable names only and should be removed after mutable API is
95 // integrated into google3.
96 string ClassName(const Descriptor* descriptor);
97 string ClassName(const EnumDescriptor* descriptor);
98 string ClassName(const ServiceDescriptor* descriptor);
99 string ClassName(const FileDescriptor* descriptor);
100 
101 // Comma-separate list of option-specified interfaces implemented by the
102 // Message, to follow the "implements" declaration of the Message definition.
104 // Comma-separate list of option-specified interfaces implemented by the
105 // MutableMessage, to follow the "implements" declaration of the MutableMessage
106 // definition.
108 // Comma-separate list of option-specified interfaces implemented by the
109 // Builder, to follow the "implements" declaration of the Builder definition.
111 // Comma-separate list of option-specified interfaces extended by the
112 // MessageOrBuilder, to follow the "extends" declaration of the
113 // MessageOrBuilder definition.
115 
116 // Get the unqualified Java class name for mutable messages. i.e. without
117 // package or outer classnames.
119  return descriptor->name();
120 }
121 
122 
123 // Whether we should generate multiple java files for messages.
124 inline bool MultipleJavaFiles(
125  const FileDescriptor* descriptor, bool immutable) {
126  return descriptor->options().java_multiple_files();
127 }
128 
129 // Get the unqualified name that should be used for a field's field
130 // number constant.
132 
133 // Returns the type of the FieldDescriptor.
134 // 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.
137 
138 enum JavaType {
148 };
149 
150 JavaType GetJavaType(const FieldDescriptor* field);
151 
152 const char* PrimitiveTypeName(JavaType type);
153 
154 // Get the fully-qualified class name for a boxed primitive type, e.g.
155 // "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message
156 // types.
158 
159 // Get the name of the java enum constant representing this type. E.g.,
160 // "INT32" for FieldDescriptor::TYPE_INT32. The enum constant's full
161 // name is "com.google.protobuf.WireFormat.FieldType.INT32".
163 
164 class ClassNameResolver;
165 string DefaultValue(const FieldDescriptor* field, bool immutable,
166  ClassNameResolver* name_resolver);
167 inline string ImmutableDefaultValue(const FieldDescriptor* field,
168  ClassNameResolver* name_resolver) {
169  return DefaultValue(field, true, name_resolver);
170 }
171 bool IsDefaultValueJavaDefault(const FieldDescriptor* field);
172 
173 // Does this message have specialized equals() and hashCode() methods?
175  return descriptor->file()->options().java_generate_equals_and_hash();
176 }
177 
178 // Does this message class have descriptor and reflection methods?
180  bool enforce_lite) {
181  return !enforce_lite &&
182  descriptor->file()->options().optimize_for() !=
184 }
186  bool enforce_lite) {
187  return !enforce_lite &&
188  descriptor->file()->options().optimize_for() !=
190 }
192  bool enforce_lite) {
193  return !enforce_lite &&
195 }
196 
197 // Should we generate generic services for this file?
198 inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) {
199  return file->service_count() > 0 &&
200  HasDescriptorMethods(file, enforce_lite) &&
201  file->options().java_generic_services();
202 }
203 
204 inline bool IsLazy(const FieldDescriptor* descriptor, bool enforce_lite) {
205  // Currently, the proto-lite version suports lazy field.
206  // TODO(niwasaki): Support lazy fields also for other proto runtimes.
207  if (HasDescriptorMethods(descriptor->file(), enforce_lite)) {
208  return false;
209  }
210  return descriptor->options().lazy();
211 }
212 
213 // Methods for shared bitfields.
214 
215 // Gets the name of the shared bitfield for the given index.
216 string GetBitFieldName(int index);
217 
218 // Gets the name of the shared bitfield for the given bit index.
219 // Effectively, GetBitFieldName(bitIndex / 32)
220 string GetBitFieldNameForBit(int bitIndex);
221 
222 // Generates the java code for the expression that returns the boolean value
223 // of the bit of the shared bitfields for the given bit index.
224 // Example: "((bitField1_ & 0x04) == 0x04)"
225 string GenerateGetBit(int bitIndex);
226 
227 // Generates the java code for the expression that sets the bit of the shared
228 // bitfields for the given bit index.
229 // Example: "bitField1_ = (bitField1_ | 0x04)"
230 string GenerateSetBit(int bitIndex);
231 
232 // Generates the java code for the expression that clears the bit of the shared
233 // bitfields for the given bit index.
234 // Example: "bitField1_ = (bitField1_ & ~0x04)"
235 string GenerateClearBit(int bitIndex);
236 
237 // Does the same as GenerateGetBit but operates on the bit field on a local
238 // variable. This is used by the builder to copy the value in the builder to
239 // the message.
240 // Example: "((from_bitField1_ & 0x04) == 0x04)"
241 string GenerateGetBitFromLocal(int bitIndex);
242 
243 // Does the same as GenerateSetBit but operates on the bit field on a local
244 // variable. This is used by the builder to copy the value in the builder to
245 // the message.
246 // Example: "to_bitField1_ = (to_bitField1_ | 0x04)"
247 string GenerateSetBitToLocal(int bitIndex);
248 
249 // Does the same as GenerateGetBit but operates on the bit field on a local
250 // variable. This is used by the parsing constructor to record if a repeated
251 // field is mutable.
252 // Example: "((mutable_bitField1_ & 0x04) == 0x04)"
253 string GenerateGetBitMutableLocal(int bitIndex);
254 
255 // Does the same as GenerateSetBit but operates on the bit field on a local
256 // variable. This is used by the parsing constructor to record if a repeated
257 // field is mutable.
258 // Example: "mutable_bitField1_ = (mutable_bitField1_ | 0x04)"
259 string GenerateSetBitMutableLocal(int bitIndex);
260 
261 // Returns whether the JavaType is a reference type.
263 
264 // Returns the capitalized name for calling relative functions in
265 // CodedInputStream
266 const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable);
267 
268 // For encodings with fixed sizes, returns that size in bytes. Otherwise
269 // returns -1.
271 
272 // Comparators used to sort fields in MessageGenerator
274  inline bool operator()(const FieldDescriptor* a,
275  const FieldDescriptor* b) const {
276  return a->number() < b->number();
277  }
278 };
279 
281  bool operator()(const Descriptor::ExtensionRange* a,
282  const Descriptor::ExtensionRange* b) const {
283  return a->start < b->start;
284  }
285 };
286 
287 // Sort the fields of the given Descriptor by number into a new[]'d array
288 // and return it. The caller should delete the returned array.
290 
291 // Does this message class have any packed fields?
292 inline bool HasPackedFields(const Descriptor* descriptor) {
293  for (int i = 0; i < descriptor->field_count(); i++) {
294  if (descriptor->field(i)->is_packed()) {
295  return true;
296  }
297  }
298  return false;
299 }
300 
301 // Check a message type and its sub-message types recursively to see if any of
302 // them has a required field. Return true if a required field is found.
303 bool HasRequiredFields(const Descriptor* descriptor);
304 
305 // Whether a .proto file supports field presence test for non-message types.
306 inline bool SupportFieldPresence(const FileDescriptor* descriptor) {
307  return descriptor->syntax() != FileDescriptor::SYNTAX_PROTO3;
308 }
309 
310 // Whether generate classes expose public PARSER instances.
311 inline bool ExposePublicParser(const FileDescriptor* descriptor) {
312  // TODO(liujisi): Mark the PARSER private in 3.1.x releases.
313  return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO2;
314 }
315 
316 // Whether unknown enum values are kept (i.e., not stored in UnknownFieldSet
317 // but in the message and can be queried using additional getters that return
318 // ints.
319 inline bool SupportUnknownEnumValue(const FileDescriptor* descriptor) {
320  return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO3;
321 }
322 
323 // Check whether a mesasge has repeated fields.
324 bool HasRepeatedFields(const Descriptor* descriptor);
325 
326 inline bool IsMapEntry(const Descriptor* descriptor) {
327  return descriptor->options().map_entry();
328 }
329 
330 inline bool IsMapField(const FieldDescriptor* descriptor) {
331  return descriptor->is_map();
332 }
333 
334 inline bool PreserveUnknownFields(const Descriptor* descriptor) {
335  return descriptor->file()->syntax() != FileDescriptor::SYNTAX_PROTO3;
336 }
337 
338 inline bool IsAnyMessage(const Descriptor* descriptor) {
339  return descriptor->full_name() == "google.protobuf.Any";
340 }
341 
342 inline bool CheckUtf8(const FieldDescriptor* descriptor) {
343  return descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 ||
344  descriptor->file()->options().java_string_check_utf8();
345 }
346 
347 } // namespace java
348 } // namespace compiler
349 } // namespace protobuf
350 
351 } // namespace google
352 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
Definition: upb.c:6604
Definition: descriptor.h:801
string DefaultValue(const FieldDescriptor *field, bool immutable, ClassNameResolver *name_resolver)
Definition: java_helpers.cc:387
const char * GetCapitalizedType(const FieldDescriptor *field, bool immutable)
Definition: java_helpers.cc:622
bool SupportUnknownEnumValue(const FileDescriptor *descriptor)
Definition: java_helpers.h:319
string UniqueFileScopeIdentifier(const Descriptor *descriptor)
Definition: java_helpers.cc:151
string GenerateClearBit(int bitIndex)
Definition: java_helpers.cc:577
const FieldDescriptor * field
Definition: parser_unittest.cc:2279
bool IsMapEntry(const Descriptor *descriptor)
Definition: java_helpers.h:326
string UnderscoresToCapitalizedCamelCase(const FieldDescriptor *field)
Definition: java_helpers.cc:143
const MessageOptions & options() const
bool HasRequiredFields(const Descriptor *type, hash_set< const Descriptor *> *already_seen)
Definition: java_helpers.cc:703
const FileDescriptor * file() const
const FieldOptions & options() const
const Descriptor * descriptor
Definition: descriptor.cc:271
Definition: method.py:1
const char kThinSeparator[]
Definition: java_helpers.cc:57
string GenerateGetBitFromLocal(int bitIndex)
Definition: java_helpers.cc:586
const FileOptions & options() const
Definition: descriptor.h:1156
int FixedSize(FieldDescriptor::Type type)
Definition: java_helpers.cc:655
string GenerateGetBit(int bitIndex)
Definition: java_helpers.cc:569
Definition: descriptor.h:172
::google::protobuf::FileOptions_OptimizeMode optimize_for() const
Definition: descriptor.pb.h:5955
string ImmutableDefaultValue(const FieldDescriptor *field, ClassNameResolver *name_resolver)
Definition: java_helpers.h:167
string ExtraBuilderInterfaces(const Descriptor *descriptor)
Definition: java_helpers.cc:241
JavaType
Definition: java_helpers.h:138
bool PreserveUnknownFields(const Descriptor *descriptor)
Definition: java_helpers.h:334
USVString filename
Definition: ErrorEvent.idl:46
bool HasPackedFields(const Descriptor *descriptor)
Definition: java_helpers.h:292
string FieldConstantName(const FieldDescriptor *field)
Definition: java_helpers.cc:253
static const OptimizeMode LITE_RUNTIME
Definition: descriptor.pb.h:1896
bool HasEqualsAndHashCode(const Descriptor *descriptor)
Definition: java_helpers.h:174
string ShortMutableJavaClassName(const Descriptor *descriptor)
Definition: java_helpers.h:118
string GenerateGetBitMutableLocal(int bitIndex)
Definition: java_helpers.cc:594
Definition: descriptor.h:439
bool IsDefaultValueJavaDefault(const FieldDescriptor *field)
Definition: java_helpers.cc:466
Definition: java_name_resolver.h:53
string StripProto(const string &filename)
Definition: java_helpers.cc:155
string GetBitFieldName(int index)
Definition: java_helpers.cc:536
VoEFile * file
Definition: voe_cmd_test.cc:59
Definition: protobuf.h:85
bool IsLazy(const FieldDescriptor *descriptor, bool enforce_lite)
Definition: java_helpers.h:204
Type
Definition: descriptor.h:443
GLuint index
Definition: gl2.h:383
bool HasDescriptorMethods(const Descriptor *descriptor, bool enforce_lite)
Definition: java_helpers.h:179
bool operator()(const Descriptor::ExtensionRange *a, const Descriptor::ExtensionRange *b) const
Definition: java_helpers.h:281
const FieldDescriptor * field(int index) const
string FileClassName(const FileDescriptor *file, bool immutable)
Definition: java_helpers.cc:163
EGLImageKHR EGLint * name
Definition: eglext.h:851
string ClassName(const Descriptor *descriptor)
Definition: java_helpers.cc:214
string ToJavaName(const string &full_name, const FileDescriptor *file)
Definition: java_helpers.cc:193
const char * BoxedPrimitiveTypeName(JavaType type)
Definition: java_helpers.cc:329
Definition: __init__.py:1
string GetBitFieldNameForBit(int bitIndex)
Definition: java_helpers.cc:543
GLboolean GLboolean GLboolean GLboolean a
Definition: gl2ext.h:306
FieldDescriptor::Type GetType(const FieldDescriptor *field)
Definition: java_helpers.cc:259
const char kThickSeparator[]
Definition: java_helpers.cc:55
bool MultipleJavaFiles(const FileDescriptor *descriptor, bool immutable)
Definition: java_helpers.h:124
bool HasGenericServices(const FileDescriptor *file, bool enforce_lite)
Definition: java_helpers.h:198
WireFormatLite::FieldType field_type(FieldType type)
Definition: extension_set_heavy.cc:133
bool IsReferenceType(JavaType type)
Definition: java_helpers.cc:602
bool IsMapField(const FieldDescriptor *descriptor)
Definition: java_helpers.h:330
EGLenum type
Definition: eglext.h:63
bool is_packed() const
Definition: descriptor.cc:2623
string GenerateSetBitMutableLocal(int bitIndex)
Definition: java_helpers.cc:598
bool java_generate_equals_and_hash() const
Definition: descriptor.pb.h:5907
const char * PrimitiveTypeName(JavaType type)
Definition: java_helpers.cc:309
const FieldDescriptor ** SortFieldsByNumber(const Descriptor *descriptor)
Definition: java_helpers.cc:687
JavaType GetJavaType(const FieldDescriptor *field)
Definition: java_helpers.cc:263
for i
Definition: complexityMeasures.m:24
string ExtraMutableMessageInterfaces(const Descriptor *descriptor)
string GenerateSetBit(int bitIndex)
Definition: java_helpers.cc:573
Syntax syntax() const
Definition: descriptor.h:1899
bool java_generic_services() const
Definition: descriptor.pb.h:6058
const FileDescriptor * file() const
string ExtraMessageInterfaces(const Descriptor *descriptor)
Definition: java_helpers.cc:234
bool java_string_check_utf8() const
Definition: descriptor.pb.h:5931
bool map_entry() const
Definition: descriptor.pb.h:6368
string JavaPackageToDir(string package_name)
Definition: java_helpers.cc:184
GLboolean GLboolean GLboolean b
Definition: gl2ext.h:306
const string & name() const
Definition: gflags_completions.h:115
string FileJavaPackage(const FileDescriptor *file, bool immutable)
Definition: java_helpers.cc:168
bool SupportFieldPresence(const FileDescriptor *descriptor)
Definition: java_helpers.h:306
const char * FieldTypeName(FieldDescriptor::Type field_type)
Definition: java_helpers.cc:349
bool CheckUtf8(const FieldDescriptor *descriptor)
Definition: java_helpers.h:342
string GenerateSetBitToLocal(int bitIndex)
Definition: java_helpers.cc:590
bool java_multiple_files() const
Definition: descriptor.pb.h:5883
string ExtraMessageOrBuilderInterfaces(const Descriptor *descriptor)
Definition: java_helpers.cc:247
const string & full_name() const
bool is_map() const
Definition: descriptor.cc:1677
bool lazy() const
Definition: descriptor.pb.h:6500
bool HasRepeatedFields(const Descriptor *descriptor)
Definition: java_helpers.cc:744
bool ExposePublicParser(const FileDescriptor *descriptor)
Definition: java_helpers.h:311
Definition: protobuf.h:70
bool IsAnyMessage(const Descriptor *descriptor)
Definition: java_helpers.h:338
Definition: protobuf.h:95
const FileDescriptor * file() const
bool operator()(const FieldDescriptor *a, const FieldDescriptor *b) const
Definition: java_helpers.h:274
string UnderscoresToCamelCase(const string &input, bool cap_next_letter)
Definition: java_helpers.cc:104