blob: 4ae07f150fa0ae6dfac7768f57ab2ce02304ba62 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
temporal40ee5512008-07-10 02:12:20 +00003// http://code.google.com/p/protobuf/
4//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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>
kenton@google.com80b1d622009-07-29 01:13:20 +000039#include <google/protobuf/descriptor.pb.h>
temporal40ee5512008-07-10 02:12:20 +000040#include <google/protobuf/descriptor.h>
41
42namespace google {
43namespace protobuf {
44namespace compiler {
45namespace java {
46
47// Commonly-used separator comments. Thick is a line of '=', thin is a line
48// of '-'.
49extern const char kThickSeparator[];
50extern const char kThinSeparator[];
51
52// Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes
53// "fooBarBaz" or "FooBarBaz", respectively.
54string UnderscoresToCamelCase(const FieldDescriptor* field);
55string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field);
56
57// Similar, but for method names. (Typically, this merely has the effect
58// of lower-casing the first letter of the name.)
59string UnderscoresToCamelCase(const MethodDescriptor* method);
60
61// Strips ".proto" or ".protodevel" from the end of a filename.
62string StripProto(const string& filename);
63
64// Gets the unqualified class name for the file. Each .proto file becomes a
65// single Java class, with all its contents nested in that class.
66string FileClassName(const FileDescriptor* file);
67
68// Returns the file's Java package name.
69string FileJavaPackage(const FileDescriptor* file);
70
liujisi@google.com33165fe2010-11-02 13:14:58 +000071// Returns output directory for the given package name.
72string JavaPackageToDir(string package_name);
73
temporal40ee5512008-07-10 02:12:20 +000074// Converts the given fully-qualified name in the proto namespace to its
75// fully-qualified name in the Java namespace, given that it is in the given
76// file.
77string ToJavaName(const string& full_name, const FileDescriptor* file);
78
79// These return the fully-qualified class name corresponding to the given
80// descriptor.
81inline string ClassName(const Descriptor* descriptor) {
82 return ToJavaName(descriptor->full_name(), descriptor->file());
83}
84inline string ClassName(const EnumDescriptor* descriptor) {
85 return ToJavaName(descriptor->full_name(), descriptor->file());
86}
87inline string ClassName(const ServiceDescriptor* descriptor) {
88 return ToJavaName(descriptor->full_name(), descriptor->file());
89}
kenton@google.com24bf56f2008-09-24 20:31:01 +000090inline string ExtensionIdentifierName(const FieldDescriptor* descriptor) {
91 return ToJavaName(descriptor->full_name(), descriptor->file());
92}
temporal40ee5512008-07-10 02:12:20 +000093string ClassName(const FileDescriptor* descriptor);
94
kenton@google.comcfa2d8a2009-04-18 00:02:12 +000095// Get the unqualified name that should be used for a field's field
96// number constant.
97string FieldConstantName(const FieldDescriptor *field);
98
kenton@google.comfccb1462009-12-18 02:11:36 +000099// Returns the type of the FieldDescriptor.
100// This does nothing interesting for the open source release, but is used for
101// hacks that improve compatability with version 1 protocol buffers at Google.
102FieldDescriptor::Type GetType(const FieldDescriptor* field);
103
temporal40ee5512008-07-10 02:12:20 +0000104enum JavaType {
105 JAVATYPE_INT,
106 JAVATYPE_LONG,
107 JAVATYPE_FLOAT,
108 JAVATYPE_DOUBLE,
109 JAVATYPE_BOOLEAN,
110 JAVATYPE_STRING,
111 JAVATYPE_BYTES,
112 JAVATYPE_ENUM,
113 JAVATYPE_MESSAGE
114};
115
kenton@google.comfccb1462009-12-18 02:11:36 +0000116JavaType GetJavaType(const FieldDescriptor* field);
temporal40ee5512008-07-10 02:12:20 +0000117
118// Get the fully-qualified class name for a boxed primitive type, e.g.
119// "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message
120// types.
121const char* BoxedPrimitiveTypeName(JavaType type);
122
kenton@google.com80b1d622009-07-29 01:13:20 +0000123string DefaultValue(const FieldDescriptor* field);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000124bool IsDefaultValueJavaDefault(const FieldDescriptor* field);
kenton@google.com80b1d622009-07-29 01:13:20 +0000125
126// Does this message class keep track of unknown fields?
127inline bool HasUnknownFields(const Descriptor* descriptor) {
128 return descriptor->file()->options().optimize_for() !=
129 FileOptions::LITE_RUNTIME;
130}
131
132// Does this message class have generated parsing, serialization, and other
133// standard methods for which reflection-based fallback implementations exist?
134inline bool HasGeneratedMethods(const Descriptor* descriptor) {
135 return descriptor->file()->options().optimize_for() !=
136 FileOptions::CODE_SIZE;
137}
138
liujisi@google.com33165fe2010-11-02 13:14:58 +0000139// Does this message have specialized equals() and hashCode() methods?
140inline bool HasEqualsAndHashCode(const Descriptor* descriptor) {
141 return descriptor->file()->options().java_generate_equals_and_hash();
142}
143
kenton@google.com80b1d622009-07-29 01:13:20 +0000144// Does this message class have descriptor and reflection methods?
145inline bool HasDescriptorMethods(const Descriptor* descriptor) {
146 return descriptor->file()->options().optimize_for() !=
147 FileOptions::LITE_RUNTIME;
148}
149inline bool HasDescriptorMethods(const EnumDescriptor* descriptor) {
150 return descriptor->file()->options().optimize_for() !=
151 FileOptions::LITE_RUNTIME;
152}
153inline bool HasDescriptorMethods(const FileDescriptor* descriptor) {
154 return descriptor->options().optimize_for() !=
155 FileOptions::LITE_RUNTIME;
156}
157
liujisi@google.com33165fe2010-11-02 13:14:58 +0000158inline bool HasNestedBuilders(const Descriptor* descriptor) {
159 // The proto-lite version doesn't support nested builders.
160 return descriptor->file()->options().optimize_for() !=
161 FileOptions::LITE_RUNTIME;
162}
163
kenton@google.comfccb1462009-12-18 02:11:36 +0000164// Should we generate generic services for this file?
165inline bool HasGenericServices(const FileDescriptor *file) {
166 return file->service_count() > 0 &&
167 file->options().optimize_for() != FileOptions::LITE_RUNTIME &&
168 file->options().java_generic_services();
169}
170
liujisi@google.com33165fe2010-11-02 13:14:58 +0000171
172// Methods for shared bitfields.
173
174// Gets the name of the shared bitfield for the given index.
175string GetBitFieldName(int index);
176
177// Gets the name of the shared bitfield for the given bit index.
178// Effectively, GetBitFieldName(bitIndex / 32)
179string GetBitFieldNameForBit(int bitIndex);
180
181// Generates the java code for the expression that returns the boolean value
182// of the bit of the shared bitfields for the given bit index.
183// Example: "((bitField1_ & 0x04) == 0x04)"
184string GenerateGetBit(int bitIndex);
185
186// Generates the java code for the expression that sets the bit of the shared
187// bitfields for the given bit index.
188// Example: "bitField1_ = (bitField1_ | 0x04)"
189string GenerateSetBit(int bitIndex);
190
191// Generates the java code for the expression that clears the bit of the shared
192// bitfields for the given bit index.
193// Example: "bitField1_ = (bitField1_ & ~0x04)"
194string GenerateClearBit(int bitIndex);
195
196// Does the same as GenerateGetBit but operates on the bit field on a local
197// variable. This is used by the builder to copy the value in the builder to
198// the message.
199// Example: "((from_bitField1_ & 0x04) == 0x04)"
200string GenerateGetBitFromLocal(int bitIndex);
201
202// Does the same as GenerateSetBit but operates on the bit field on a local
203// variable. This is used by the builder to copy the value in the builder to
204// the message.
205// Example: "to_bitField1_ = (to_bitField1_ | 0x04)"
206string GenerateSetBitToLocal(int bitIndex);
207
temporal40ee5512008-07-10 02:12:20 +0000208} // namespace java
209} // namespace compiler
210} // namespace protobuf
211
212} // namespace google
213#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__