java_helpers.h revision fbaaef999ba563838ebd00874ed8a1c01fbf286d
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/descriptor.pb.h> 40#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 71// Converts the given fully-qualified name in the proto namespace to its 72// fully-qualified name in the Java namespace, given that it is in the given 73// file. 74string ToJavaName(const string& full_name, const FileDescriptor* file); 75 76// These return the fully-qualified class name corresponding to the given 77// descriptor. 78inline string ClassName(const Descriptor* descriptor) { 79 return ToJavaName(descriptor->full_name(), descriptor->file()); 80} 81inline string ClassName(const EnumDescriptor* descriptor) { 82 return ToJavaName(descriptor->full_name(), descriptor->file()); 83} 84inline string ClassName(const ServiceDescriptor* descriptor) { 85 return ToJavaName(descriptor->full_name(), descriptor->file()); 86} 87inline string ExtensionIdentifierName(const FieldDescriptor* descriptor) { 88 return ToJavaName(descriptor->full_name(), descriptor->file()); 89} 90string ClassName(const FileDescriptor* descriptor); 91 92// Get the unqualified name that should be used for a field's field 93// number constant. 94string FieldConstantName(const FieldDescriptor *field); 95 96enum JavaType { 97 JAVATYPE_INT, 98 JAVATYPE_LONG, 99 JAVATYPE_FLOAT, 100 JAVATYPE_DOUBLE, 101 JAVATYPE_BOOLEAN, 102 JAVATYPE_STRING, 103 JAVATYPE_BYTES, 104 JAVATYPE_ENUM, 105 JAVATYPE_MESSAGE 106}; 107 108JavaType GetJavaType(FieldDescriptor::Type field_type); 109 110inline JavaType GetJavaType(const FieldDescriptor* field) { 111 return GetJavaType(field->type()); 112} 113 114// Get the fully-qualified class name for a boxed primitive type, e.g. 115// "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message 116// types. 117const char* BoxedPrimitiveTypeName(JavaType type); 118 119string DefaultValue(const FieldDescriptor* field); 120 121// Does this message class keep track of unknown fields? 122inline bool HasUnknownFields(const Descriptor* descriptor) { 123 return descriptor->file()->options().optimize_for() != 124 FileOptions::LITE_RUNTIME; 125} 126 127// Does this message class have generated parsing, serialization, and other 128// standard methods for which reflection-based fallback implementations exist? 129inline bool HasGeneratedMethods(const Descriptor* descriptor) { 130 return descriptor->file()->options().optimize_for() != 131 FileOptions::CODE_SIZE; 132} 133 134// Does this message class have descriptor and reflection methods? 135inline bool HasDescriptorMethods(const Descriptor* descriptor) { 136 return descriptor->file()->options().optimize_for() != 137 FileOptions::LITE_RUNTIME; 138} 139inline bool HasDescriptorMethods(const EnumDescriptor* descriptor) { 140 return descriptor->file()->options().optimize_for() != 141 FileOptions::LITE_RUNTIME; 142} 143inline bool HasDescriptorMethods(const FileDescriptor* descriptor) { 144 return descriptor->options().optimize_for() != 145 FileOptions::LITE_RUNTIME; 146} 147 148} // namespace java 149} // namespace compiler 150} // namespace protobuf 151 152} // namespace google 153#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ 154