1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
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 #include <google/protobuf/compiler/java/java_extension.h>
36
37 #include <google/protobuf/io/printer.h>
38 #include <google/protobuf/stubs/strutil.h>
39 #include <google/protobuf/compiler/java/java_context.h>
40 #include <google/protobuf/compiler/java/java_doc_comment.h>
41 #include <google/protobuf/compiler/java/java_helpers.h>
42 #include <google/protobuf/compiler/java/java_name_resolver.h>
43
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace java {
48
ImmutableExtensionGenerator(const FieldDescriptor * descriptor,Context * context)49 ImmutableExtensionGenerator::ImmutableExtensionGenerator(
50 const FieldDescriptor* descriptor, Context* context)
51 : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
52 if (descriptor_->extension_scope() != NULL) {
53 scope_ =
54 name_resolver_->GetImmutableClassName(descriptor_->extension_scope());
55 } else {
56 scope_ = name_resolver_->GetImmutableClassName(descriptor_->file());
57 }
58 }
59
~ImmutableExtensionGenerator()60 ImmutableExtensionGenerator::~ImmutableExtensionGenerator() {}
61
62 // Initializes the vars referenced in the generated code templates.
InitTemplateVars(const FieldDescriptor * descriptor,const std::string & scope,bool immutable,ClassNameResolver * name_resolver,std::map<std::string,std::string> * vars_pointer)63 void ExtensionGenerator::InitTemplateVars(
64 const FieldDescriptor* descriptor, const std::string& scope, bool immutable,
65 ClassNameResolver* name_resolver,
66 std::map<std::string, std::string>* vars_pointer) {
67 std::map<std::string, std::string>& vars = *vars_pointer;
68 vars["scope"] = scope;
69 vars["name"] = UnderscoresToCamelCaseCheckReserved(descriptor);
70 vars["containing_type"] =
71 name_resolver->GetClassName(descriptor->containing_type(), immutable);
72 vars["number"] = StrCat(descriptor->number());
73 vars["constant_name"] = FieldConstantName(descriptor);
74 vars["index"] = StrCat(descriptor->index());
75 vars["default"] = descriptor->is_repeated()
76 ? ""
77 : DefaultValue(descriptor, immutable, name_resolver);
78 vars["type_constant"] = FieldTypeName(GetType(descriptor));
79 vars["packed"] = descriptor->is_packed() ? "true" : "false";
80 vars["enum_map"] = "null";
81 vars["prototype"] = "null";
82
83 JavaType java_type = GetJavaType(descriptor);
84 std::string singular_type;
85 switch (java_type) {
86 case JAVATYPE_MESSAGE:
87 singular_type =
88 name_resolver->GetClassName(descriptor->message_type(), immutable);
89 vars["prototype"] = singular_type + ".getDefaultInstance()";
90 break;
91 case JAVATYPE_ENUM:
92 singular_type =
93 name_resolver->GetClassName(descriptor->enum_type(), immutable);
94 vars["enum_map"] = singular_type + ".internalGetValueMap()";
95 break;
96 case JAVATYPE_STRING:
97 singular_type = "java.lang.String";
98 break;
99 case JAVATYPE_BYTES:
100 singular_type = immutable ? "com.google.protobuf.ByteString" : "byte[]";
101 break;
102 default:
103 singular_type = BoxedPrimitiveTypeName(java_type);
104 break;
105 }
106 vars["type"] = descriptor->is_repeated()
107 ? "java.util.List<" + singular_type + ">"
108 : singular_type;
109 vars["singular_type"] = singular_type;
110 }
111
Generate(io::Printer * printer)112 void ImmutableExtensionGenerator::Generate(io::Printer* printer) {
113 std::map<std::string, std::string> vars;
114 const bool kUseImmutableNames = true;
115 InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
116 &vars);
117 printer->Print(vars, "public static final int $constant_name$ = $number$;\n");
118
119 WriteFieldDocComment(printer, descriptor_);
120 if (descriptor_->extension_scope() == NULL) {
121 // Non-nested
122 printer->Print(
123 vars,
124 "public static final\n"
125 " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
126 " $containing_type$,\n"
127 " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
128 " .newFileScopedGeneratedExtension(\n"
129 " $singular_type$.class,\n"
130 " $prototype$);\n");
131 } else {
132 // Nested
133 printer->Print(
134 vars,
135 "public static final\n"
136 " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
137 " $containing_type$,\n"
138 " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
139 " .newMessageScopedGeneratedExtension(\n"
140 " $scope$.getDefaultInstance(),\n"
141 " $index$,\n"
142 " $singular_type$.class,\n"
143 " $prototype$);\n");
144 }
145 printer->Annotate("name", descriptor_);
146 }
147
GenerateNonNestedInitializationCode(io::Printer * printer)148 int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode(
149 io::Printer* printer) {
150 int bytecode_estimate = 0;
151 if (descriptor_->extension_scope() == NULL) {
152 // Only applies to non-nested extensions.
153 printer->Print(
154 "$name$.internalInit(descriptor.getExtensions().get($index$));\n",
155 "name", UnderscoresToCamelCaseCheckReserved(descriptor_), "index",
156 StrCat(descriptor_->index()));
157 bytecode_estimate += 21;
158 }
159 return bytecode_estimate;
160 }
161
GenerateRegistrationCode(io::Printer * printer)162 int ImmutableExtensionGenerator::GenerateRegistrationCode(
163 io::Printer* printer) {
164 printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name",
165 UnderscoresToCamelCaseCheckReserved(descriptor_));
166 return 7;
167 }
168
169 } // namespace java
170 } // namespace compiler
171 } // namespace protobuf
172 } // namespace google
173