java_extension.cc
Go to the documentation of this file.
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 
36 
43 
44 
45 namespace google {
46 namespace protobuf {
47 namespace compiler {
48 namespace java {
49 
51  const FieldDescriptor* descriptor, Context* context)
52  : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
53  if (descriptor_->extension_scope() != NULL) {
54  scope_ =
56  } else {
58  }
59 }
60 
62 
63 // Initializes the vars referenced in the generated code templates.
65  const FieldDescriptor* descriptor, const std::string& scope, bool immutable,
66  ClassNameResolver* name_resolver,
67  std::map<std::string, std::string>* vars_pointer) {
68  std::map<std::string, std::string>& vars = *vars_pointer;
69  vars["scope"] = scope;
71  vars["containing_type"] =
72  name_resolver->GetClassName(descriptor->containing_type(), immutable);
73  vars["number"] = StrCat(descriptor->number());
74  vars["constant_name"] = FieldConstantName(descriptor);
75  vars["index"] = StrCat(descriptor->index());
76  vars["default"] = descriptor->is_repeated()
77  ? ""
78  : DefaultValue(descriptor, immutable, name_resolver);
79  vars["type_constant"] = FieldTypeName(GetType(descriptor));
80  vars["packed"] = descriptor->is_packed() ? "true" : "false";
81  vars["enum_map"] = "null";
82  vars["prototype"] = "null";
83 
84  JavaType java_type = GetJavaType(descriptor);
85  std::string singular_type;
86  switch (java_type) {
87  case JAVATYPE_MESSAGE:
88  singular_type =
89  name_resolver->GetClassName(descriptor->message_type(), immutable);
90  vars["prototype"] = singular_type + ".getDefaultInstance()";
91  break;
92  case JAVATYPE_ENUM:
93  singular_type =
94  name_resolver->GetClassName(descriptor->enum_type(), immutable);
95  vars["enum_map"] = singular_type + ".internalGetValueMap()";
96  break;
97  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;
103  default:
104  singular_type = BoxedPrimitiveTypeName(java_type);
105  break;
106  }
107  vars["type"] = descriptor->is_repeated()
108  ? "java.util.List<" + singular_type + ">"
109  : singular_type;
110  vars["singular_type"] = singular_type;
111 }
112 
114  std::map<std::string, std::string> vars;
115  const bool kUseImmutableNames = true;
116  InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
117  &vars);
118  printer->Print(vars, "public static final int $constant_name$ = $number$;\n");
119 
121  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");
132  } else {
133  // 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");
145  }
146  printer->Annotate("name", descriptor_);
147 }
148 
150  io::Printer* printer) {
151  int bytecode_estimate = 0;
152  if (descriptor_->extension_scope() == NULL) {
153  // Only applies to non-nested extensions.
154  printer->Print(
155  "$name$.internalInit(descriptor.getExtensions().get($index$));\n",
157  StrCat(descriptor_->index()));
158  bytecode_estimate += 21;
159  }
160  return bytecode_estimate;
161 }
162 
164  io::Printer* printer) {
165  printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name",
167  return 7;
168 }
169 
170 } // namespace java
171 } // namespace compiler
172 } // namespace protobuf
173 } // namespace google
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
google::protobuf::compiler::java::UnderscoresToCamelCaseCheckReserved
std::string UnderscoresToCamelCaseCheckReserved(const FieldDescriptor *field)
Definition: java_helpers.cc:212
java_extension.h
java_name_resolver.h
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
java_doc_comment.h
google::protobuf::compiler::java::ImmutableExtensionGenerator::ImmutableExtensionGenerator
ImmutableExtensionGenerator(const FieldDescriptor *descriptor, Context *context)
Definition: java_extension.cc:50
java_helpers.h
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::compiler::java::JAVATYPE_ENUM
@ JAVATYPE_ENUM
Definition: java_helpers.h:213
google::protobuf::compiler::java::Context
Definition: java_context.h:65
google::protobuf::compiler::java::WriteFieldDocComment
void WriteFieldDocComment(io::Printer *printer, const FieldDescriptor *field)
Definition: java_doc_comment.cc:175
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::compiler::java::ClassNameResolver::GetImmutableClassName
std::string GetImmutableClassName(const DescriptorType *descriptor)
Definition: java_name_resolver.h:88
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::java::ImmutableExtensionGenerator::Generate
virtual void Generate(io::Printer *printer)
Definition: java_extension.cc:113
google::protobuf::compiler::java::ImmutableExtensionGenerator::GenerateNonNestedInitializationCode
virtual int GenerateNonNestedInitializationCode(io::Printer *printer)
Definition: java_extension.cc:149
google::protobuf::compiler::java::BoxedPrimitiveTypeName
const char * BoxedPrimitiveTypeName(JavaType type)
Definition: java_helpers.cc:400
google::protobuf::compiler::java::JAVATYPE_MESSAGE
@ JAVATYPE_MESSAGE
Definition: java_helpers.h:214
google::protobuf::compiler::java::ImmutableExtensionGenerator::name_resolver_
ClassNameResolver * name_resolver_
Definition: java_extension.h:103
strutil.h
google::protobuf::FieldDescriptor::file
const FileDescriptor * file() const
google::protobuf::FieldDescriptor::extension_scope
const Descriptor * extension_scope() const
google::protobuf::compiler::java::GetJavaType
JavaType GetJavaType(const FieldDescriptor *field)
Definition: java_helpers.cc:321
printer.h
google::protobuf::compiler::java::ImmutableExtensionGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: java_extension.h:102
google::protobuf::compiler::java::ExtensionGenerator::InitTemplateVars
static void InitTemplateVars(const FieldDescriptor *descriptor, const std::string &scope, bool immutable, ClassNameResolver *name_resolver, std::map< std::string, std::string > *vars_pointer)
Definition: java_extension.cc:64
google::protobuf::compiler::java::ImmutableExtensionGenerator::scope_
std::string scope_
Definition: java_extension.h:104
google::protobuf::io::Printer::Annotate
void Annotate(const char *varname, const SomeDescriptor *descriptor)
Definition: printer.h:199
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::java::GetType
FieldDescriptor::Type GetType(const FieldDescriptor *field)
Definition: java_helpers.cc:317
java
google::protobuf::compiler::java::FieldTypeName
const char * FieldTypeName(FieldDescriptor::Type field_type)
Definition: java_helpers.cc:445
google::protobuf::compiler::java::ImmutableExtensionGenerator::~ImmutableExtensionGenerator
virtual ~ImmutableExtensionGenerator()
Definition: java_extension.cc:61
google::protobuf::FieldDescriptor::index
int index() const
Definition: src/google/protobuf/descriptor.h:2081
google::protobuf::compiler::java::ImmutableExtensionGenerator::GenerateRegistrationCode
virtual int GenerateRegistrationCode(io::Printer *printer)
Definition: java_extension.cc:163
google::protobuf::compiler::java::JAVATYPE_BYTES
@ JAVATYPE_BYTES
Definition: java_helpers.h:212
google::protobuf::compiler::java::FieldConstantName
std::string FieldConstantName(const FieldDescriptor *field)
Definition: java_helpers.cc:311
google::protobuf::compiler::java::DefaultValue
std::string DefaultValue(const FieldDescriptor *field, bool immutable, ClassNameResolver *name_resolver)
Definition: java_helpers.cc:501
java_context.h
google::protobuf::compiler::java::ClassNameResolver::GetClassName
std::string GetClassName(const Descriptor *descriptor, bool immutable)
Definition: java_name_resolver.cc:229
google::protobuf::compiler::java::ClassNameResolver
Definition: java_name_resolver.h:56
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
google::protobuf::compiler::java::JavaType
JavaType
Definition: java_helpers.h:205
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::java::JAVATYPE_STRING
@ JAVATYPE_STRING
Definition: java_helpers.h:211


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54