1e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Protocol Buffers - Google's data interchange format
2e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Copyright 2008 Google Inc.  All rights reserved.
3e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// http://code.google.com/p/protobuf/
4e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville//
5e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Redistribution and use in source and binary forms, with or without
6e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// modification, are permitted provided that the following conditions are
7e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// met:
8e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville//
9e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville//     * Redistributions of source code must retain the above copyright
10e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// notice, this list of conditions and the following disclaimer.
11e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville//     * Redistributions in binary form must reproduce the above
12e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// copyright notice, this list of conditions and the following disclaimer
13e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// in the documentation and/or other materials provided with the
14e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// distribution.
15e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville//     * Neither the name of Google Inc. nor the names of its
16e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// contributors may be used to endorse or promote products derived from
17e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// this software without specific prior written permission.
18e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville//
19e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
31e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Author: kenton@google.com (Kenton Varda)
32e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville//  Based on original Protocol Buffers design by
33e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville//  Sanjay Ghemawat, Jeff Dean, and others.
34e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
35e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
36e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville#define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
37e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
38e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville#include <string>
39e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville#include <google/protobuf/compiler/javamicro/javamicro_params.h>
40e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville#include <google/protobuf/descriptor.pb.h>
41e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville#include <google/protobuf/descriptor.h>
42e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
43e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillenamespace google {
44e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillenamespace protobuf {
45e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillenamespace compiler {
46e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillenamespace javamicro {
47e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
48e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Commonly-used separator comments.  Thick is a line of '=', thin is a line
49e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// of '-'.
50e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savilleextern const char kThickSeparator[];
51e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savilleextern const char kThinSeparator[];
52e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
53e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes
54e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// "fooBarBaz" or "FooBarBaz", respectively.
55e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring UnderscoresToCamelCase(const FieldDescriptor* field);
56e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field);
57e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
58e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Similar, but for method names.  (Typically, this merely has the effect
59e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// of lower-casing the first letter of the name.)
60e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring UnderscoresToCamelCase(const MethodDescriptor* method);
61e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
62e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Strips ".proto" or ".protodevel" from the end of a filename.
63e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring StripProto(const string& filename);
64e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
65e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Gets the unqualified class name for the file.  Each .proto file becomes a
66e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// single Java class, with all its contents nested in that class.
67e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring FileClassName(const Params& params, const FileDescriptor* file);
68e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
69e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Returns the file's Java package name.
70e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring FileJavaPackage(const Params& params, const FileDescriptor* file);
71e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
72624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// Returns whether the Java outer class is needed, i.e. whether the option
73624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// java_multiple_files is false, or the proto file contains any file-scope
74624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// enums/extensions.
75624c448fbef20a1a2fad2289f622b468c25763d1Max Caibool IsOuterClassNeeded(const Params& params, const FileDescriptor* file);
76624c448fbef20a1a2fad2289f622b468c25763d1Max Cai
77624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// Converts the given simple name of a proto entity to its fully-qualified name
78624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// in the Java namespace, given that it is in the given file enclosed in the
79624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// given parent message (or NULL for file-scope entities). Whether the file's
80624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// outer class name should be included in the return value depends on factors
81624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// inferrable from the given arguments, including is_class which indicates
82624c448fbef20a1a2fad2289f622b468c25763d1Max Cai// whether the entity translates to a Java class.
83624c448fbef20a1a2fad2289f622b468c25763d1Max Caistring ToJavaName(const Params& params, const string& name, bool is_class,
84624c448fbef20a1a2fad2289f622b468c25763d1Max Cai    const Descriptor* parent, const FileDescriptor* file);
85e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
86e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// These return the fully-qualified class name corresponding to the given
87e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// descriptor.
88e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savilleinline string ClassName(const Params& params, const Descriptor* descriptor) {
89624c448fbef20a1a2fad2289f622b468c25763d1Max Cai  return ToJavaName(params, descriptor->name(), true,
90624c448fbef20a1a2fad2289f622b468c25763d1Max Cai                    descriptor->containing_type(), descriptor->file());
91e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville}
92e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring ClassName(const Params& params, const EnumDescriptor* descriptor);
93e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savilleinline string ClassName(const Params& params,
94e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville    const ServiceDescriptor* descriptor) {
95624c448fbef20a1a2fad2289f622b468c25763d1Max Cai  return ToJavaName(params, descriptor->name(), true, NULL, descriptor->file());
96e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville}
97e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savilleinline string ExtensionIdentifierName(const Params& params,
98e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville    const FieldDescriptor* descriptor) {
99624c448fbef20a1a2fad2289f622b468c25763d1Max Cai  return ToJavaName(params, descriptor->name(), false,
100624c448fbef20a1a2fad2289f622b468c25763d1Max Cai                    descriptor->extension_scope(), descriptor->file());
101e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville}
102e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring ClassName(const Params& params, const FileDescriptor* descriptor);
103e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
104e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Get the unqualified name that should be used for a field's field
105e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// number constant.
106e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring FieldConstantName(const FieldDescriptor *field);
107e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
108e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savilleenum JavaType {
109e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_INT,
110e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_LONG,
111e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_FLOAT,
112e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_DOUBLE,
113e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_BOOLEAN,
114e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_STRING,
115e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_BYTES,
116e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_ENUM,
117e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  JAVATYPE_MESSAGE
118e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville};
119e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
120e2d542951c059563a3b7f74c257dac4f222d9dc5Wink SavilleJavaType GetJavaType(FieldDescriptor::Type field_type);
121e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
122e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savilleinline JavaType GetJavaType(const FieldDescriptor* field) {
123e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville  return GetJavaType(field->type());
124e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville}
125e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
126e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// Get the fully-qualified class name for a boxed primitive type, e.g.
127e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// "java.lang.Integer" for JAVATYPE_INT.  Returns NULL for enum and message
128e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville// types.
129e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savilleconst char* BoxedPrimitiveTypeName(JavaType type);
130e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
131e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Savillestring DefaultValue(const Params& params, const FieldDescriptor* field);
132e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
133e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville}  // namespace javamicro
134e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville}  // namespace compiler
135e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville}  // namespace protobuf
136e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville
137e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville}  // namespace google
138e2d542951c059563a3b7f74c257dac4f222d9dc5Wink Saville#endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
139