javamicro_helpers.h revision ede38fe9b9f93888e6e41afc7abb09525f44da95
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/javamicro/javamicro_params.h>
40#include <google/protobuf/descriptor.pb.h>
41#include <google/protobuf/descriptor.h>
42
43namespace google {
44namespace protobuf {
45namespace compiler {
46namespace javamicro {
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// Similar, but for method names.  (Typically, this merely has the effect
59// of lower-casing the first letter of the name.)
60string UnderscoresToCamelCase(const MethodDescriptor* method);
61
62// Strips ".proto" or ".protodevel" from the end of a filename.
63string StripProto(const string& filename);
64
65// Gets the unqualified class name for the file.  Each .proto file becomes a
66// single Java class, with all its contents nested in that class.
67string FileClassName(const Params& params, const FileDescriptor* file);
68
69// Returns the file's Java package name.
70string FileJavaPackage(const Params& params, const FileDescriptor* file);
71
72// Converts the given fully-qualified name in the proto namespace to its
73// fully-qualified name in the Java namespace, given that it is in the given
74// file.
75string ToJavaName(const Params& params, const string& full_name,
76    const FileDescriptor* file);
77
78// These return the fully-qualified class name corresponding to the given
79// descriptor.
80inline string ClassName(const Params& params, const Descriptor* descriptor) {
81  return ToJavaName(params, descriptor->full_name(), descriptor->file());
82}
83string ClassName(const Params& params, const EnumDescriptor* descriptor);
84inline string ClassName(const Params& params,
85    const ServiceDescriptor* descriptor) {
86  return ToJavaName(params, descriptor->full_name(), descriptor->file());
87}
88inline string ExtensionIdentifierName(const Params& params,
89    const FieldDescriptor* descriptor) {
90  return ToJavaName(params, descriptor->full_name(), descriptor->file());
91}
92string ClassName(const Params& params, const FileDescriptor* descriptor);
93
94// Get the unqualified name that should be used for a field's field
95// number constant.
96string FieldConstantName(const FieldDescriptor *field);
97
98enum JavaType {
99  JAVATYPE_INT,
100  JAVATYPE_LONG,
101  JAVATYPE_FLOAT,
102  JAVATYPE_DOUBLE,
103  JAVATYPE_BOOLEAN,
104  JAVATYPE_STRING,
105  JAVATYPE_BYTES,
106  JAVATYPE_ENUM,
107  JAVATYPE_MESSAGE
108};
109
110JavaType GetJavaType(FieldDescriptor::Type field_type);
111
112inline JavaType GetJavaType(const FieldDescriptor* field) {
113  return GetJavaType(field->type());
114}
115
116// Get the fully-qualified class name for a boxed primitive type, e.g.
117// "java.lang.Integer" for JAVATYPE_INT.  Returns NULL for enum and message
118// types.
119const char* BoxedPrimitiveTypeName(JavaType type);
120
121string DefaultValue(const Params& params, const FieldDescriptor* field);
122
123}  // namespace javamicro
124}  // namespace compiler
125}  // namespace protobuf
126
127}  // namespace google
128#endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
129