protobuf/src/google/protobuf/compiler/java/immutable/extension.cc

157 lines
5.7 KiB
C++
Raw Normal View History

2008-07-09 19:12:20 -07:00
// Protocol Buffers - Google's data interchange format
Integrate changes from internal Google-internal branch. General * License changed from Apache 2.0 to New BSD. * It is now possible to define custom "options", which are basically annotations which may be placed on definitions in a .proto file. For example, you might define a field option called "foo" like so: import "google/protobuf/descriptor.proto" extend google.protobuf.FieldOptions { optional string foo = 12345; } Then you annotate a field using the "foo" option: message MyMessage { optional int32 some_field = 1 [(foo) = "bar"] } The value of this option is then visible via the message's Descriptor: const FieldDescriptor* field = MyMessage::descriptor()->FindFieldByName("some_field"); assert(field->options().GetExtension(foo) == "bar"); This feature has been implemented and tested in C++ and Java. Other languages may or may not need to do extra work to support custom options, depending on how they construct descriptors. C++ * Fixed some GCC warnings that only occur when using -pedantic. * Improved static initialization code, making ordering more predictable among other things. * TextFormat will no longer accept messages which contain multiple instances of a singular field. Previously, the latter instance would overwrite the former. * Now works on systems that don't have hash_map. Python * Strings now use the "unicode" type rather than the "str" type. String fields may still be assigned ASCII "str" values; they will automatically be converted. * Adding a property to an object representing a repeated field now raises an exception. For example: # No longer works (and never should have). message.some_repeated_field.foo = 1
2008-09-24 13:31:01 -07:00
// Copyright 2008 Google Inc. All rights reserved.
2008-07-09 19:12:20 -07:00
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
2008-07-09 19:12:20 -07:00
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include "google/protobuf/compiler/java/immutable/extension.h"
2014-07-17 17:47:59 -07:00
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/io/printer.h"
2008-07-09 19:12:20 -07:00
// Must be last.
#include "google/protobuf/port_def.inc"
2008-07-09 19:12:20 -07:00
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
2014-07-17 17:47:59 -07:00
ImmutableExtensionGenerator::ImmutableExtensionGenerator(
const FieldDescriptor* descriptor, Context* context)
: descriptor_(descriptor),
name_resolver_(context->GetNameResolver()),
context_(context) {
if (descriptor_->extension_scope() != nullptr) {
2019-04-09 06:23:32 -07:00
scope_ =
name_resolver_->GetImmutableClassName(descriptor_->extension_scope());
Integrate changes from internal Google-internal branch. General * License changed from Apache 2.0 to New BSD. * It is now possible to define custom "options", which are basically annotations which may be placed on definitions in a .proto file. For example, you might define a field option called "foo" like so: import "google/protobuf/descriptor.proto" extend google.protobuf.FieldOptions { optional string foo = 12345; } Then you annotate a field using the "foo" option: message MyMessage { optional int32 some_field = 1 [(foo) = "bar"] } The value of this option is then visible via the message's Descriptor: const FieldDescriptor* field = MyMessage::descriptor()->FindFieldByName("some_field"); assert(field->options().GetExtension(foo) == "bar"); This feature has been implemented and tested in C++ and Java. Other languages may or may not need to do extra work to support custom options, depending on how they construct descriptors. C++ * Fixed some GCC warnings that only occur when using -pedantic. * Improved static initialization code, making ordering more predictable among other things. * TextFormat will no longer accept messages which contain multiple instances of a singular field. Previously, the latter instance would overwrite the former. * Now works on systems that don't have hash_map. Python * Strings now use the "unicode" type rather than the "str" type. String fields may still be assigned ASCII "str" values; they will automatically be converted. * Adding a property to an object representing a repeated field now raises an exception. For example: # No longer works (and never should have). message.some_repeated_field.foo = 1
2008-09-24 13:31:01 -07:00
} else {
2014-07-17 17:47:59 -07:00
scope_ = name_resolver_->GetImmutableClassName(descriptor_->file());
Integrate changes from internal Google-internal branch. General * License changed from Apache 2.0 to New BSD. * It is now possible to define custom "options", which are basically annotations which may be placed on definitions in a .proto file. For example, you might define a field option called "foo" like so: import "google/protobuf/descriptor.proto" extend google.protobuf.FieldOptions { optional string foo = 12345; } Then you annotate a field using the "foo" option: message MyMessage { optional int32 some_field = 1 [(foo) = "bar"] } The value of this option is then visible via the message's Descriptor: const FieldDescriptor* field = MyMessage::descriptor()->FindFieldByName("some_field"); assert(field->options().GetExtension(foo) == "bar"); This feature has been implemented and tested in C++ and Java. Other languages may or may not need to do extra work to support custom options, depending on how they construct descriptors. C++ * Fixed some GCC warnings that only occur when using -pedantic. * Improved static initialization code, making ordering more predictable among other things. * TextFormat will no longer accept messages which contain multiple instances of a singular field. Previously, the latter instance would overwrite the former. * Now works on systems that don't have hash_map. Python * Strings now use the "unicode" type rather than the "str" type. String fields may still be assigned ASCII "str" values; they will automatically be converted. * Adding a property to an object representing a repeated field now raises an exception. For example: # No longer works (and never should have). message.some_repeated_field.foo = 1
2008-09-24 13:31:01 -07:00
}
}
2008-07-09 19:12:20 -07:00
2014-07-17 17:47:59 -07:00
ImmutableExtensionGenerator::~ImmutableExtensionGenerator() {}
2008-07-09 19:12:20 -07:00
// Initializes the vars referenced in the generated code templates.
void ExtensionGenerator::InitTemplateVars(
const FieldDescriptor* descriptor, const std::string& scope, bool immutable,
ClassNameResolver* name_resolver,
absl::flat_hash_map<absl::string_view, std::string>* vars_pointer,
Context* context) {
absl::flat_hash_map<absl::string_view, std::string>& vars = *vars_pointer;
vars["scope"] = scope;
vars["name"] = UnderscoresToCamelCaseCheckReserved(descriptor);
2014-07-17 17:47:59 -07:00
vars["containing_type"] =
name_resolver->GetClassName(descriptor->containing_type(), immutable);
vars["number"] = absl::StrCat(descriptor->number());
vars["constant_name"] = FieldConstantName(descriptor);
vars["index"] = absl::StrCat(descriptor->index());
2019-04-09 06:23:32 -07:00
vars["default"] = descriptor->is_repeated()
? ""
: DefaultValue(descriptor, immutable, name_resolver,
context->options());
vars["type_constant"] = std::string(FieldTypeName(GetType(descriptor)));
vars["packed"] = descriptor->is_packed() ? "true" : "false";
vars["enum_map"] = "null";
vars["prototype"] = "null";
2008-07-09 19:12:20 -07:00
JavaType java_type = GetJavaType(descriptor);
std::string singular_type;
2008-07-09 19:12:20 -07:00
switch (java_type) {
case JAVATYPE_MESSAGE:
2019-04-09 06:23:32 -07:00
singular_type =
name_resolver->GetClassName(descriptor->message_type(), immutable);
vars["prototype"] = absl::StrCat(singular_type, ".getDefaultInstance()");
2008-07-09 19:12:20 -07:00
break;
case JAVATYPE_ENUM:
2019-04-09 06:23:32 -07:00
singular_type =
name_resolver->GetClassName(descriptor->enum_type(), immutable);
vars["enum_map"] = absl::StrCat(singular_type, ".internalGetValueMap()");
2008-07-09 19:12:20 -07:00
break;
2014-07-17 17:47:59 -07:00
case JAVATYPE_STRING:
singular_type = "java.lang.String";
break;
case JAVATYPE_BYTES:
singular_type = immutable ? "com.google.protobuf.ByteString" : "byte[]";
break;
2008-07-09 19:12:20 -07:00
default:
singular_type = std::string(BoxedPrimitiveTypeName(java_type));
2008-07-09 19:12:20 -07:00
break;
}
2019-04-09 06:23:32 -07:00
vars["type"] = descriptor->is_repeated()
? absl::StrCat("java.util.List<", singular_type, ">")
2019-04-09 06:23:32 -07:00
: singular_type;
vars["singular_type"] = singular_type;
2008-07-09 19:12:20 -07:00
}
2014-07-17 17:47:59 -07:00
void ImmutableExtensionGenerator::Generate(io::Printer* printer) {
absl::flat_hash_map<absl::string_view, std::string> vars;
2014-07-17 17:47:59 -07:00
const bool kUseImmutableNames = true;
InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
&vars, context_);
2019-04-09 06:23:32 -07:00
printer->Print(vars, "public static final int $constant_name$ = $number$;\n");
Integrate changes from internal Google-internal branch. General * License changed from Apache 2.0 to New BSD. * It is now possible to define custom "options", which are basically annotations which may be placed on definitions in a .proto file. For example, you might define a field option called "foo" like so: import "google/protobuf/descriptor.proto" extend google.protobuf.FieldOptions { optional string foo = 12345; } Then you annotate a field using the "foo" option: message MyMessage { optional int32 some_field = 1 [(foo) = "bar"] } The value of this option is then visible via the message's Descriptor: const FieldDescriptor* field = MyMessage::descriptor()->FindFieldByName("some_field"); assert(field->options().GetExtension(foo) == "bar"); This feature has been implemented and tested in C++ and Java. Other languages may or may not need to do extra work to support custom options, depending on how they construct descriptors. C++ * Fixed some GCC warnings that only occur when using -pedantic. * Improved static initialization code, making ordering more predictable among other things. * TextFormat will no longer accept messages which contain multiple instances of a singular field. Previously, the latter instance would overwrite the former. * Now works on systems that don't have hash_map. Python * Strings now use the "unicode" type rather than the "str" type. String fields may still be assigned ASCII "str" values; they will automatically be converted. * Adding a property to an object representing a repeated field now raises an exception. For example: # No longer works (and never should have). message.some_repeated_field.foo = 1
2008-09-24 13:31:01 -07:00
WriteFieldDocComment(printer, descriptor_, context_->options());
if (descriptor_->extension_scope() == nullptr) {
2016-03-30 11:39:59 -07:00
// Non-nested
printer->Print(
vars,
"public static final\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $containing_type$,\n"
" $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
" .newFileScopedGeneratedExtension(\n"
" $singular_type$.class,\n"
" $prototype$);\n");
Integrate changes from internal Google-internal branch. General * License changed from Apache 2.0 to New BSD. * It is now possible to define custom "options", which are basically annotations which may be placed on definitions in a .proto file. For example, you might define a field option called "foo" like so: import "google/protobuf/descriptor.proto" extend google.protobuf.FieldOptions { optional string foo = 12345; } Then you annotate a field using the "foo" option: message MyMessage { optional int32 some_field = 1 [(foo) = "bar"] } The value of this option is then visible via the message's Descriptor: const FieldDescriptor* field = MyMessage::descriptor()->FindFieldByName("some_field"); assert(field->options().GetExtension(foo) == "bar"); This feature has been implemented and tested in C++ and Java. Other languages may or may not need to do extra work to support custom options, depending on how they construct descriptors. C++ * Fixed some GCC warnings that only occur when using -pedantic. * Improved static initialization code, making ordering more predictable among other things. * TextFormat will no longer accept messages which contain multiple instances of a singular field. Previously, the latter instance would overwrite the former. * Now works on systems that don't have hash_map. Python * Strings now use the "unicode" type rather than the "str" type. String fields may still be assigned ASCII "str" values; they will automatically be converted. * Adding a property to an object representing a repeated field now raises an exception. For example: # No longer works (and never should have). message.some_repeated_field.foo = 1
2008-09-24 13:31:01 -07:00
} else {
2016-03-30 11:39:59 -07:00
// Nested
printer->Print(
vars,
"public static final\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $containing_type$,\n"
" $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
" .newMessageScopedGeneratedExtension(\n"
" $scope$.getDefaultInstance(),\n"
" $index$,\n"
" $singular_type$.class,\n"
" $prototype$);\n");
Integrate changes from internal Google-internal branch. General * License changed from Apache 2.0 to New BSD. * It is now possible to define custom "options", which are basically annotations which may be placed on definitions in a .proto file. For example, you might define a field option called "foo" like so: import "google/protobuf/descriptor.proto" extend google.protobuf.FieldOptions { optional string foo = 12345; } Then you annotate a field using the "foo" option: message MyMessage { optional int32 some_field = 1 [(foo) = "bar"] } The value of this option is then visible via the message's Descriptor: const FieldDescriptor* field = MyMessage::descriptor()->FindFieldByName("some_field"); assert(field->options().GetExtension(foo) == "bar"); This feature has been implemented and tested in C++ and Java. Other languages may or may not need to do extra work to support custom options, depending on how they construct descriptors. C++ * Fixed some GCC warnings that only occur when using -pedantic. * Improved static initialization code, making ordering more predictable among other things. * TextFormat will no longer accept messages which contain multiple instances of a singular field. Previously, the latter instance would overwrite the former. * Now works on systems that don't have hash_map. Python * Strings now use the "unicode" type rather than the "str" type. String fields may still be assigned ASCII "str" values; they will automatically be converted. * Adding a property to an object representing a repeated field now raises an exception. For example: # No longer works (and never should have). message.some_repeated_field.foo = 1
2008-09-24 13:31:01 -07:00
}
printer->Annotate("name", descriptor_);
Integrate changes from internal Google-internal branch. General * License changed from Apache 2.0 to New BSD. * It is now possible to define custom "options", which are basically annotations which may be placed on definitions in a .proto file. For example, you might define a field option called "foo" like so: import "google/protobuf/descriptor.proto" extend google.protobuf.FieldOptions { optional string foo = 12345; } Then you annotate a field using the "foo" option: message MyMessage { optional int32 some_field = 1 [(foo) = "bar"] } The value of this option is then visible via the message's Descriptor: const FieldDescriptor* field = MyMessage::descriptor()->FindFieldByName("some_field"); assert(field->options().GetExtension(foo) == "bar"); This feature has been implemented and tested in C++ and Java. Other languages may or may not need to do extra work to support custom options, depending on how they construct descriptors. C++ * Fixed some GCC warnings that only occur when using -pedantic. * Improved static initialization code, making ordering more predictable among other things. * TextFormat will no longer accept messages which contain multiple instances of a singular field. Previously, the latter instance would overwrite the former. * Now works on systems that don't have hash_map. Python * Strings now use the "unicode" type rather than the "str" type. String fields may still be assigned ASCII "str" values; they will automatically be converted. * Adding a property to an object representing a repeated field now raises an exception. For example: # No longer works (and never should have). message.some_repeated_field.foo = 1
2008-09-24 13:31:01 -07:00
}
int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode(
io::Printer* printer) {
int bytecode_estimate = 0;
if (descriptor_->extension_scope() == nullptr) {
2016-03-30 11:39:59 -07:00
// Only applies to non-nested extensions.
printer->Print(
"$name$.internalInit(descriptor.getExtensions().get($index$));\n",
"name", UnderscoresToCamelCaseCheckReserved(descriptor_), "index",
absl::StrCat(descriptor_->index()));
bytecode_estimate += 21;
}
return bytecode_estimate;
}
int ImmutableExtensionGenerator::GenerateRegistrationCode(
2014-07-17 17:47:59 -07:00
io::Printer* printer) {
2019-04-09 06:23:32 -07:00
printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name",
UnderscoresToCamelCaseCheckReserved(descriptor_));
return 7;
Integrate changes from internal Google-internal branch. General * License changed from Apache 2.0 to New BSD. * It is now possible to define custom "options", which are basically annotations which may be placed on definitions in a .proto file. For example, you might define a field option called "foo" like so: import "google/protobuf/descriptor.proto" extend google.protobuf.FieldOptions { optional string foo = 12345; } Then you annotate a field using the "foo" option: message MyMessage { optional int32 some_field = 1 [(foo) = "bar"] } The value of this option is then visible via the message's Descriptor: const FieldDescriptor* field = MyMessage::descriptor()->FindFieldByName("some_field"); assert(field->options().GetExtension(foo) == "bar"); This feature has been implemented and tested in C++ and Java. Other languages may or may not need to do extra work to support custom options, depending on how they construct descriptors. C++ * Fixed some GCC warnings that only occur when using -pedantic. * Improved static initialization code, making ordering more predictable among other things. * TextFormat will no longer accept messages which contain multiple instances of a singular field. Previously, the latter instance would overwrite the former. * Now works on systems that don't have hash_map. Python * Strings now use the "unicode" type rather than the "str" type. String fields may still be assigned ASCII "str" values; they will automatically be converted. * Adding a property to an object representing a repeated field now raises an exception. For example: # No longer works (and never should have). message.some_repeated_field.foo = 1
2008-09-24 13:31:01 -07:00
}
2008-07-09 19:12:20 -07:00
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"