javanano_helpers.h revision e74fe623e115237968a3de1143d7cdb4df710858
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#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
36#define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
37
38#include <string>
39#include <google/protobuf/compiler/javanano/javanano_params.h>
40#include <google/protobuf/descriptor.pb.h>
41#include <google/protobuf/descriptor.h>
42
43namespace google {
44namespace protobuf {
45namespace compiler {
46namespace javanano {
47
48// Commonly-used separator comments.  Thick is a line of '=', thin is a line
49// of '-'.
50extern const char kThickSeparator[];
51extern const char kThinSeparator[];
52
53// Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes
54// "fooBarBaz" or "FooBarBaz", respectively.
55string UnderscoresToCamelCase(const FieldDescriptor* field);
56string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field);
57
58// Appends an "_" to the end of a field where the name is a reserved java
59// keyword.  For example int32 public = 1 will generate int public_.
60string RenameJavaKeywords(const string& input);
61
62// Similar, but for method names.  (Typically, this merely has the effect
63// of lower-casing the first letter of the name.)
64string UnderscoresToCamelCase(const MethodDescriptor* method);
65
66// Strips ".proto" or ".protodevel" from the end of a filename.
67string StripProto(const string& filename);
68
69// Gets the unqualified class name for the file.  Each .proto file becomes a
70// single Java class, with all its contents nested in that class.
71string FileClassName(const Params& params, const FileDescriptor* file);
72
73// Returns the file's Java package name.
74string FileJavaPackage(const Params& params, const FileDescriptor* file);
75
76// Returns whether the Java outer class is needed, i.e. whether the option
77// java_multiple_files is false, or the proto file contains any file-scope
78// enums/extensions.
79bool IsOuterClassNeeded(const Params& params, const FileDescriptor* file);
80
81// Converts the given simple name of a proto entity to its fully-qualified name
82// in the Java namespace, given that it is in the given file enclosed in the
83// given parent message (or NULL for file-scope entities). Whether the file's
84// outer class name should be included in the return value depends on factors
85// inferrable from the given arguments, including is_class which indicates
86// whether the entity translates to a Java class.
87string ToJavaName(const Params& params, const string& name, bool is_class,
88    const Descriptor* parent, const FileDescriptor* file);
89
90// These return the fully-qualified class name corresponding to the given
91// descriptor.
92inline string ClassName(const Params& params, const Descriptor* descriptor) {
93  return ToJavaName(params, descriptor->name(), true,
94                    descriptor->containing_type(), descriptor->file());
95}
96string ClassName(const Params& params, const EnumDescriptor* descriptor);
97inline string ClassName(const Params& params,
98    const ServiceDescriptor* descriptor) {
99  return ToJavaName(params, descriptor->name(), true, NULL, descriptor->file());
100}
101inline string ExtensionIdentifierName(const Params& params,
102    const FieldDescriptor* descriptor) {
103  return ToJavaName(params, descriptor->name(), false,
104                    descriptor->extension_scope(), descriptor->file());
105}
106string ClassName(const Params& params, const FileDescriptor* descriptor);
107
108// Get the unqualified name that should be used for a field's field
109// number constant.
110string FieldConstantName(const FieldDescriptor *field);
111
112string FieldDefaultConstantName(const FieldDescriptor *field);
113
114enum JavaType {
115  JAVATYPE_INT,
116  JAVATYPE_LONG,
117  JAVATYPE_FLOAT,
118  JAVATYPE_DOUBLE,
119  JAVATYPE_BOOLEAN,
120  JAVATYPE_STRING,
121  JAVATYPE_BYTES,
122  JAVATYPE_ENUM,
123  JAVATYPE_MESSAGE
124};
125
126JavaType GetJavaType(FieldDescriptor::Type field_type);
127
128inline JavaType GetJavaType(const FieldDescriptor* field) {
129  return GetJavaType(field->type());
130}
131
132// Get the fully-qualified class name for a boxed primitive type, e.g.
133// "java.lang.Integer" for JAVATYPE_INT.  Returns NULL for enum and message
134// types.
135const char* BoxedPrimitiveTypeName(JavaType type);
136
137string EmptyArrayName(const Params& params, const FieldDescriptor* field);
138
139string DefaultValue(const Params& params, const FieldDescriptor* field);
140
141
142// Methods for shared bitfields.
143
144// Gets the name of the shared bitfield for the given index.
145string GetBitFieldName(int index);
146
147// Gets the name of the shared bitfield for the given bit index.
148// Effectively, GetBitFieldName(bit_index / 32)
149string GetBitFieldNameForBit(int bit_index);
150
151// Generates the java code for the expression that returns the boolean value
152// of the bit of the shared bitfields for the given bit index.
153// Example: "((bitField1_ & 0x04) == 0x04)"
154string GenerateGetBit(int bit_index);
155
156// Generates the java code for the expression that sets the bit of the shared
157// bitfields for the given bit index.
158// Example: "bitField1_ = (bitField1_ | 0x04)"
159string GenerateSetBit(int bit_index);
160
161// Generates the java code for the expression that clears the bit of the shared
162// bitfields for the given bit index.
163// Example: "bitField1_ = (bitField1_ & ~0x04)"
164string GenerateClearBit(int bit_index);
165
166// Sets the 'get_*', 'set_*' and 'clear_*' variables, where * is the given bit
167// field name, to the appropriate Java expressions for the given bit index.
168void SetBitOperationVariables(const string name,
169    int bitIndex, map<string, string>* variables);
170
171}  // namespace javanano
172}  // namespace compiler
173}  // namespace protobuf
174
175}  // namespace google
176#endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
177