PROTOBUF_SYNC_PIPER
pull/10399/head
Mike Kruskal 2 years ago
parent ca4b0630a6
commit bbed70881e
  1. 61
      ruby/ext/google/protobuf_c/message.c
  2. 5
      ruby/lib/google/protobuf/message_exts.rb
  3. 2
      src/google/protobuf/compiler/java/context.cc
  4. 1
      src/google/protobuf/compiler/java/doc_comment.h
  5. 39
      src/google/protobuf/compiler/java/enum.cc
  6. 10
      src/google/protobuf/compiler/java/enum_field.cc
  7. 37
      src/google/protobuf/compiler/java/enum_field_lite.cc
  8. 60
      src/google/protobuf/compiler/java/enum_lite.cc
  9. 11
      src/google/protobuf/compiler/java/extension.cc
  10. 10
      src/google/protobuf/compiler/java/extension.h
  11. 6
      src/google/protobuf/compiler/java/extension_lite.cc
  12. 1
      src/google/protobuf/compiler/java/extension_lite.h
  13. 25
      src/google/protobuf/compiler/java/file.cc
  14. 2
      src/google/protobuf/compiler/java/generator.cc
  15. 2
      src/google/protobuf/compiler/java/generator.h
  16. 27
      src/google/protobuf/compiler/java/helpers.cc
  17. 25
      src/google/protobuf/compiler/java/helpers.h
  18. 180
      src/google/protobuf/compiler/java/map_field.cc
  19. 1
      src/google/protobuf/compiler/java/map_field.h
  20. 151
      src/google/protobuf/compiler/java/map_field_lite.cc
  21. 54
      src/google/protobuf/compiler/java/message.cc
  22. 162
      src/google/protobuf/compiler/java/message_builder.cc
  23. 17
      src/google/protobuf/compiler/java/message_field.cc
  24. 1
      src/google/protobuf/compiler/java/message_field.h
  25. 21
      src/google/protobuf/compiler/java/message_field_lite.cc
  26. 2
      src/google/protobuf/compiler/java/message_field_lite.h
  27. 45
      src/google/protobuf/compiler/java/message_lite.cc
  28. 20
      src/google/protobuf/compiler/java/name_resolver.cc
  29. 13
      src/google/protobuf/compiler/java/name_resolver.h
  30. 5
      src/google/protobuf/compiler/java/names.h
  31. 4
      src/google/protobuf/compiler/java/options.h
  32. 17
      src/google/protobuf/compiler/java/primitive_field.cc
  33. 29
      src/google/protobuf/compiler/java/primitive_field_lite.cc
  34. 1
      src/google/protobuf/compiler/java/primitive_field_lite.h
  35. 4
      src/google/protobuf/compiler/java/service.cc
  36. 29
      src/google/protobuf/compiler/java/shared_code_generator.cc
  37. 13
      src/google/protobuf/compiler/java/string_field.cc
  38. 52
      src/google/protobuf/compiler/java/string_field_lite.cc
  39. 2
      src/google/protobuf/compiler/java/string_field_lite.h
  40. 2
      src/google/protobuf/port_def.inc
  41. 1
      src/google/protobuf/port_undef.inc

@ -37,6 +37,7 @@
#include "repeated_field.h"
static VALUE cParseError = Qnil;
static VALUE cAbstractMessage = Qnil;
static ID descriptor_instancevar_interned;
static VALUE initialize_rb_class_with_no_args(VALUE klass) {
@ -1201,36 +1202,8 @@ VALUE build_class_from_descriptor(VALUE descriptor) {
klass = rb_define_class_id(
// Docs say this parameter is ignored. User will assign return value to
// their own toplevel constant class name.
rb_intern("Message"), rb_cObject);
rb_intern("Message"), cAbstractMessage);
rb_ivar_set(klass, descriptor_instancevar_interned, descriptor);
rb_define_alloc_func(klass, Message_alloc);
rb_require("google/protobuf/message_exts");
rb_include_module(klass, rb_eval_string("::Google::Protobuf::MessageExts"));
rb_extend_object(
klass, rb_eval_string("::Google::Protobuf::MessageExts::ClassMethods"));
rb_define_method(klass, "method_missing", Message_method_missing, -1);
rb_define_method(klass, "respond_to_missing?", Message_respond_to_missing,
-1);
rb_define_method(klass, "initialize", Message_initialize, -1);
rb_define_method(klass, "dup", Message_dup, 0);
// Also define #clone so that we don't inherit Object#clone.
rb_define_method(klass, "clone", Message_dup, 0);
rb_define_method(klass, "==", Message_eq, 1);
rb_define_method(klass, "eql?", Message_eq, 1);
rb_define_method(klass, "freeze", Message_freeze, 0);
rb_define_method(klass, "hash", Message_hash, 0);
rb_define_method(klass, "to_h", Message_to_h, 0);
rb_define_method(klass, "inspect", Message_inspect, 0);
rb_define_method(klass, "to_s", Message_inspect, 0);
rb_define_method(klass, "[]", Message_index, 1);
rb_define_method(klass, "[]=", Message_index_set, 2);
rb_define_singleton_method(klass, "decode", Message_decode, -1);
rb_define_singleton_method(klass, "encode", Message_encode, -1);
rb_define_singleton_method(klass, "decode_json", Message_decode_json, -1);
rb_define_singleton_method(klass, "encode_json", Message_encode_json, -1);
rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
return klass;
}
@ -1392,8 +1365,38 @@ const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m,
return self->msg;
}
static void Message_define_class(VALUE klass) {
rb_define_alloc_func(klass, Message_alloc);
rb_require("google/protobuf/message_exts");
rb_define_method(klass, "method_missing", Message_method_missing, -1);
rb_define_method(klass, "respond_to_missing?", Message_respond_to_missing,
-1);
rb_define_method(klass, "initialize", Message_initialize, -1);
rb_define_method(klass, "dup", Message_dup, 0);
// Also define #clone so that we don't inherit Object#clone.
rb_define_method(klass, "clone", Message_dup, 0);
rb_define_method(klass, "==", Message_eq, 1);
rb_define_method(klass, "eql?", Message_eq, 1);
rb_define_method(klass, "freeze", Message_freeze, 0);
rb_define_method(klass, "hash", Message_hash, 0);
rb_define_method(klass, "to_h", Message_to_h, 0);
rb_define_method(klass, "inspect", Message_inspect, 0);
rb_define_method(klass, "to_s", Message_inspect, 0);
rb_define_method(klass, "[]", Message_index, 1);
rb_define_method(klass, "[]=", Message_index_set, 2);
rb_define_singleton_method(klass, "decode", Message_decode, -1);
rb_define_singleton_method(klass, "encode", Message_encode, -1);
rb_define_singleton_method(klass, "decode_json", Message_decode_json, -1);
rb_define_singleton_method(klass, "encode_json", Message_encode_json, -1);
rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
}
void Message_register(VALUE protobuf) {
cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
cAbstractMessage = rb_define_class_under(protobuf, "AbstractMessage", rb_cObject);
Message_define_class(cAbstractMessage);
rb_gc_register_address(&cAbstractMessage);
// Ruby-interned string: "descriptor". We use this identifier to store an
// instance variable on message classes we create in order to link them back

@ -49,5 +49,10 @@ module Google
end
end
class AbstractMessage
include MessageExts
extend MessageExts::ClassMethods
end
private_constant :AbstractMessage
end
end

@ -43,7 +43,7 @@ namespace compiler {
namespace java {
Context::Context(const FileDescriptor* file, const Options& options)
: name_resolver_(new ClassNameResolver), options_(options) {
: name_resolver_(new ClassNameResolver(options)), options_(options) {
InitializeFieldGeneratorInfo(file);
}

@ -96,6 +96,7 @@ void WriteMethodDocComment(io::Printer* printer,
const MethodDescriptor* method);
// Exposed for testing only.
// Also called by proto1-Java code generator.
PROTOC_EXPORT std::string EscapeJavadoc(const std::string& input);
} // namespace java

@ -80,6 +80,10 @@ EnumGenerator::~EnumGenerator() {}
void EnumGenerator::Generate(io::Printer* printer) {
WriteEnumDocComment(printer, descriptor_);
MaybePrintGeneratedAnnotation(context_, printer, descriptor_, immutable_api_);
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoNonnullApi\n");
}
printer->Print(
"$deprecation$public enum $classname$\n"
" implements com.google.protobuf.ProtocolMessageEnum {\n",
@ -181,23 +185,32 @@ void EnumGenerator::Generate(io::Printer* printer) {
printer->Print(
" return value;\n"
"}\n"
"\n"
"/**\n"
" * @param value The numeric wire value of the corresponding enum "
"entry.\n"
" * @return The enum associated with the given numeric wire value.\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $classname$ valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n"
"\n");
if (context_->options().opensource_runtime) {
printer->Print(
"/**\n"
" * @param value The numeric wire value of the corresponding enum "
"entry.\n"
" * @return The enum associated with the given numeric wire value.\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $classname$ valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n",
"classname", descriptor_->name());
}
printer->Print(
"/**\n"
" * @param value The numeric wire value of the corresponding enum "
"entry.\n"
" * @return The enum associated with the given numeric wire value.\n"
" */\n"
" */\n");
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoMethodMayReturnNull\n");
}
printer->Print(
"public static $classname$ forNumber(int value) {\n"
" switch (value) {\n",
"classname", descriptor_->name());

@ -60,7 +60,8 @@ namespace {
void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex,
int builderBitIndex, const FieldGeneratorInfo* info,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* variables) {
std::map<std::string, std::string>* variables,
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
(*variables)["type"] =
@ -68,7 +69,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["kt_type"] = EscapeKotlinKeywords((*variables)["type"]);
(*variables)["mutable_type"] =
name_resolver->GetMutableClassName(descriptor->enum_type());
(*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_number"] =
StrCat(descriptor->default_value_enum()->number());
(*variables)["tag"] = StrCat(
@ -142,7 +144,7 @@ ImmutableEnumFieldGenerator::ImmutableEnumFieldGenerator(
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
SetEnumVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor), name_resolver_,
&variables_);
&variables_, context);
}
ImmutableEnumFieldGenerator::~ImmutableEnumFieldGenerator() {}
@ -636,7 +638,7 @@ RepeatedImmutableEnumFieldGenerator::RepeatedImmutableEnumFieldGenerator(
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
SetEnumVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor), name_resolver_,
&variables_);
&variables_, context);
}
RepeatedImmutableEnumFieldGenerator::~RepeatedImmutableEnumFieldGenerator() {}

@ -68,7 +68,8 @@ bool EnableExperimentalRuntimeForLite() {
void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex,
int builderBitIndex, const FieldGeneratorInfo* info,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* variables) {
std::map<std::string, std::string>* variables,
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
(*variables)["type"] =
@ -76,7 +77,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["kt_type"] = EscapeKotlinKeywords((*variables)["type"]);
(*variables)["mutable_type"] =
name_resolver->GetMutableClassName(descriptor->enum_type());
(*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_number"] =
StrCat(descriptor->default_value_enum()->number());
(*variables)["tag"] = StrCat(
@ -95,6 +97,12 @@ void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["required"] = descriptor->is_required() ? "true" : "false";
if (HasHasbit(descriptor)) {
if (!context->options().opensource_runtime) {
(*variables)["bit_field_id"] = StrCat(messageBitIndex / 32);
(*variables)["bit_field_name"] = GetBitFieldNameForBit(messageBitIndex);
(*variables)["bit_field_mask"] =
StrCat(1 << (messageBitIndex % 32));
}
// For singular messages and builders, one bit is used for the hasField bit.
(*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
@ -142,7 +150,7 @@ ImmutableEnumFieldLiteGenerator::ImmutableEnumFieldLiteGenerator(
name_resolver_(context->GetNameResolver()) {
SetEnumVariables(descriptor, messageBitIndex, 0,
context->GetFieldGeneratorInfo(descriptor), name_resolver_,
&variables_);
&variables_, context);
}
ImmutableEnumFieldLiteGenerator::~ImmutableEnumFieldLiteGenerator() {}
@ -169,6 +177,20 @@ void ImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers(
void ImmutableEnumFieldLiteGenerator::GenerateMembers(
io::Printer* printer) const {
if (!context_->options().opensource_runtime) {
printer->Print(
variables_,
"@com.google.protobuf.ProtoField(\n"
" fieldNumber=$number$,\n"
" type=com.google.protobuf.FieldType.$annotation_field_type$,\n"
" isRequired=$required$)\n");
if (HasHazzer(descriptor_)) {
printer->Print(variables_,
"@com.google.protobuf.ProtoPresenceCheckedField(\n"
" presenceBitsId=$bit_field_id$,\n"
" mask=$bit_field_mask$)\n");
}
}
printer->Print(variables_, "private int $name$_;\n");
PrintExtraFieldInfo(variables_, printer);
if (HasHazzer(descriptor_)) {
@ -514,7 +536,7 @@ RepeatedImmutableEnumFieldLiteGenerator::
name_resolver_(context->GetNameResolver()) {
SetEnumVariables(descriptor, messageBitIndex, 0,
context->GetFieldGeneratorInfo(descriptor), name_resolver_,
&variables_);
&variables_, context);
}
RepeatedImmutableEnumFieldLiteGenerator::
@ -550,6 +572,13 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers(
void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers(
io::Printer* printer) const {
if (!context_->options().opensource_runtime) {
printer->Print(
variables_,
"@com.google.protobuf.ProtoField(\n"
" fieldNumber=$number$,\n"
" type=com.google.protobuf.FieldType.$annotation_field_type$)\n");
}
printer->Print(
variables_,
"private com.google.protobuf.Internal.IntList $name$_;\n"

@ -153,17 +153,26 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
printer->Print(
" return value;\n"
"}\n"
"\n"
"/**\n"
" * @param value The number of the enum to look for.\n"
" * @return The enum associated with the given number.\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $classname$ valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n"
"\n");
if (context_->options().opensource_runtime) {
printer->Print(
"/**\n"
" * @param value The number of the enum to look for.\n"
" * @return The enum associated with the given number.\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $classname$ valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n",
"classname", descriptor_->name());
}
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoMethodMayReturnNull\n");
}
printer->Print(
"public static $classname$ forNumber(int value) {\n"
" switch (value) {\n",
"classname", descriptor_->name());
@ -212,6 +221,35 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
" };\n"
"\n",
"classname", descriptor_->name());
if (!context_->options().opensource_runtime) {
printer->Print(
"/**\n"
" * Override of toString that prints the number and name.\n"
" * This is primarily intended as a developer aid.\n"
" *\n"
" * <p>NOTE: This implementation is liable to change in the future,\n"
" * and should not be relied on in code.\n"
" */\n"
"@java.lang.Override\n"
"public java.lang.String toString() {\n"
" StringBuilder result = new StringBuilder(\"<\");\n"
" result.append(getClass().getName()).append('@')\n"
" .append(java.lang.Integer.toHexString(\n"
" java.lang.System.identityHashCode(this)));\n");
if (SupportUnknownEnumValue(descriptor_->file())) {
printer->Print(
" if (this != UNRECOGNIZED) {\n"
" result.append(\" number=\").append(getNumber());\n"
" }\n");
} else {
printer->Print(" result.append(\" number=\").append(getNumber());\n");
}
printer->Print(
" return result.append(\" name=\")\n"
" .append(name()).append('>').toString();\n"
"}\n"
"\n");
}
printer->Print(
"private final int value;\n\n"

@ -51,7 +51,9 @@ namespace java {
ImmutableExtensionGenerator::ImmutableExtensionGenerator(
const FieldDescriptor* descriptor, Context* context)
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
: descriptor_(descriptor),
name_resolver_(context->GetNameResolver()),
context_(context) {
if (descriptor_->extension_scope() != NULL) {
scope_ =
name_resolver_->GetImmutableClassName(descriptor_->extension_scope());
@ -66,7 +68,7 @@ ImmutableExtensionGenerator::~ImmutableExtensionGenerator() {}
void ExtensionGenerator::InitTemplateVars(
const FieldDescriptor* descriptor, const std::string& scope, bool immutable,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* vars_pointer) {
std::map<std::string, std::string>* vars_pointer, Context* context) {
std::map<std::string, std::string>& vars = *vars_pointer;
vars["scope"] = scope;
vars["name"] = UnderscoresToCamelCaseCheckReserved(descriptor);
@ -77,7 +79,8 @@ void ExtensionGenerator::InitTemplateVars(
vars["index"] = StrCat(descriptor->index());
vars["default"] = descriptor->is_repeated()
? ""
: DefaultValue(descriptor, immutable, name_resolver);
: DefaultValue(descriptor, immutable, name_resolver,
context->options());
vars["type_constant"] = FieldTypeName(GetType(descriptor));
vars["packed"] = descriptor->is_packed() ? "true" : "false";
vars["enum_map"] = "null";
@ -116,7 +119,7 @@ void ImmutableExtensionGenerator::Generate(io::Printer* printer) {
std::map<std::string, std::string> vars;
const bool kUseImmutableNames = true;
InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
&vars);
&vars, context_);
printer->Print(vars, "public static final int $constant_name$ = $number$;\n");
WriteFieldDocComment(printer, descriptor_);

@ -79,10 +79,11 @@ class ExtensionGenerator {
virtual int GenerateRegistrationCode(io::Printer* printer) = 0;
protected:
static void InitTemplateVars(
const FieldDescriptor* descriptor, const std::string& scope,
bool immutable, ClassNameResolver* name_resolver,
std::map<std::string, std::string>* vars_pointer);
static void InitTemplateVars(const FieldDescriptor* descriptor,
const std::string& scope, bool immutable,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* vars_pointer,
Context* context);
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionGenerator);
@ -102,6 +103,7 @@ class ImmutableExtensionGenerator : public ExtensionGenerator {
const FieldDescriptor* descriptor_;
ClassNameResolver* name_resolver_;
std::string scope_;
Context* context_;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableExtensionGenerator);

@ -47,7 +47,9 @@ namespace java {
ImmutableExtensionLiteGenerator::ImmutableExtensionLiteGenerator(
const FieldDescriptor* descriptor, Context* context)
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
: descriptor_(descriptor),
name_resolver_(context->GetNameResolver()),
context_(context) {
if (descriptor_->extension_scope() != NULL) {
scope_ =
name_resolver_->GetImmutableClassName(descriptor_->extension_scope());
@ -62,7 +64,7 @@ void ImmutableExtensionLiteGenerator::Generate(io::Printer* printer) {
std::map<std::string, std::string> vars;
const bool kUseImmutableNames = true;
InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
&vars);
&vars, context_);
printer->Print(vars, "public static final int $constant_name$ = $number$;\n");
WriteFieldDocComment(printer, descriptor_);

@ -63,6 +63,7 @@ class ImmutableExtensionLiteGenerator : public ExtensionGenerator {
const FieldDescriptor* descriptor_;
ClassNameResolver* name_resolver_;
std::string scope_;
Context* context_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableExtensionLiteGenerator);
};

@ -188,7 +188,7 @@ void MaybeRestartJavaMethod(io::Printer* printer, int* bytecode_estimate,
FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options,
bool immutable_api)
: file_(file),
java_package_(FileJavaPackage(file, immutable_api)),
java_package_(FileJavaPackage(file, immutable_api, options)),
message_generators_(file->message_type_count()),
extension_generators_(file->extension_count()),
context_(new Context(file, options)),
@ -196,7 +196,7 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options,
options_(options),
immutable_api_(immutable_api) {
classname_ = name_resolver_->GetFileClassName(file, immutable_api);
generator_factory_.reset(new ImmutableGeneratorFactory(context_.get()));
generator_factory_.reset(new ImmutableGeneratorFactory(context_.get()));
for (int i = 0; i < file_->message_type_count(); ++i) {
message_generators_[i].reset(
generator_factory_->NewMessageGenerator(file_->message_type(i)));
@ -271,8 +271,12 @@ void FileGenerator::Generate(io::Printer* printer) {
"package", java_package_);
}
PrintGeneratedAnnotation(
printer, '$', options_.annotate_code ? classname_ + ".java.pb.meta" : "");
printer, '$', options_.annotate_code ? classname_ + ".java.pb.meta" : "",
options_);
if (!options_.opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoNonnullApi\n");
}
printer->Print(
"$deprecation$public final class $classname$ {\n"
" private $ctor$() {}\n",
@ -401,11 +405,14 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable(
" descriptor;\n"
"static {\n",
// TODO(dweis): Mark this as final.
"final", "");
"final", options_.opensource_runtime ? "" : "final");
printer->Indent();
SharedCodeGenerator shared_code_generator(file_, options_);
shared_code_generator.GenerateDescriptors(printer);
if (options_.opensource_runtime) {
SharedCodeGenerator shared_code_generator(file_, options_);
shared_code_generator.GenerateDescriptors(printer);
} else {
}
int bytecode_estimate = 0;
int method_num = 0;
@ -499,8 +506,8 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(
printer->Print(
"descriptor = $immutable_package$.$descriptor_classname$.descriptor;\n",
"immutable_package", FileJavaPackage(file_, true), "descriptor_classname",
name_resolver_->GetDescriptorClassName(file_));
"immutable_package", FileJavaPackage(file_, true, options_),
"descriptor_classname", name_resolver_->GetDescriptorClassName(file_));
for (int i = 0; i < file_->message_type_count(); i++) {
message_generators_[i]->GenerateStaticVariableInitializers(printer);
@ -547,7 +554,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(
scope = name_resolver_->GetMutableClassName(field->extension_scope()) +
".getDescriptor()";
} else {
scope = FileJavaPackage(field->file(), true) + "." +
scope = FileJavaPackage(field->file(), true, options_) + "." +
name_resolver_->GetDescriptorClassName(field->file()) +
".descriptor";
}

@ -74,6 +74,8 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
ParseGeneratorParameter(parameter, &options);
Options file_options;
file_options.opensource_runtime = opensource_runtime_;
for (int i = 0; i < options.size(); i++) {
if (options[i].first == "output_list_file") {
file_options.output_list_file = options[i].second;

@ -68,7 +68,7 @@ class PROTOC_EXPORT JavaGenerator : public CodeGenerator {
}
private:
bool opensource_runtime_ = true;
bool opensource_runtime_ = PROTO2_IS_OSS;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(JavaGenerator);
};

@ -66,8 +66,9 @@ const char kThinSeparator[] =
"// -------------------------------------------------------------------\n";
namespace {
const char* kDefaultPackage = "";
const char* DefaultPackage(Options options) {
return options.opensource_runtime ? "" : "com.google.protos";
}
// Names that should be avoided (in UpperCamelCase format).
// Using them will cause the compiler to generate accessors whose names
@ -136,7 +137,8 @@ std::string FieldName(const FieldDescriptor* field) {
} // namespace
void PrintGeneratedAnnotation(io::Printer* printer, char delimiter,
const std::string& annotation_file) {
const std::string& annotation_file,
Options options) {
if (annotation_file.empty()) {
return;
}
@ -305,17 +307,17 @@ std::string CamelCaseFieldName(const FieldDescriptor* field) {
}
std::string FileClassName(const FileDescriptor* file, bool immutable) {
ClassNameResolver name_resolver;
return name_resolver.GetFileClassName(file, immutable);
return ClassNameResolver().GetFileClassName(file, immutable);
}
std::string FileJavaPackage(const FileDescriptor* file, bool immutable) {
std::string FileJavaPackage(const FileDescriptor* file, bool immutable,
Options options) {
std::string result;
if (file->options().has_java_package()) {
result = file->options().java_package();
} else {
result = kDefaultPackage;
result = DefaultPackage(options);
if (!file->package().empty()) {
if (!result.empty()) result += '.';
result += file->package();
@ -325,8 +327,8 @@ std::string FileJavaPackage(const FileDescriptor* file, bool immutable) {
return result;
}
std::string FileJavaPackage(const FileDescriptor* file) {
return FileJavaPackage(file, true /* immutable */);
std::string FileJavaPackage(const FileDescriptor* file, Options options) {
return FileJavaPackage(file, true /* immutable */, options);
}
std::string JavaPackageToDir(std::string package_name) {
@ -532,7 +534,7 @@ std::string GetOneofStoredType(const FieldDescriptor* field) {
case JAVATYPE_ENUM:
return "java.lang.Integer";
case JAVATYPE_MESSAGE:
return ClassName(field->message_type());
return ClassNameResolver().GetClassName(field->message_type(), true);
default:
return BoxedPrimitiveTypeName(javaType);
}
@ -595,7 +597,7 @@ bool AllAscii(const std::string& text) {
}
std::string DefaultValue(const FieldDescriptor* field, bool immutable,
ClassNameResolver* name_resolver) {
ClassNameResolver* name_resolver, Options options) {
// Switch on CppType since we need to know which default_value_* method
// of FieldDescriptor to call.
switch (field->cpp_type()) {
@ -819,7 +821,8 @@ bool IsReferenceType(JavaType type) {
return false;
}
const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable) {
const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable,
Options options) {
switch (GetType(field)) {
case FieldDescriptor::TYPE_INT32:
return "Int32";

@ -41,8 +41,12 @@
#include <google/protobuf/io/printer.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/compiler/java/context.h>
#include <google/protobuf/compiler/java/options.h>
#include <google/protobuf/descriptor.pb.h>
// Must be last.
#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
namespace compiler {
@ -64,7 +68,8 @@ bool IsForbiddenKotlin(const std::string& field_name);
// annotation_file should be generated from the filename of the source file
// being annotated (which in turn must be a Java identifier plus ".java").
void PrintGeneratedAnnotation(io::Printer* printer, char delimiter = '$',
const std::string& annotation_file = "");
const std::string& annotation_file = "",
Options options = {});
// If a GeneratedMessageLite contains non-lite enums, then its verifier
// must be instantiated inline, rather than retrieved from the enum class.
@ -113,7 +118,8 @@ std::string UniqueFileScopeIdentifier(const Descriptor* descriptor);
std::string FileClassName(const FileDescriptor* file, bool immutable = true);
// Returns the file's Java package name.
std::string FileJavaPackage(const FileDescriptor* file, bool immutable);
std::string FileJavaPackage(const FileDescriptor* file, bool immutable,
Options options = {});
// Returns output directory for the given package name.
std::string JavaPackageToDir(std::string package_name);
@ -209,7 +215,8 @@ void MaybePrintGeneratedAnnotation(Context* context, io::Printer* printer,
PrintGeneratedAnnotation(printer, '$',
context->options().annotate_code
? AnnotationFileName(descriptor, suffix)
: "");
: "",
context->options());
}
}
@ -254,10 +261,12 @@ const char* FieldTypeName(const FieldDescriptor::Type field_type);
class ClassNameResolver;
std::string DefaultValue(const FieldDescriptor* field, bool immutable,
ClassNameResolver* name_resolver);
ClassNameResolver* name_resolver,
Options options = {});
inline std::string ImmutableDefaultValue(const FieldDescriptor* field,
ClassNameResolver* name_resolver) {
return DefaultValue(field, true, name_resolver);
ClassNameResolver* name_resolver,
Options options = {}) {
return DefaultValue(field, true, name_resolver, options);
}
bool IsDefaultValueJavaDefault(const FieldDescriptor* field);
bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field);
@ -336,7 +345,8 @@ bool IsReferenceType(JavaType type);
// Returns the capitalized name for calling relative functions in
// CodedInputStream
const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable);
const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable,
Options options);
// For encodings with fixed sizes, returns that size in bytes. Otherwise
// returns -1.
@ -474,4 +484,5 @@ std::pair<int, int> GetTableDrivenNumberOfEntriesAndLookUpStartFieldNumber(
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__

@ -102,7 +102,12 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
const JavaType keyJavaType = GetJavaType(key);
const JavaType valueJavaType = GetJavaType(value);
std::string pass_through_nullness = "/* nullable */\n";
// The code that generates the open-source version appears not to understand
// #else, so we have an #ifndef instead.
std::string pass_through_nullness =
context->options().opensource_runtime
? "/* nullable */\n"
: "@com.google.protobuf.Internal.ProtoPassThroughNullness ";
(*variables)["key_type"] = TypeName(key, name_resolver, false);
std::string boxed_key_type = TypeName(key, name_resolver, true);
@ -113,7 +118,8 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["short_key_type"] =
boxed_key_type.substr(boxed_key_type.rfind('.') + 1);
(*variables)["key_wire_type"] = WireType(key);
(*variables)["key_default_value"] = DefaultValue(key, true, name_resolver);
(*variables)["key_default_value"] =
DefaultValue(key, true, name_resolver, context->options());
(*variables)["key_null_check"] =
IsReferenceType(keyJavaType)
? "if (key == null) { throw new NullPointerException(\"map key\"); }"
@ -132,7 +138,8 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["boxed_value_type"] = "java.lang.Integer";
(*variables)["value_wire_type"] = WireType(value);
(*variables)["value_default_value"] =
DefaultValue(value, true, name_resolver) + ".getNumber()";
DefaultValue(value, true, name_resolver, context->options()) +
".getNumber()";
(*variables)["value_enum_type"] = TypeName(value, name_resolver, false);
@ -146,7 +153,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
} else {
// Map unknown values to the default value if we don't have UNRECOGNIZED.
(*variables)["unrecognized_value"] =
DefaultValue(value, true, name_resolver);
DefaultValue(value, true, name_resolver, context->options());
}
} else {
(*variables)["value_type"] = TypeName(value, name_resolver, false);
@ -158,7 +165,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["boxed_value_type"] = TypeName(value, name_resolver, true);
(*variables)["value_wire_type"] = WireType(value);
(*variables)["value_default_value"] =
DefaultValue(value, true, name_resolver);
DefaultValue(value, true, name_resolver, context->options());
}
(*variables)["type_parameters"] =
(*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"];
@ -194,7 +201,9 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
ImmutableMapFieldGenerator::ImmutableMapFieldGenerator(
const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex,
Context* context)
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
: descriptor_(descriptor),
name_resolver_(context->GetNameResolver()),
context_(context) {
SetMessageVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor), context,
&variables_);
@ -218,14 +227,16 @@ void ImmutableMapFieldGenerator::GenerateInterfaceMembers(
" $key_type$ key);\n");
printer->Annotate("{", "}", descriptor_);
if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(
variables_,
@ -277,14 +288,16 @@ void ImmutableMapFieldGenerator::GenerateInterfaceMembers(
printer->Annotate("{", "}", descriptor_);
}
} else {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"java.util.Map<$type_parameters$>\n"
"${$get$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"java.util.Map<$type_parameters$>\n"
"${$get$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$java.util.Map<$type_parameters$>\n"
@ -401,18 +414,20 @@ void ImmutableMapFieldGenerator::GenerateBuilderMembers(
"}\n");
printer->Annotate("{", "}", descriptor_);
if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
printer->Print(
variables_,
"/**\n"
" * Use alternate mutation accessors instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$getMutable$capitalized_name$$}$() {\n"
" return internalGetAdapted$capitalized_name$Map(\n"
" internalGetMutable$capitalized_name$().getMutableMap());\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(
variables_,
"/**\n"
" * Use alternate mutation accessors instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$getMutable$capitalized_name$$}$() {\n"
" return internalGetAdapted$capitalized_name$Map(\n"
" internalGetMutable$capitalized_name$().getMutableMap());\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$public Builder ${$put$capitalized_name$$}$(\n"
@ -437,17 +452,19 @@ void ImmutableMapFieldGenerator::GenerateBuilderMembers(
"}\n");
printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) {
printer->Print(
variables_,
"/**\n"
" * Use alternate mutation accessors instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
"${$getMutable$capitalized_name$Value$}$() {\n"
" return internalGetMutable$capitalized_name$().getMutableMap();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(
variables_,
"/**\n"
" * Use alternate mutation accessors instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
"${$getMutable$capitalized_name$Value$}$() {\n"
" return internalGetMutable$capitalized_name$().getMutableMap();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(
variables_,
@ -473,17 +490,19 @@ void ImmutableMapFieldGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_);
}
} else {
printer->Print(
variables_,
"/**\n"
" * Use alternate mutation accessors instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$type_parameters$>\n"
"${$getMutable$capitalized_name$$}$() {\n"
" return internalGetMutable$capitalized_name$().getMutableMap();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(
variables_,
"/**\n"
" * Use alternate mutation accessors instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$type_parameters$>\n"
"${$getMutable$capitalized_name$$}$() {\n"
" return internalGetMutable$capitalized_name$().getMutableMap();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$"
@ -530,17 +549,20 @@ void ImmutableMapFieldGenerator::GenerateMapGetters(
"}\n");
printer->Annotate("{", "}", descriptor_);
if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Override\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(
variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Override\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"@java.lang.Override\n"
@ -640,17 +662,19 @@ void ImmutableMapFieldGenerator::GenerateMapGetters(
printer->Annotate("{", "}", descriptor_);
}
} else {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Override\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$type_parameters$> "
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Override\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$type_parameters$> "
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"@java.lang.Override\n"

@ -71,6 +71,7 @@ class ImmutableMapFieldGenerator : public ImmutableFieldGenerator {
const FieldDescriptor* descriptor_;
std::map<std::string, std::string> variables_;
ClassNameResolver* name_resolver_;
Context* context_;
void GenerateMapGetters(io::Printer* printer) const;
};

@ -104,14 +104,18 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
const JavaType keyJavaType = GetJavaType(key);
const JavaType valueJavaType = GetJavaType(value);
std::string pass_through_nullness = "/* nullable */\n";
std::string pass_through_nullness =
context->options().opensource_runtime
? "/* nullable */\n"
: "@com.google.protobuf.Internal.ProtoPassThroughNullness ";
(*variables)["key_type"] = TypeName(key, name_resolver, false);
(*variables)["boxed_key_type"] = TypeName(key, name_resolver, true);
(*variables)["kt_key_type"] = KotlinTypeName(key, name_resolver);
(*variables)["kt_value_type"] = KotlinTypeName(value, name_resolver);
(*variables)["key_wire_type"] = WireType(key);
(*variables)["key_default_value"] = DefaultValue(key, true, name_resolver);
(*variables)["key_default_value"] =
DefaultValue(key, true, name_resolver, context->options());
// We use `x.getClass()` as a null check because it generates less bytecode
// than an `if (x == null) { throw ... }` statement.
(*variables)["key_null_check"] =
@ -131,7 +135,8 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["boxed_value_type"] = "java.lang.Integer";
(*variables)["value_wire_type"] = WireType(value);
(*variables)["value_default_value"] =
DefaultValue(value, true, name_resolver) + ".getNumber()";
DefaultValue(value, true, name_resolver, context->options()) +
".getNumber()";
(*variables)["value_enum_type"] = TypeName(value, name_resolver, false);
@ -145,7 +150,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
} else {
// Map unknown values to the default value if we don't have UNRECOGNIZED.
(*variables)["unrecognized_value"] =
DefaultValue(value, true, name_resolver);
DefaultValue(value, true, name_resolver, context->options());
}
} else {
(*variables)["value_type"] = TypeName(value, name_resolver, false);
@ -157,7 +162,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["boxed_value_type"] = TypeName(value, name_resolver, true);
(*variables)["value_wire_type"] = WireType(value);
(*variables)["value_default_value"] =
DefaultValue(value, true, name_resolver);
DefaultValue(value, true, name_resolver, context->options());
}
(*variables)["type_parameters"] =
(*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"];
@ -203,14 +208,16 @@ void ImmutableMapFieldLiteGenerator::GenerateInterfaceMembers(
" $key_type$ key);\n");
printer->Annotate("{", "}", descriptor_);
if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(
variables_,
@ -262,14 +269,16 @@ void ImmutableMapFieldLiteGenerator::GenerateInterfaceMembers(
printer->Annotate("{", "}", descriptor_);
}
} else {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"java.util.Map<$type_parameters$>\n"
"${$get$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"java.util.Map<$type_parameters$>\n"
"${$get$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$java.util.Map<$type_parameters$>\n"
@ -306,6 +315,12 @@ void ImmutableMapFieldLiteGenerator::GenerateMembers(
" $value_wire_type$,\n"
" $value_default_value$);\n"
"}\n");
if (!context_->options().opensource_runtime) {
printer->Print(variables_,
"@com.google.protobuf.ProtoField(\n"
" fieldNumber=$number$,\n"
" type=com.google.protobuf.FieldType.MAP)\n");
}
printer->Print(variables_,
"private com.google.protobuf.MapFieldLite<\n"
" $type_parameters$> $name$_ =\n"
@ -347,16 +362,19 @@ void ImmutableMapFieldLiteGenerator::GenerateMembers(
" com.google.protobuf.Internal.MapAdapter.newEnumConverter(\n"
" $value_enum_type$.internalGetValueMap(),\n"
" $unrecognized_value$);\n");
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(
variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(
variables_,
@ -461,17 +479,19 @@ void ImmutableMapFieldLiteGenerator::GenerateMembers(
printer->Annotate("{", "}", descriptor_);
}
} else {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Override\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$type_parameters$> "
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Override\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$type_parameters$> "
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"@java.lang.Override\n"
@ -602,16 +622,19 @@ void ImmutableMapFieldLiteGenerator::GenerateBuilderMembers(
"}\n");
printer->Annotate("{", "}", descriptor_);
if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(
variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"@java.lang.Override\n"
@ -757,17 +780,19 @@ void ImmutableMapFieldLiteGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_);
}
} else {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Override\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$type_parameters$> "
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
if (context_->options().opensource_runtime) {
printer->Print(variables_,
"/**\n"
" * Use {@link #get$capitalized_name$Map()} instead.\n"
" */\n"
"@java.lang.Override\n"
"@java.lang.Deprecated\n"
"public java.util.Map<$type_parameters$> "
"${$get$capitalized_name$$}$() {\n"
" return get$capitalized_name$Map();\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
}
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"@java.lang.Override\n"

@ -261,6 +261,9 @@ int ImmutableMessageGenerator::GenerateFieldAccessorTableInitializer(
void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) {
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true, "OrBuilder");
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoNonnullApi\n");
}
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n"
@ -320,6 +323,10 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
WriteMessageDocComment(printer, descriptor_);
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true);
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoNonnullApi\n");
}
// The builder_type stores the super type name of the nested Builder class.
std::string builder_type;
if (descriptor_->extension_range_count() > 0) {
@ -458,18 +465,22 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
"private $oneof_capitalized_name$Case(int value) {\n"
" this.value = value;\n"
"}\n");
if (context_->options().opensource_runtime) {
printer->Print(
vars,
"/**\n"
" * @param value The number of the enum to look for.\n"
" * @return The enum associated with the given number.\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $oneof_capitalized_name$Case valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n");
}
printer->Print(
vars,
"/**\n"
" * @param value The number of the enum to look for.\n"
" * @return The enum associated with the given number.\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $oneof_capitalized_name$Case valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n"
"public static $oneof_capitalized_name$Case forNumber(int value) {\n"
" switch (value) {\n");
for (int j = 0; j < (oneof)->field_count(); j++) {
@ -982,6 +993,10 @@ void ImmutableMessageGenerator::GenerateEqualsAndHashCode(
printer->Print(
"@java.lang.Override\n"
"public boolean equals(");
if (!context_->options().opensource_runtime) {
printer->Print(
"@com.google.protobuf.Internal.ProtoMethodAcceptsNullParameter\n");
}
printer->Print("final java.lang.Object obj) {\n");
printer->Indent();
printer->Print(
@ -1440,8 +1455,15 @@ void ImmutableMessageGenerator::GenerateKotlinDsl(io::Printer* printer) const {
void ImmutableMessageGenerator::GenerateKotlinMembers(
io::Printer* printer) const {
printer->Print("@kotlin.jvm.JvmName(\"-initialize$camelcase_name$\")\n",
"camelcase_name",
name_resolver_->GetKotlinFactoryName(descriptor_));
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.errorprone.annotations.CheckReturnValue\n");
}
printer->Print(
"@kotlin.jvm.JvmName(\"-initialize$camelcase_name$\")\n"
"inline fun $camelcase_name$(block: $message_kt$.Dsl.() -> "
"kotlin.Unit): "
"$message$ "
@ -1470,8 +1492,13 @@ void ImmutableMessageGenerator::GenerateKotlinMembers(
void ImmutableMessageGenerator::GenerateTopLevelKotlinMembers(
io::Printer* printer) const {
printer->Print("@kotlin.jvm.JvmSynthetic\n");
if (context_->options().opensource_runtime) {
printer->Print("@com.google.errorprone.annotations.CheckReturnValue\n");
}
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"inline fun $message$.copy(block: $message_kt$.Dsl.() -> "
"kotlin.Unit): $message$ =\n"
" $message_kt$.Dsl._create(this.toBuilder()).apply { block() "
@ -1716,7 +1743,8 @@ void ImmutableMessageGenerator::GenerateAnyMethods(io::Printer* printer) {
"@java.lang.SuppressWarnings(\"unchecked\")\n"
"public <T extends com.google.protobuf.Message> T unpack(\n"
" java.lang.Class<T> clazz)\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\n");
printer->Print(
" boolean invalidClazz = false;\n"
" if (cachedUnpackValue != null) {\n"
" if (cachedUnpackValue.getClass() == clazz) {\n"

@ -168,24 +168,26 @@ void MessageBuilderGenerator::Generate(io::Printer* printer) {
.GenerateBuilderMembers(printer);
}
// Override methods declared in GeneratedMessage to return the concrete
// generated type so callsites won't depend on GeneratedMessage. This
// is needed to keep binary compatibility when we change generated code
// to subclass a different GeneratedMessage class (e.g., in v3.0.0 release
// we changed all generated code to subclass GeneratedMessageV3).
printer->Print(
"@java.lang.Override\n"
"public final Builder setUnknownFields(\n"
" final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
" return super.setUnknownFields(unknownFields);\n"
"}\n"
"\n"
"@java.lang.Override\n"
"public final Builder mergeUnknownFields(\n"
" final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
" return super.mergeUnknownFields(unknownFields);\n"
"}\n"
"\n");
if (context_->options().opensource_runtime) {
// Override methods declared in GeneratedMessage to return the concrete
// generated type so callsites won't depend on GeneratedMessage. This
// is needed to keep binary compatibility when we change generated code
// to subclass a different GeneratedMessage class (e.g., in v3.0.0 release
// we changed all generated code to subclass GeneratedMessageV3).
printer->Print(
"@java.lang.Override\n"
"public final Builder setUnknownFields(\n"
" final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
" return super.setUnknownFields(unknownFields);\n"
"}\n"
"\n"
"@java.lang.Override\n"
"public final Builder mergeUnknownFields(\n"
" final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
" return super.mergeUnknownFields(unknownFields);\n"
"}\n"
"\n");
}
printer->Print(
"\n"
@ -443,75 +445,77 @@ void MessageBuilderGenerator::GenerateCommonBuilderMethods(
"\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
// Override methods declared in GeneratedMessage to return the concrete
// generated type so callsites won't depend on GeneratedMessage. This
// is needed to keep binary compatibility when we change generated code
// to subclass a different GeneratedMessage class (e.g., in v3.0.0 release
// we changed all generated code to subclass GeneratedMessageV3).
printer->Print(
"@java.lang.Override\n"
"public Builder clone() {\n"
" return super.clone();\n"
"}\n"
"@java.lang.Override\n"
"public Builder setField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" java.lang.Object value) {\n"
" return super.setField(field, value);\n"
"}\n"
"@java.lang.Override\n"
"public Builder clearField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field) {\n"
" return super.clearField(field);\n"
"}\n"
"@java.lang.Override\n"
"public Builder clearOneof(\n"
" com.google.protobuf.Descriptors.OneofDescriptor oneof) {\n"
" return super.clearOneof(oneof);\n"
"}\n"
"@java.lang.Override\n"
"public Builder setRepeatedField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" int index, java.lang.Object value) {\n"
" return super.setRepeatedField(field, index, value);\n"
"}\n"
"@java.lang.Override\n"
"public Builder addRepeatedField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" java.lang.Object value) {\n"
" return super.addRepeatedField(field, value);\n"
"}\n");
if (descriptor_->extension_range_count() > 0) {
if (context_->options().opensource_runtime) {
// Override methods declared in GeneratedMessage to return the concrete
// generated type so callsites won't depend on GeneratedMessage. This
// is needed to keep binary compatibility when we change generated code
// to subclass a different GeneratedMessage class (e.g., in v3.0.0 release
// we changed all generated code to subclass GeneratedMessageV3).
printer->Print(
"@java.lang.Override\n"
"public <Type> Builder setExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, Type> extension,\n"
" Type value) {\n"
" return super.setExtension(extension, value);\n"
"public Builder clone() {\n"
" return super.clone();\n"
"}\n"
"@java.lang.Override\n"
"public <Type> Builder setExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, java.util.List<Type>> extension,\n"
" int index, Type value) {\n"
" return super.setExtension(extension, index, value);\n"
"public Builder setField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" java.lang.Object value) {\n"
" return super.setField(field, value);\n"
"}\n"
"@java.lang.Override\n"
"public <Type> Builder addExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, java.util.List<Type>> extension,\n"
" Type value) {\n"
" return super.addExtension(extension, value);\n"
"public Builder clearField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field) {\n"
" return super.clearField(field);\n"
"}\n"
"@java.lang.Override\n"
"public <Type> Builder clearExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, ?> extension) {\n"
" return super.clearExtension(extension);\n"
"}\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
"public Builder clearOneof(\n"
" com.google.protobuf.Descriptors.OneofDescriptor oneof) {\n"
" return super.clearOneof(oneof);\n"
"}\n"
"@java.lang.Override\n"
"public Builder setRepeatedField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" int index, java.lang.Object value) {\n"
" return super.setRepeatedField(field, index, value);\n"
"}\n"
"@java.lang.Override\n"
"public Builder addRepeatedField(\n"
" com.google.protobuf.Descriptors.FieldDescriptor field,\n"
" java.lang.Object value) {\n"
" return super.addRepeatedField(field, value);\n"
"}\n");
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"@java.lang.Override\n"
"public <Type> Builder setExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, Type> extension,\n"
" Type value) {\n"
" return super.setExtension(extension, value);\n"
"}\n"
"@java.lang.Override\n"
"public <Type> Builder setExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, java.util.List<Type>> extension,\n"
" int index, Type value) {\n"
" return super.setExtension(extension, index, value);\n"
"}\n"
"@java.lang.Override\n"
"public <Type> Builder addExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, java.util.List<Type>> extension,\n"
" Type value) {\n"
" return super.addExtension(extension, value);\n"
"}\n"
"@java.lang.Override\n"
"public <Type> Builder clearExtension(\n"
" com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
" $classname$, ?> extension) {\n"
" return super.clearExtension(extension);\n"
"}\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
}
}
// -----------------------------------------------------------------

@ -59,7 +59,8 @@ namespace {
void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
int builderBitIndex, const FieldGeneratorInfo* info,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* variables) {
std::map<std::string, std::string>* variables,
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
(*variables)["type"] =
@ -82,8 +83,10 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
(*variables)["on_changed"] = "onChanged();";
(*variables)["ver"] = GeneratedCodeVersionSuffix();
(*variables)["get_parser"] =
ExposePublicParser(descriptor->message_type()->file()) ? "PARSER"
: "parser()";
ExposePublicParser(descriptor->message_type()->file()) &&
context->options().opensource_runtime
? "PARSER"
: "parser()";
if (HasHasbit(descriptor)) {
// For singular messages and builders, one bit is used for the hasField bit.
@ -133,10 +136,12 @@ void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
ImmutableMessageFieldGenerator::ImmutableMessageFieldGenerator(
const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex,
Context* context)
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
: descriptor_(descriptor),
name_resolver_(context->GetNameResolver()),
context_(context) {
SetMessageVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
ImmutableMessageFieldGenerator::~ImmutableMessageFieldGenerator() {}
@ -861,7 +866,7 @@ RepeatedImmutableMessageFieldGenerator::RepeatedImmutableMessageFieldGenerator(
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
SetMessageVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
RepeatedImmutableMessageFieldGenerator::

@ -91,6 +91,7 @@ class ImmutableMessageFieldGenerator : public ImmutableFieldGenerator {
const FieldDescriptor* descriptor_;
std::map<std::string, std::string> variables_;
ClassNameResolver* name_resolver_;
Context* context_;
void PrintNestedBuilderCondition(io::Printer* printer,
const char* regular_case,

@ -59,7 +59,8 @@ namespace {
void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
int builderBitIndex, const FieldGeneratorInfo* info,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* variables) {
std::map<std::string, std::string>* variables,
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
(*variables)["type"] =
@ -118,10 +119,11 @@ ImmutableMessageFieldLiteGenerator::ImmutableMessageFieldLiteGenerator(
const FieldDescriptor* descriptor, int messageBitIndex, Context* context)
: descriptor_(descriptor),
messageBitIndex_(messageBitIndex),
name_resolver_(context->GetNameResolver()) {
name_resolver_(context->GetNameResolver()),
context_(context) {
SetMessageVariables(descriptor, messageBitIndex, 0,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
ImmutableMessageFieldLiteGenerator::~ImmutableMessageFieldLiteGenerator() {}
@ -496,10 +498,12 @@ RepeatedImmutableMessageFieldLiteGenerator::
RepeatedImmutableMessageFieldLiteGenerator(
const FieldDescriptor* descriptor, int messageBitIndex,
Context* context)
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
: descriptor_(descriptor),
name_resolver_(context->GetNameResolver()),
context_(context) {
SetMessageVariables(descriptor, messageBitIndex, 0,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
RepeatedImmutableMessageFieldLiteGenerator::
@ -529,6 +533,13 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateInterfaceMembers(
void RepeatedImmutableMessageFieldLiteGenerator::GenerateMembers(
io::Printer* printer) const {
if (!context_->options().opensource_runtime) {
printer->Print(
variables_,
"@com.google.protobuf.ProtoField(\n"
" fieldNumber=$number$,\n"
" type=com.google.protobuf.FieldType.$annotation_field_type$)\n");
}
printer->Print(
variables_,
"private com.google.protobuf.Internal.ProtobufList<$type$> $name$_;\n");

@ -82,6 +82,7 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator {
std::map<std::string, std::string> variables_;
const int messageBitIndex_;
ClassNameResolver* name_resolver_;
Context* context_;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageFieldLiteGenerator);
@ -128,6 +129,7 @@ class RepeatedImmutableMessageFieldLiteGenerator
const FieldDescriptor* descriptor_;
std::map<std::string, std::string> variables_;
ClassNameResolver* name_resolver_;
Context* context_;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutableMessageFieldLiteGenerator);

@ -114,6 +114,10 @@ int ImmutableMessageLiteGenerator::GenerateStaticVariableInitializers(
void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) {
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true, "OrBuilder");
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoNonnullApi\n");
}
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"$deprecation$public interface ${$$classname$OrBuilder$}$ extends \n"
@ -236,10 +240,12 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) {
vars["oneof_capitalized_name"] =
context_->GetOneofGeneratorInfo(oneof)->capitalized_name;
vars["oneof_index"] = StrCat((oneof)->index());
// oneofCase_ and oneof_
printer->Print(vars,
"private int $oneof_name$Case_ = 0;\n"
"private java.lang.Object $oneof_name$_;\n");
if (context_->options().opensource_runtime) {
// oneofCase_ and oneof_
printer->Print(vars,
"private int $oneof_name$Case_ = 0;\n"
"private java.lang.Object $oneof_name$_;\n");
}
// OneofCase enum
printer->Print(vars, "public enum $oneof_capitalized_name$Case {\n");
printer->Indent();
@ -256,16 +262,20 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) {
"private $oneof_capitalized_name$Case(int value) {\n"
" this.value = value;\n"
"}\n");
if (context_->options().opensource_runtime) {
printer->Print(
vars,
"/**\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $oneof_capitalized_name$Case valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n");
}
printer->Print(
vars,
"/**\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $oneof_capitalized_name$Case valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n"
"public static $oneof_capitalized_name$Case forNumber(int value) {\n"
" switch (value) {\n");
for (int j = 0; j < (oneof)->field_count(); j++) {
@ -782,8 +792,14 @@ void ImmutableMessageLiteGenerator::GenerateKotlinDsl(
void ImmutableMessageLiteGenerator::GenerateKotlinMembers(
io::Printer* printer) const {
printer->Print("@kotlin.jvm.JvmName(\"-initialize$camelcase_name$\")\n",
"camelcase_name",
name_resolver_->GetKotlinFactoryName(descriptor_));
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.errorprone.annotations.CheckReturnValue\n");
}
printer->Print(
"@kotlin.jvm.JvmName(\"-initialize$camelcase_name$\")\n"
"inline fun $camelcase_name$(block: $message_kt$.Dsl.() -> "
"kotlin.Unit): $message$ =\n"
" $message_kt$.Dsl._create($message$.newBuilder()).apply { block() "
@ -810,6 +826,9 @@ void ImmutableMessageLiteGenerator::GenerateKotlinMembers(
void ImmutableMessageLiteGenerator::GenerateTopLevelKotlinMembers(
io::Printer* printer) const {
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.errorprone.annotations.CheckReturnValue\n");
}
printer->Print(
"inline fun $message$.copy(block: $message_kt$.Dsl.() -> "
"kotlin.Unit): $message$ =\n"

@ -141,10 +141,6 @@ bool MessageHasConflictingClassName(const Descriptor* message,
} // namespace
ClassNameResolver::ClassNameResolver() {}
ClassNameResolver::~ClassNameResolver() {}
std::string ClassNameResolver::GetFileDefaultImmutableClassName(
const FileDescriptor* file) {
std::string basename;
@ -217,8 +213,12 @@ bool ClassNameResolver::HasConflictingClassName(const FileDescriptor* file,
}
std::string ClassNameResolver::GetDescriptorClassName(
const FileDescriptor* descriptor) {
return GetFileImmutableClassName(descriptor);
const FileDescriptor* file) {
if (options_.opensource_runtime) {
return GetFileImmutableClassName(file);
} else {
return GetFileImmutableClassName(file) + "InternalDescriptors";
}
}
std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
@ -228,7 +228,7 @@ std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
bool immutable, bool kotlin) {
std::string result = FileJavaPackage(descriptor, immutable);
std::string result = FileJavaPackage(descriptor, immutable, options_);
if (!result.empty()) result += '.';
result += GetFileClassName(descriptor, immutable, kotlin);
return result;
@ -248,7 +248,7 @@ std::string ClassNameResolver::GetClassFullName(
bool immutable, bool is_own_file, bool kotlin) {
std::string result;
if (is_own_file) {
result = FileJavaPackage(file, immutable);
result = FileJavaPackage(file, immutable, options_);
} else {
result = GetClassName(file, immutable, kotlin);
}
@ -308,7 +308,7 @@ std::string ClassNameResolver::GetJavaClassFullName(
bool immutable, bool kotlin) {
std::string result;
if (MultipleJavaFiles(file, immutable)) {
result = FileJavaPackage(file, immutable);
result = FileJavaPackage(file, immutable, options_);
if (!result.empty()) result += '.';
} else {
result = GetClassName(file, immutable, kotlin);
@ -372,7 +372,7 @@ std::string ClassNameResolver::GetDowngradedFileClassName(
std::string ClassNameResolver::GetDowngradedClassName(
const Descriptor* descriptor) {
return FileJavaPackage(descriptor->file()) + "." +
return FileJavaPackage(descriptor->file(), true, options_) + "." +
GetDowngradedFileClassName(descriptor->file()) + "." +
ClassNameWithoutPackage(descriptor, false);
}

@ -34,7 +34,9 @@
#include <map>
#include <string>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/compiler/java/options.h>
// Must be last.
#include <google/protobuf/port_def.inc>
@ -58,8 +60,11 @@ enum NameEquality { NO_MATCH, EXACT_EQUAL, EQUAL_IGNORE_CASE };
// Thread-safety note: This class is *not* thread-safe.
class ClassNameResolver {
public:
ClassNameResolver();
~ClassNameResolver();
explicit ClassNameResolver(const Options& options = {}) : options_(options) {}
~ClassNameResolver() = default;
ClassNameResolver(const ClassNameResolver&) = delete;
ClassNameResolver& operator=(const ClassNameResolver&) = delete;
// Gets the unqualified outer class name for the file.
std::string GetFileClassName(const FileDescriptor* file, bool immutable);
@ -135,6 +140,8 @@ class ClassNameResolver {
const FileDescriptor* file, bool immutable,
bool is_own_file, bool kotlin);
Options options_;
private:
// Get the Java Class style full name of a message.
std::string GetJavaClassFullName(const std::string& name_without_package,
@ -145,8 +152,6 @@ class ClassNameResolver {
// Caches the result to provide better performance.
std::map<const FileDescriptor*, std::string>
file_immutable_outer_class_names_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ClassNameResolver);
};
} // namespace java

@ -40,6 +40,8 @@
#include <string>
#include <google/protobuf/compiler/java/options.h>
namespace google {
namespace protobuf {
@ -85,7 +87,8 @@ std::string ClassName(const ServiceDescriptor* descriptor);
//
// Returns:
// Java package name.
std::string FileJavaPackage(const FileDescriptor* descriptor);
std::string FileJavaPackage(const FileDescriptor* descriptor,
Options options = {});
// Requires:
// descriptor != NULL

@ -33,6 +33,8 @@
#include <string>
#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
namespace compiler {
@ -54,6 +56,7 @@ struct Options {
// When set, the protoc will generate the current files and all the transitive
// dependencies as lite runtime.
bool enforce_lite;
bool opensource_runtime = PROTO2_IS_OSS;
// If true, we should build .meta files and emit @Generated annotations into
// generated code.
bool annotate_code;
@ -70,4 +73,5 @@ struct Options {
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_OPTIONS_H__

@ -61,7 +61,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
int messageBitIndex, int builderBitIndex,
const FieldGeneratorInfo* info,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* variables) {
std::map<std::string, std::string>* variables,
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
JavaType javaType = GetJavaType(descriptor);
@ -106,13 +107,15 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
(*variables)["repeated_set"] = (*variables)["name"] + "_.set";
}
(*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_init"] =
IsDefaultValueJavaDefault(descriptor)
? ""
: ("= " + ImmutableDefaultValue(descriptor, name_resolver));
(*variables)["capitalized_type"] =
GetCapitalizedType(descriptor, /* immutable = */ true);
: ("= " + ImmutableDefaultValue(descriptor, name_resolver,
context->options()));
(*variables)["capitalized_type"] = GetCapitalizedType(
descriptor, /* immutable = */ true, context->options());
(*variables)["tag"] =
StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
(*variables)["tag_size"] = StrCat(
@ -209,7 +212,7 @@ ImmutablePrimitiveFieldGenerator::ImmutablePrimitiveFieldGenerator(
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
ImmutablePrimitiveFieldGenerator::~ImmutablePrimitiveFieldGenerator() {}
@ -681,7 +684,7 @@ RepeatedImmutablePrimitiveFieldGenerator::
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
RepeatedImmutablePrimitiveFieldGenerator::

@ -69,16 +69,18 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
int messageBitIndex, int builderBitIndex,
const FieldGeneratorInfo* info,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* variables) {
std::map<std::string, std::string>* variables,
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
JavaType javaType = GetJavaType(descriptor);
(*variables)["type"] = PrimitiveTypeName(javaType);
(*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType);
(*variables)["kt_type"] = KotlinTypeName(javaType);
(*variables)["field_type"] = (*variables)["type"];
(*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
(*variables)["capitalized_type"] =
GetCapitalizedType(descriptor, /* immutable = */ true);
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["capitalized_type"] = GetCapitalizedType(
descriptor, /* immutable = */ true, context->options());
(*variables)["tag"] =
StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
(*variables)["tag_size"] = StrCat(
@ -199,10 +201,11 @@ ImmutablePrimitiveFieldLiteGenerator::ImmutablePrimitiveFieldLiteGenerator(
const FieldDescriptor* descriptor, int messageBitIndex, Context* context)
: descriptor_(descriptor),
messageBitIndex_(messageBitIndex),
context_(context),
name_resolver_(context->GetNameResolver()) {
SetPrimitiveVariables(descriptor, messageBitIndex, 0,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
ImmutablePrimitiveFieldLiteGenerator::~ImmutablePrimitiveFieldLiteGenerator() {}
@ -231,6 +234,20 @@ void ImmutablePrimitiveFieldLiteGenerator::GenerateMembers(
variables_,
"private static final $field_type$ $bytes_default$ = $default$;\n");
}
if (!context_->options().opensource_runtime) {
printer->Print(
variables_,
"@com.google.protobuf.ProtoField(\n"
" fieldNumber=$number$,\n"
" type=com.google.protobuf.FieldType.$annotation_field_type$,\n"
" isRequired=$required$)\n");
if (HasHazzer(descriptor_)) {
printer->Print(variables_,
"@com.google.protobuf.ProtoPresenceCheckedField(\n"
" presenceBitsId=$bit_field_id$,\n"
" mask=$bit_field_mask$)\n");
}
}
printer->Print(variables_, "private $field_type$ $name$_;\n");
PrintExtraFieldInfo(variables_, printer);
if (HasHazzer(descriptor_)) {
@ -493,7 +510,7 @@ RepeatedImmutablePrimitiveFieldLiteGenerator::
name_resolver_(context->GetNameResolver()) {
SetPrimitiveVariables(descriptor, messageBitIndex, 0,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
RepeatedImmutablePrimitiveFieldLiteGenerator::

@ -81,6 +81,7 @@ class ImmutablePrimitiveFieldLiteGenerator
const FieldDescriptor* descriptor_;
std::map<std::string, std::string> variables_;
const int messageBitIndex_;
Context* context_;
ClassNameResolver* name_resolver_;
private:

@ -68,6 +68,10 @@ void ImmutableServiceGenerator::Generate(io::Printer* printer) {
WriteServiceDocComment(printer, descriptor_);
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true);
if (!context_->options().opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoNonnullApi\n");
}
printer->Print(
"public $static$ abstract class $classname$\n"
" implements com.google.protobuf.Service {\n",

@ -51,14 +51,16 @@ namespace java {
SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file,
const Options& options)
: name_resolver_(new ClassNameResolver), file_(file), options_(options) {}
: name_resolver_(new ClassNameResolver(options)),
file_(file),
options_(options) {}
SharedCodeGenerator::~SharedCodeGenerator() {}
void SharedCodeGenerator::Generate(
GeneratorContext* context, std::vector<std::string>* file_list,
std::vector<std::string>* annotation_file_list) {
std::string java_package = FileJavaPackage(file_);
std::string java_package = FileJavaPackage(file_, true, options_);
std::string package_dir = JavaPackageToDir(java_package);
if (HasDescriptorMethods(file_, options_.enforce_lite)) {
@ -87,7 +89,12 @@ void SharedCodeGenerator::Generate(
"package", java_package);
}
PrintGeneratedAnnotation(printer.get(), '$',
options_.annotate_code ? info_relative_path : "");
options_.annotate_code ? info_relative_path : "",
options_);
if (!options_.opensource_runtime) {
printer->Print("@com.google.protobuf.Internal.ProtoNonnullApi\n");
}
printer->Print(
"public final class $classname$ {\n"
" public static com.google.protobuf.Descriptors.FileDescriptor\n"
@ -163,7 +170,7 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) {
std::vector<std::pair<std::string, std::string> > dependencies;
for (int i = 0; i < file_->dependency_count(); i++) {
std::string filename = file_->dependency(i)->name();
std::string package = FileJavaPackage(file_->dependency(i));
std::string package = FileJavaPackage(file_->dependency(i), true, options_);
std::string classname =
name_resolver_->GetDescriptorClassName(file_->dependency(i));
std::string full_name;
@ -180,13 +187,15 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) {
printer->Print(
"descriptor = com.google.protobuf.Descriptors.FileDescriptor\n"
" .internalBuildGeneratedFileFrom(descriptorData,\n");
printer->Print(
" new com.google.protobuf.Descriptors.FileDescriptor[] {\n");
if (options_.opensource_runtime) {
printer->Print(
" new com.google.protobuf.Descriptors.FileDescriptor[] {\n");
for (int i = 0; i < dependencies.size(); i++) {
const std::string& dependency = dependencies[i].second;
printer->Print(" $dependency$.getDescriptor(),\n", "dependency",
dependency);
for (int i = 0; i < dependencies.size(); i++) {
const std::string& dependency = dependencies[i].second;
printer->Print(" $dependency$.getDescriptor(),\n", "dependency",
dependency);
}
}
printer->Print(" });\n");

@ -63,14 +63,17 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
int messageBitIndex, int builderBitIndex,
const FieldGeneratorInfo* info,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* variables) {
std::map<std::string, std::string>* variables,
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
(*variables)["empty_list"] = "com.google.protobuf.LazyStringArrayList.EMPTY";
(*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_init"] =
"= " + ImmutableDefaultValue(descriptor, name_resolver);
"= " +
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["capitalized_type"] = "String";
(*variables)["tag"] =
StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
@ -151,7 +154,7 @@ ImmutableStringFieldGenerator::ImmutableStringFieldGenerator(
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
ImmutableStringFieldGenerator::~ImmutableStringFieldGenerator() {}
@ -753,7 +756,7 @@ RepeatedImmutableStringFieldGenerator::RepeatedImmutableStringFieldGenerator(
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
RepeatedImmutableStringFieldGenerator::

@ -63,15 +63,18 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
int messageBitIndex, int builderBitIndex,
const FieldGeneratorInfo* info,
ClassNameResolver* name_resolver,
std::map<std::string, std::string>* variables) {
std::map<std::string, std::string>* variables,
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
(*variables)["empty_list"] =
"com.google.protobuf.GeneratedMessageLite.emptyProtobufList()";
(*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_init"] =
"= " + ImmutableDefaultValue(descriptor, name_resolver);
"= " +
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["capitalized_type"] = "java.lang.String";
(*variables)["tag"] =
StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
@ -92,8 +95,17 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
" is deprecated\") "
: "";
(*variables)["required"] = descriptor->is_required() ? "true" : "false";
if (!context->options().opensource_runtime) {
(*variables)["enforce_utf8"] = CheckUtf8(descriptor) ? "true" : "false";
}
if (HasHasbit(descriptor)) {
if (!context->options().opensource_runtime) {
(*variables)["bit_field_id"] = StrCat(messageBitIndex / 32);
(*variables)["bit_field_name"] = GetBitFieldNameForBit(messageBitIndex);
(*variables)["bit_field_mask"] =
StrCat(1 << (messageBitIndex % 32));
}
// For singular messages and builders, one bit is used for the hasField bit.
(*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
@ -126,10 +138,11 @@ ImmutableStringFieldLiteGenerator::ImmutableStringFieldLiteGenerator(
const FieldDescriptor* descriptor, int messageBitIndex, Context* context)
: descriptor_(descriptor),
messageBitIndex_(messageBitIndex),
name_resolver_(context->GetNameResolver()) {
name_resolver_(context->GetNameResolver()),
context_(context) {
SetPrimitiveVariables(descriptor, messageBitIndex, 0,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
ImmutableStringFieldLiteGenerator::~ImmutableStringFieldLiteGenerator() {}
@ -181,6 +194,21 @@ void ImmutableStringFieldLiteGenerator::GenerateInterfaceMembers(
void ImmutableStringFieldLiteGenerator::GenerateMembers(
io::Printer* printer) const {
if (!context_->options().opensource_runtime) {
printer->Print(
variables_,
"@com.google.protobuf.ProtoField(\n"
" fieldNumber=$number$,\n"
" type=com.google.protobuf.FieldType.$annotation_field_type$,\n"
" isRequired=$required$,\n"
" isEnforceUtf8=$enforce_utf8$)\n");
if (HasHazzer(descriptor_)) {
printer->Print(variables_,
"@com.google.protobuf.ProtoPresenceCheckedField(\n"
" presenceBitsId=$bit_field_id$,\n"
" mask=$bit_field_mask$)\n");
}
}
printer->Print(variables_, "private java.lang.String $name$_;\n");
PrintExtraFieldInfo(variables_, printer);
@ -520,10 +548,12 @@ RepeatedImmutableStringFieldLiteGenerator::
RepeatedImmutableStringFieldLiteGenerator(const FieldDescriptor* descriptor,
int messageBitIndex,
Context* context)
: descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
: descriptor_(descriptor),
name_resolver_(context->GetNameResolver()),
context_(context) {
SetPrimitiveVariables(descriptor, messageBitIndex, 0,
context->GetFieldGeneratorInfo(descriptor),
name_resolver_, &variables_);
name_resolver_, &variables_, context);
}
RepeatedImmutableStringFieldLiteGenerator::
@ -554,6 +584,14 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateInterfaceMembers(
void RepeatedImmutableStringFieldLiteGenerator::GenerateMembers(
io::Printer* printer) const {
if (!context_->options().opensource_runtime) {
printer->Print(
variables_,
"@com.google.protobuf.ProtoField(\n"
" fieldNumber=$number$,\n"
" type=com.google.protobuf.FieldType.$annotation_field_type$,\n"
" isEnforceUtf8=$enforce_utf8$)\n");
}
printer->Print(
variables_,
"private com.google.protobuf.Internal.ProtobufList<java.lang.String> "

@ -83,6 +83,7 @@ class ImmutableStringFieldLiteGenerator : public ImmutableFieldLiteGenerator {
std::map<std::string, std::string> variables_;
const int messageBitIndex_;
ClassNameResolver* name_resolver_;
Context* context_;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableStringFieldLiteGenerator);
@ -127,6 +128,7 @@ class RepeatedImmutableStringFieldLiteGenerator
const FieldDescriptor* descriptor_;
std::map<std::string, std::string> variables_;
ClassNameResolver* name_resolver_;
Context* context_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutableStringFieldLiteGenerator);
};

@ -993,6 +993,8 @@
#define PROTOBUF_DLOG(x) \
for (bool b = PROTOBUF_DEBUG; b; b = false) GOOGLE_LOG(x)
#define PROTO2_IS_OSS true
// We don't want code outside port_def doing complex testing, so
// remove our portable condition test macros to nudge folks away from
// using it themselves.

@ -111,6 +111,7 @@
#undef PROTOBUF_GUARDED_BY
#undef PROTOBUF_DEBUG
#undef PROTOBUF_DLOG
#undef PROTO2_IS_OSS
#ifdef PROTOBUF_FUTURE_BREAKING_CHANGES
#undef PROTOBUF_FUTURE_BREAKING_CHANGES

Loading…
Cancel
Save