blob: 4db7085e01c6bf8835d978ea139aedad763cc837 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@google.com (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34
35#include <google/protobuf/compiler/java/java_extension.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000036
37#include <google/protobuf/compiler/java/java_context.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000038#include <google/protobuf/compiler/java/java_doc_comment.h>
temporal40ee5512008-07-10 02:12:20 +000039#include <google/protobuf/compiler/java/java_helpers.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000040#include <google/protobuf/compiler/java/java_name_resolver.h>
temporal40ee5512008-07-10 02:12:20 +000041#include <google/protobuf/io/printer.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000042#include <google/protobuf/stubs/strutil.h>
temporal40ee5512008-07-10 02:12:20 +000043
44namespace google {
45namespace protobuf {
46namespace compiler {
47namespace java {
48
jieluo@google.com4de8f552014-07-18 00:47:59 +000049ImmutableExtensionGenerator::ImmutableExtensionGenerator(
50 const FieldDescriptor* descriptor, Context* context)
51 : descriptor_(descriptor), context_(context),
52 name_resolver_(context->GetNameResolver()) {
kenton@google.com24bf56f2008-09-24 20:31:01 +000053 if (descriptor_->extension_scope() != NULL) {
jieluo@google.com4de8f552014-07-18 00:47:59 +000054 scope_ = name_resolver_->GetImmutableClassName(
55 descriptor_->extension_scope());
kenton@google.com24bf56f2008-09-24 20:31:01 +000056 } else {
jieluo@google.com4de8f552014-07-18 00:47:59 +000057 scope_ = name_resolver_->GetImmutableClassName(descriptor_->file());
kenton@google.com24bf56f2008-09-24 20:31:01 +000058 }
59}
temporal40ee5512008-07-10 02:12:20 +000060
jieluo@google.com4de8f552014-07-18 00:47:59 +000061ImmutableExtensionGenerator::~ImmutableExtensionGenerator() {}
temporal40ee5512008-07-10 02:12:20 +000062
liujisi@google.com33165fe2010-11-02 13:14:58 +000063// Initializes the vars referenced in the generated code templates.
jieluo@google.com4de8f552014-07-18 00:47:59 +000064void ExtensionGenerator::InitTemplateVars(const FieldDescriptor* descriptor,
65 const string& scope,
66 bool immutable,
67 ClassNameResolver* name_resolver,
68 map<string, string>* vars_pointer) {
liujisi@google.com33165fe2010-11-02 13:14:58 +000069 map<string, string> &vars = *vars_pointer;
70 vars["scope"] = scope;
71 vars["name"] = UnderscoresToCamelCase(descriptor);
jieluo@google.com4de8f552014-07-18 00:47:59 +000072 vars["containing_type"] =
73 name_resolver->GetClassName(descriptor->containing_type(), immutable);
liujisi@google.com33165fe2010-11-02 13:14:58 +000074 vars["number"] = SimpleItoa(descriptor->number());
75 vars["constant_name"] = FieldConstantName(descriptor);
76 vars["index"] = SimpleItoa(descriptor->index());
jieluo@google.com4de8f552014-07-18 00:47:59 +000077 vars["default"] = descriptor->is_repeated() ?
78 "" : DefaultValue(descriptor, immutable, name_resolver);
79 vars["type_constant"] = FieldTypeName(GetType(descriptor));
liujisi@google.com33165fe2010-11-02 13:14:58 +000080 vars["packed"] = descriptor->options().packed() ? "true" : "false";
kenton@google.com80b1d622009-07-29 01:13:20 +000081 vars["enum_map"] = "null";
82 vars["prototype"] = "null";
temporal40ee5512008-07-10 02:12:20 +000083
liujisi@google.com33165fe2010-11-02 13:14:58 +000084 JavaType java_type = GetJavaType(descriptor);
temporal40ee5512008-07-10 02:12:20 +000085 string singular_type;
86 switch (java_type) {
87 case JAVATYPE_MESSAGE:
jieluo@google.com4de8f552014-07-18 00:47:59 +000088 singular_type = name_resolver->GetClassName(descriptor->message_type(),
89 immutable);
liujisi@google.com33165fe2010-11-02 13:14:58 +000090 vars["prototype"] = singular_type + ".getDefaultInstance()";
temporal40ee5512008-07-10 02:12:20 +000091 break;
92 case JAVATYPE_ENUM:
jieluo@google.com4de8f552014-07-18 00:47:59 +000093 singular_type = name_resolver->GetClassName(descriptor->enum_type(),
94 immutable);
liujisi@google.com33165fe2010-11-02 13:14:58 +000095 vars["enum_map"] = singular_type + ".internalGetValueMap()";
temporal40ee5512008-07-10 02:12:20 +000096 break;
jieluo@google.com4de8f552014-07-18 00:47:59 +000097 case JAVATYPE_STRING:
98 singular_type = "java.lang.String";
99 break;
100 case JAVATYPE_BYTES:
101 singular_type = immutable ? "com.google.protobuf.ByteString" : "byte[]";
102 break;
temporal40ee5512008-07-10 02:12:20 +0000103 default:
liujisi@google.com33165fe2010-11-02 13:14:58 +0000104 singular_type = BoxedPrimitiveTypeName(java_type);
temporal40ee5512008-07-10 02:12:20 +0000105 break;
106 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000107 vars["type"] = descriptor->is_repeated() ?
108 "java.util.List<" + singular_type + ">" : singular_type;
109 vars["singular_type"] = singular_type;
110}
111
jieluo@google.com4de8f552014-07-18 00:47:59 +0000112void ImmutableExtensionGenerator::Generate(io::Printer* printer) {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000113 map<string, string> vars;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000114 const bool kUseImmutableNames = true;
115 InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
116 &vars);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000117 printer->Print(vars,
118 "public static final int $constant_name$ = $number$;\n");
temporal40ee5512008-07-10 02:12:20 +0000119
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000120 WriteFieldDocComment(printer, descriptor_);
kenton@google.com80b1d622009-07-29 01:13:20 +0000121 if (HasDescriptorMethods(descriptor_->file())) {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000122 // Non-lite extensions
123 if (descriptor_->extension_scope() == NULL) {
124 // Non-nested
125 printer->Print(
126 vars,
127 "public static final\n"
128 " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
129 " $containing_type$,\n"
130 " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
131 " .newFileScopedGeneratedExtension(\n"
132 " $singular_type$.class,\n"
133 " $prototype$);\n");
kenton@google.com80b1d622009-07-29 01:13:20 +0000134 } else {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000135 // Nested
136 printer->Print(
137 vars,
138 "public static final\n"
139 " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
140 " $containing_type$,\n"
141 " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
142 " .newMessageScopedGeneratedExtension(\n"
143 " $scope$.getDefaultInstance(),\n"
144 " $index$,\n"
145 " $singular_type$.class,\n"
146 " $prototype$);\n");
kenton@google.com80b1d622009-07-29 01:13:20 +0000147 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000148 } else {
149 // Lite extensions
150 if (descriptor_->is_repeated()) {
151 printer->Print(
152 vars,
153 "public static final\n"
154 " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
155 " $containing_type$,\n"
156 " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
157 " .newRepeatedGeneratedExtension(\n"
158 " $containing_type$.getDefaultInstance(),\n"
159 " $prototype$,\n"
160 " $enum_map$,\n"
161 " $number$,\n"
162 " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000163 " $packed$,\n"
164 " $singular_type$.class);\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000165 } else {
166 printer->Print(
167 vars,
168 "public static final\n"
169 " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
170 " $containing_type$,\n"
171 " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
172 " .newSingularGeneratedExtension(\n"
173 " $containing_type$.getDefaultInstance(),\n"
174 " $default$,\n"
175 " $prototype$,\n"
176 " $enum_map$,\n"
177 " $number$,\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000178 " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
179 " $singular_type$.class);\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000180 }
181 }
182}
183
Daniel Martine2416ca2014-11-25 10:37:57 -0500184int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode(
liujisi@google.com33165fe2010-11-02 13:14:58 +0000185 io::Printer* printer) {
Daniel Martine2416ca2014-11-25 10:37:57 -0500186 int bytecode_estimate = 0;
liujisi@google.com33165fe2010-11-02 13:14:58 +0000187 if (descriptor_->extension_scope() == NULL &&
188 HasDescriptorMethods(descriptor_->file())) {
189 // Only applies to non-nested, non-lite extensions.
190 printer->Print(
191 "$name$.internalInit(descriptor.getExtensions().get($index$));\n",
192 "name", UnderscoresToCamelCase(descriptor_),
193 "index", SimpleItoa(descriptor_->index()));
Daniel Martine2416ca2014-11-25 10:37:57 -0500194 bytecode_estimate += 21;
temporal40ee5512008-07-10 02:12:20 +0000195 }
Daniel Martine2416ca2014-11-25 10:37:57 -0500196 return bytecode_estimate;
temporal40ee5512008-07-10 02:12:20 +0000197}
198
Daniel Martine2416ca2014-11-25 10:37:57 -0500199int ImmutableExtensionGenerator::GenerateRegistrationCode(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000200 io::Printer* printer) {
kenton@google.com24bf56f2008-09-24 20:31:01 +0000201 printer->Print(
202 "registry.add($scope$.$name$);\n",
203 "scope", scope_,
204 "name", UnderscoresToCamelCase(descriptor_));
Daniel Martine2416ca2014-11-25 10:37:57 -0500205 return 7;
kenton@google.com24bf56f2008-09-24 20:31:01 +0000206}
207
temporal40ee5512008-07-10 02:12:20 +0000208} // namespace java
209} // namespace compiler
210} // namespace protobuf
temporal40ee5512008-07-10 02:12:20 +0000211} // namespace google