blob: 46b5faaa50945d9766bdbe6ae0d0878b7150abb8 [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_);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700121 if (descriptor_->extension_scope() == NULL) {
122 // Non-nested
123 printer->Print(
124 vars,
125 "public static final\n"
126 " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
127 " $containing_type$,\n"
128 " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
129 " .newFileScopedGeneratedExtension(\n"
130 " $singular_type$.class,\n"
131 " $prototype$);\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000132 } else {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700133 // Nested
134 printer->Print(
135 vars,
136 "public static final\n"
137 " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
138 " $containing_type$,\n"
139 " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
140 " .newMessageScopedGeneratedExtension(\n"
141 " $scope$.getDefaultInstance(),\n"
142 " $index$,\n"
143 " $singular_type$.class,\n"
144 " $prototype$);\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000145 }
146}
147
Daniel Martine2416ca2014-11-25 10:37:57 -0500148int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode(
liujisi@google.com33165fe2010-11-02 13:14:58 +0000149 io::Printer* printer) {
Daniel Martine2416ca2014-11-25 10:37:57 -0500150 int bytecode_estimate = 0;
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700151 if (descriptor_->extension_scope() == NULL) {
152 // Only applies to non-nested extensions.
liujisi@google.com33165fe2010-11-02 13:14:58 +0000153 printer->Print(
154 "$name$.internalInit(descriptor.getExtensions().get($index$));\n",
155 "name", UnderscoresToCamelCase(descriptor_),
156 "index", SimpleItoa(descriptor_->index()));
Daniel Martine2416ca2014-11-25 10:37:57 -0500157 bytecode_estimate += 21;
temporal40ee5512008-07-10 02:12:20 +0000158 }
Daniel Martine2416ca2014-11-25 10:37:57 -0500159 return bytecode_estimate;
temporal40ee5512008-07-10 02:12:20 +0000160}
161
Daniel Martine2416ca2014-11-25 10:37:57 -0500162int ImmutableExtensionGenerator::GenerateRegistrationCode(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000163 io::Printer* printer) {
kenton@google.com24bf56f2008-09-24 20:31:01 +0000164 printer->Print(
165 "registry.add($scope$.$name$);\n",
166 "scope", scope_,
167 "name", UnderscoresToCamelCase(descriptor_));
Daniel Martine2416ca2014-11-25 10:37:57 -0500168 return 7;
kenton@google.com24bf56f2008-09-24 20:31:01 +0000169}
170
temporal40ee5512008-07-10 02:12:20 +0000171} // namespace java
172} // namespace compiler
173} // namespace protobuf
temporal40ee5512008-07-10 02:12:20 +0000174} // namespace google