protobuf/src/google/protobuf/compiler/java/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 
35 #include <google/protobuf/compiler/java/java_extension.h>
36 
37 #include <google/protobuf/compiler/java/java_context.h>
38 #include <google/protobuf/compiler/java/java_doc_comment.h>
39 #include <google/protobuf/compiler/java/java_helpers.h>
40 #include <google/protobuf/compiler/java/java_name_resolver.h>
41 #include <google/protobuf/io/printer.h>
42 #include <google/protobuf/stubs/strutil.h>
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace java {
48 
50  const FieldDescriptor* descriptor, Context* context)
51  : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
52  if (descriptor_->extension_scope() != NULL) {
53  scope_ =
55  } else {
57  }
58 }
59 
61 
62 // Initializes the vars referenced in the generated code templates.
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;
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 
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 
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 
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",
156  StrCat(descriptor_->index()));
157  bytecode_estimate += 21;
158  }
159  return bytecode_estimate;
160 }
161 
163  io::Printer* printer) {
164  printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name",
166  return 7;
167 }
168 
169 } // namespace java
170 } // namespace compiler
171 } // namespace protobuf
172 } // namespace google
descriptor_
string_view descriptor_
Definition: elf.cc:154
google::protobuf::compiler::java::UnderscoresToCamelCaseCheckReserved
std::string UnderscoresToCamelCaseCheckReserved(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.cc:210
google::protobuf::compiler::java::ImmutableExtensionGenerator::ImmutableExtensionGenerator
ImmutableExtensionGenerator(const FieldDescriptor *descriptor, Context *context)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.cc:49
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::compiler::java::WriteFieldDocComment
void WriteFieldDocComment(io::Printer *printer, const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_doc_comment.cc:175
google::protobuf::compiler::java::ClassNameResolver::GetImmutableClassName
std::string GetImmutableClassName(const DescriptorType *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_name_resolver.h:88
google::protobuf::FieldDescriptor::extension_scope
const Descriptor * extension_scope
Definition: protobuf/src/google/protobuf/descriptor.h:933
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1482
google::protobuf::compiler::java::ImmutableExtensionGenerator::Generate
virtual void Generate(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.cc:112
FieldDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:133
google::protobuf::compiler::java::ImmutableExtensionGenerator::GenerateNonNestedInitializationCode
virtual int GenerateNonNestedInitializationCode(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.cc:148
google::protobuf::compiler::java::BoxedPrimitiveTypeName
const char * BoxedPrimitiveTypeName(JavaType type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.cc:398
google::protobuf::FieldDescriptor::file
const FileDescriptor * file() const
google::protobuf::compiler::java::GetJavaType
JavaType GetJavaType(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.cc:319
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: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.cc:63
google::protobuf::compiler::java::ImmutableExtensionGenerator::scope_
std::string scope_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.h:104
google::protobuf::compiler::java::JAVATYPE_BYTES
@ JAVATYPE_BYTES
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.h:212
google::protobuf::compiler::java::GetType
FieldDescriptor::Type GetType(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.cc:315
java
google::protobuf::compiler::java::ImmutableExtensionGenerator::name_resolver_
ClassNameResolver * name_resolver_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.h:103
google::protobuf::compiler::java::FieldTypeName
const char * FieldTypeName(FieldDescriptor::Type field_type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.cc:443
google::protobuf::compiler::java::ImmutableExtensionGenerator::~ImmutableExtensionGenerator
virtual ~ImmutableExtensionGenerator()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.cc:60
google::protobuf::FieldDescriptor::index
int index() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2081
google::protobuf::compiler::java::JAVATYPE_ENUM
@ JAVATYPE_ENUM
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.h:213
google::protobuf::compiler::java::ImmutableExtensionGenerator::GenerateRegistrationCode
virtual int GenerateRegistrationCode(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.cc:162
google::protobuf::compiler::java::FieldConstantName
std::string FieldConstantName(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.cc:309
google::protobuf::compiler::java::DefaultValue
std::string DefaultValue(const FieldDescriptor *field, bool immutable, ClassNameResolver *name_resolver)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.cc:499
google::protobuf::compiler::java::ImmutableExtensionGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_extension.h:102
google::protobuf::compiler::java::JAVATYPE_MESSAGE
@ JAVATYPE_MESSAGE
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.h:214
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
google::protobuf::compiler::java::JAVATYPE_STRING
@ JAVATYPE_STRING
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.h:211
google::protobuf::compiler::java::JavaType
JavaType
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.h:205
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:09