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 96// Returns the type of the FieldDescriptor. 97// This does nothing interesting for the open source release, but is used for 98// hacks that improve compatability with version 1 protocol buffers at Google. 99FieldDescriptor::Type GetType(const FieldDescriptor* field); 100 101enum JavaType { 102 JAVATYPE_INT, 103 JAVATYPE_LONG, 104 JAVATYPE_FLOAT, 105 JAVATYPE_DOUBLE, 106 JAVATYPE_BOOLEAN, 107 JAVATYPE_STRING, 108 JAVATYPE_BYTES, 109 JAVATYPE_ENUM, 110 JAVATYPE_MESSAGE 111}; 112 113JavaType GetJavaType(const FieldDescriptor* field); 114 115// Get the fully-qualified class name for a boxed primitive type, e.g. 116// "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message 117// types. 118const char* BoxedPrimitiveTypeName(JavaType type); 119 120string DefaultValue(const FieldDescriptor* field); 121 122// Does this message class keep track of unknown fields? 123inline bool HasUnknownFields(const Descriptor* descriptor) { 124 return descriptor->file()->options().optimize_for() != 125 FileOptions::LITE_RUNTIME; 126} 127 128// Does this message class have generated parsing, serialization, and other 129// standard methods for which reflection-based fallback implementations exist? 130inline bool HasGeneratedMethods(const Descriptor* descriptor) { 131 return descriptor->file()->options().optimize_for() != 132 FileOptions::CODE_SIZE; 133} 134 135// Does this message class have descriptor and reflection methods? 136inline bool HasDescriptorMethods(const Descriptor* descriptor) { 137 return descriptor->file()->options().optimize_for() != 138 FileOptions::LITE_RUNTIME; 139} 140inline bool HasDescriptorMethods(const EnumDescriptor* descriptor) { 141 return descriptor->file()->options().optimize_for() != 142 FileOptions::LITE_RUNTIME; 143} 144inline bool HasDescriptorMethods(const FileDescriptor* descriptor) { 145 return descriptor->options().optimize_for() != 146 FileOptions::LITE_RUNTIME; 147} 148 149// Should we generate generic services for this file? 150inline bool HasGenericServices(const FileDescriptor *file) { 151 return file->service_count() > 0 && 152 file->options().optimize_for() != FileOptions::LITE_RUNTIME && 153 file->options().java_generic_services(); 154} 155 156} // namespace java 157} // namespace compiler 158} // namespace protobuf 159 160} // namespace google 161#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ 162