1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Protocol Buffers - Google's data interchange format 2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2008 Google Inc. All rights reserved. 3afb4b72037e3f13db208590fc782c4bc8e27f862Jeff Davidson// https://developers.google.com/protocol-buffers/ 4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without 6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are 7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met: 8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions of source code must retain the above copyright 10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer. 11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions in binary form must reproduce the above 12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer 13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the 14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution. 15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Neither the name of Google Inc. nor the names of its 16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from 17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission. 18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Author: kenton@google.com (Kenton Varda) 32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Based on original Protocol Buffers design by 33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Sanjay Ghemawat, Jeff Dean, and others. 34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ 36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ 37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <string> 39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/descriptor.pb.h> 40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/descriptor.h> 41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace google { 43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace protobuf { 44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace compiler { 45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace java { 46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Commonly-used separator comments. Thick is a line of '=', thin is a line 48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// of '-'. 49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleextern const char kThickSeparator[]; 50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleextern const char kThinSeparator[]; 51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 52a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Converts a name to camel-case. If cap_first_letter is true, capitalize the 53a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// first letter. 54a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring UnderscoresToCamelCase(const string& name, bool cap_first_letter); 55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Converts the field's name to camel-case, e.g. "foo_bar_baz" becomes 56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "fooBarBaz" or "FooBarBaz", respectively. 57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillestring UnderscoresToCamelCase(const FieldDescriptor* field); 58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillestring UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); 59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Similar, but for method names. (Typically, this merely has the effect 61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// of lower-casing the first letter of the name.) 62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillestring UnderscoresToCamelCase(const MethodDescriptor* method); 63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 64a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Get an identifier that uniquely identifies this type within the file. 65a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// This is used to declare static variables related to this type at the 66a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// outermost file scope. 67a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring UniqueFileScopeIdentifier(const Descriptor* descriptor); 68a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Strips ".proto" or ".protodevel" from the end of a filename. 70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillestring StripProto(const string& filename); 71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 72a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Gets the unqualified class name for the file. For each .proto file, there 73a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// will be one Java class containing all the immutable messages and another 74a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Java class containing all the mutable messages. 75a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// TODO(xiaofeng): remove the default value after updating client code. 76a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring FileClassName(const FileDescriptor* file, bool immutable = true); 77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns the file's Java package name. 79a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring FileJavaPackage(const FileDescriptor* file, bool immutable = true); 80a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 81a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Returns output directory for the given package name. 82a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring JavaPackageToDir(string package_name); 83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Converts the given fully-qualified name in the proto namespace to its 85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// fully-qualified name in the Java namespace, given that it is in the given 86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// file. 87a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// TODO(xiaofeng): this method is deprecated and should be removed in the 88a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// future. 89a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ToJavaName(const string& full_name, 90a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson const FileDescriptor* file); 91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 92a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// TODO(xiaofeng): the following methods are kept for they are exposed 93a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// publicly in //google/protobuf/compiler/java/names.h. They return 94a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// immutable names only and should be removed after mutable API is 95a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// integrated into google3. 96a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ClassName(const Descriptor* descriptor); 97a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ClassName(const EnumDescriptor* descriptor); 98a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ClassName(const ServiceDescriptor* descriptor); 99a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ClassName(const FileDescriptor* descriptor); 100a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 101a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Comma-separate list of option-specified interfaces implemented by the 102a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Message, to follow the "implements" declaration of the Message definition. 103a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ExtraMessageInterfaces(const Descriptor* descriptor); 104a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Comma-separate list of option-specified interfaces implemented by the 105a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// MutableMessage, to follow the "implements" declaration of the MutableMessage 106a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// definition. 107a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ExtraMutableMessageInterfaces(const Descriptor* descriptor); 108a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Comma-separate list of option-specified interfaces implemented by the 109a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Builder, to follow the "implements" declaration of the Builder definition. 110a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ExtraBuilderInterfaces(const Descriptor* descriptor); 111a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Comma-separate list of option-specified interfaces extended by the 112a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// MessageOrBuilder, to follow the "extends" declaration of the 113a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// MessageOrBuilder definition. 114a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor); 115a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 116a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Get the unqualified Java class name for mutable messages. i.e. without 117a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// package or outer classnames. 118a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsoninline string ShortMutableJavaClassName(const Descriptor* descriptor) { 119a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return descriptor->name(); 120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 121a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 122a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 123a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Whether we should generate multiple java files for messages. 124a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsoninline bool MultipleJavaFiles( 125a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson const FileDescriptor* descriptor, bool immutable) { 126a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return descriptor->options().java_multiple_files(); 127fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 128fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 129fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Get the unqualified name that should be used for a field's field 130fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// number constant. 131fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillestring FieldConstantName(const FieldDescriptor *field); 132fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 133d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Returns the type of the FieldDescriptor. 134d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// This does nothing interesting for the open source release, but is used for 135d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// hacks that improve compatability with version 1 protocol buffers at Google. 136d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleFieldDescriptor::Type GetType(const FieldDescriptor* field); 137d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 138fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleenum JavaType { 139fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_INT, 140fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_LONG, 141fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_FLOAT, 142fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_DOUBLE, 143fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_BOOLEAN, 144fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_STRING, 145fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_BYTES, 146fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_ENUM, 147fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville JAVATYPE_MESSAGE 148fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}; 149fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 150d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleJavaType GetJavaType(const FieldDescriptor* field); 151fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 152fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Get the fully-qualified class name for a boxed primitive type, e.g. 153fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message 154fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// types. 155fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char* BoxedPrimitiveTypeName(JavaType type); 156fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 157a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Get the name of the java enum constant representing this type. E.g., 158a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// "INT32" for FieldDescriptor::TYPE_INT32. The enum constant's full 159a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// name is "com.google.protobuf.WireFormat.FieldType.INT32". 160a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonconst char* FieldTypeName(const FieldDescriptor::Type field_type); 161fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 162a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonclass ClassNameResolver; 163a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring DefaultValue(const FieldDescriptor* field, bool immutable, 164a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson ClassNameResolver* name_resolver); 165a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsoninline string ImmutableDefaultValue(const FieldDescriptor* field, 166a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson ClassNameResolver* name_resolver) { 167a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return DefaultValue(field, true, name_resolver); 168a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} 169a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonbool IsDefaultValueJavaDefault(const FieldDescriptor* field); 170a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 171a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Does this message class use UnknownFieldSet? 172a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Otherwise, unknown fields will be stored in a ByteString object 173a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsoninline bool UseUnknownFieldSet(const Descriptor* descriptor) { 174fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return descriptor->file()->options().optimize_for() != 175fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville FileOptions::LITE_RUNTIME; 176fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 177fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 178fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Does this message class have generated parsing, serialization, and other 179fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// standard methods for which reflection-based fallback implementations exist? 180fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline bool HasGeneratedMethods(const Descriptor* descriptor) { 181fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return descriptor->file()->options().optimize_for() != 182fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville FileOptions::CODE_SIZE; 183fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 184fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 185a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Does this message have specialized equals() and hashCode() methods? 186a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsoninline bool HasEqualsAndHashCode(const Descriptor* descriptor) { 187a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return descriptor->file()->options().java_generate_equals_and_hash(); 188a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} 189a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 190fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Does this message class have descriptor and reflection methods? 191fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline bool HasDescriptorMethods(const Descriptor* descriptor) { 192fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return descriptor->file()->options().optimize_for() != 193fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville FileOptions::LITE_RUNTIME; 194fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 195fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline bool HasDescriptorMethods(const EnumDescriptor* descriptor) { 196fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return descriptor->file()->options().optimize_for() != 197fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville FileOptions::LITE_RUNTIME; 198fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 199fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline bool HasDescriptorMethods(const FileDescriptor* descriptor) { 200fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return descriptor->options().optimize_for() != 201fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville FileOptions::LITE_RUNTIME; 202fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} 203fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 204a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsoninline bool HasNestedBuilders(const Descriptor* descriptor) { 205a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // The proto-lite version doesn't support nested builders. 206a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return descriptor->file()->options().optimize_for() != 207a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson FileOptions::LITE_RUNTIME; 208a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} 209a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 210d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Should we generate generic services for this file? 211d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilleinline bool HasGenericServices(const FileDescriptor *file) { 212d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return file->service_count() > 0 && 213d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville file->options().optimize_for() != FileOptions::LITE_RUNTIME && 214d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville file->options().java_generic_services(); 215d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville} 216d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 217a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsoninline bool IsLazy(const FieldDescriptor* descriptor) { 218a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // Currently, the proto-lite version suports lazy field. 219a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson // TODO(niwasaki): Support lazy fields also for other proto runtimes. 220a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson if (descriptor->file()->options().optimize_for() != 221a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson FileOptions::LITE_RUNTIME) { 222a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return false; 223a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson } 224a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return descriptor->options().lazy(); 225a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} 226a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 227a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Methods for shared bitfields. 228a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 229a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Gets the name of the shared bitfield for the given index. 230a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GetBitFieldName(int index); 231a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 232a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Gets the name of the shared bitfield for the given bit index. 233a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Effectively, GetBitFieldName(bitIndex / 32) 234a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GetBitFieldNameForBit(int bitIndex); 235a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 236a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Generates the java code for the expression that returns the boolean value 237a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// of the bit of the shared bitfields for the given bit index. 238a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Example: "((bitField1_ & 0x04) == 0x04)" 239a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GenerateGetBit(int bitIndex); 240a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 241a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Generates the java code for the expression that sets the bit of the shared 242a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// bitfields for the given bit index. 243a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Example: "bitField1_ = (bitField1_ | 0x04)" 244a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GenerateSetBit(int bitIndex); 245a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 246a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Generates the java code for the expression that clears the bit of the shared 247a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// bitfields for the given bit index. 248a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Example: "bitField1_ = (bitField1_ & ~0x04)" 249a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GenerateClearBit(int bitIndex); 250a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 251a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Does the same as GenerateGetBit but operates on the bit field on a local 252a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// variable. This is used by the builder to copy the value in the builder to 253a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// the message. 254a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Example: "((from_bitField1_ & 0x04) == 0x04)" 255a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GenerateGetBitFromLocal(int bitIndex); 256a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 257a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Does the same as GenerateSetBit but operates on the bit field on a local 258a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// variable. This is used by the builder to copy the value in the builder to 259a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// the message. 260a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Example: "to_bitField1_ = (to_bitField1_ | 0x04)" 261a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GenerateSetBitToLocal(int bitIndex); 262a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 263a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Does the same as GenerateGetBit but operates on the bit field on a local 264a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// variable. This is used by the parsing constructor to record if a repeated 265a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// field is mutable. 266a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Example: "((mutable_bitField1_ & 0x04) == 0x04)" 267a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GenerateGetBitMutableLocal(int bitIndex); 268a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 269a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Does the same as GenerateSetBit but operates on the bit field on a local 270a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// variable. This is used by the parsing constructor to record if a repeated 271a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// field is mutable. 272a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Example: "mutable_bitField1_ = (mutable_bitField1_ | 0x04)" 273a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstring GenerateSetBitMutableLocal(int bitIndex); 274a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 275a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Returns whether the JavaType is a reference type. 276a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonbool IsReferenceType(JavaType type); 277a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 278a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Returns the capitalized name for calling relative functions in 279a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// CodedInputStream 280a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonconst char* GetCapitalizedType(const FieldDescriptor* field, bool immutable); 281a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 282a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// For encodings with fixed sizes, returns that size in bytes. Otherwise 283a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// returns -1. 284a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonint FixedSize(FieldDescriptor::Type type); 285a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 286a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Comparators used to sort fields in MessageGenerator 287a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstruct FieldOrderingByNumber { 288a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson inline bool operator()(const FieldDescriptor* a, 289a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson const FieldDescriptor* b) const { 290a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return a->number() < b->number(); 291a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson } 292a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson}; 293a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 294a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonstruct ExtensionRangeOrdering { 295a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson bool operator()(const Descriptor::ExtensionRange* a, 296a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson const Descriptor::ExtensionRange* b) const { 297a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return a->start < b->start; 298a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson } 299a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson}; 300a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 301a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Sort the fields of the given Descriptor by number into a new[]'d array 302a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// and return it. The caller should delete the returned array. 303a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonconst FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor); 304a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 305a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Check a message type and its sub-message types recursively to see if any of 306a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// them has a required field. Return true if a required field is found. 307a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonbool HasRequiredFields(const Descriptor* descriptor); 308a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 309a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Whether a .proto file supports field presence test for non-message types. 310a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsoninline bool SupportFieldPresence(const FileDescriptor* descriptor) { 311a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson return true; 312a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson} 313a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 314a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson// Check whether a mesasge has repeated fields. 315a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidsonbool HasRepeatedFields(const Descriptor* descriptor); 316a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson 317fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace java 318fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace compiler 319fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace protobuf 320fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 321fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace google 322fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ 323