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