diff --git a/BUILD.bazel b/BUILD.bazel index 3734b49ed9..9dd4bbdb05 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,12 +1,11 @@ # Bazel (https://bazel.build/) BUILD file for Protobuf. -load("@bazel_skylib//rules:common_settings.bzl", "string_flag") -load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library", "cc_test") +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library") -load("@rules_java//java:defs.bzl", "java_binary", "java_lite_proto_library", "java_proto_library") +load("@rules_java//java:defs.bzl", "java_lite_proto_library", "java_proto_library") load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS", "PROTOC_LINK_OPTS") -load(":protobuf.bzl", "internal_py_proto_library", "internal_php_proto_library", "internal_ruby_proto_library", "internal_objc_proto_library") +load(":protobuf.bzl", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library", "internal_ruby_proto_library") licenses(["notice"]) @@ -438,9 +437,9 @@ java_lite_proto_library( internal_objc_proto_library( name = "test_messages_proto2_objc_proto", + testonly = 1, srcs = ["//src/google/protobuf:test_messages_proto2.proto"], includes = ["src/google/protobuf"], - testonly = 1, visibility = [ "//conformance:__pkg__", "//objectivec:__subpackages__", @@ -449,10 +448,14 @@ internal_objc_proto_library( internal_objc_proto_library( name = "test_messages_proto3_objc_proto", + testonly = 1, srcs = ["//src/google/protobuf:test_messages_proto3.proto"], + includes = [ + "src/google/protobuf", + # The above must come first. + "src", + ], proto_deps = [":well_known_protos"], - includes = ["src/google/protobuf", "src"], - testonly = 1, visibility = [ "//conformance:__pkg__", "//objectivec:__subpackages__", @@ -461,10 +464,10 @@ internal_objc_proto_library( internal_php_proto_library( name = "test_messages_proto3_php_proto", + testonly = 1, srcs = ["//src/google/protobuf:test_messages_proto3.proto"], - proto_deps = [":well_known_protos"], - includes = ["src/google/protobuf", "src"], outs = [ + "GPBMetadata/TestMessagesProto3.php", "Protobuf_test_messages/Proto3/EnumOnlyProto3.php", "Protobuf_test_messages/Proto3/EnumOnlyProto3/PBBool.php", "Protobuf_test_messages/Proto3/ForeignEnum.php", @@ -474,9 +477,13 @@ internal_php_proto_library( "Protobuf_test_messages/Proto3/TestAllTypesProto3/AliasedEnum.php", "Protobuf_test_messages/Proto3/TestAllTypesProto3/NestedEnum.php", "Protobuf_test_messages/Proto3/TestAllTypesProto3/NestedMessage.php", - "GPBMetadata/TestMessagesProto3.php", ], - testonly = 1, + includes = [ + "src/google/protobuf", + # The above must come first. + "src", + ], + proto_deps = [":well_known_protos"], visibility = [ "//conformance:__pkg__", "//php:__subpackages__", @@ -485,9 +492,9 @@ internal_php_proto_library( internal_ruby_proto_library( name = "test_messages_proto2_ruby_proto", + testonly = 1, srcs = ["//src/google/protobuf:test_messages_proto2.proto"], includes = ["src/google/protobuf"], - testonly = 1, visibility = [ "//conformance:__pkg__", "//ruby:__subpackages__", @@ -496,10 +503,14 @@ internal_ruby_proto_library( internal_ruby_proto_library( name = "test_messages_proto3_ruby_proto", + testonly = 1, srcs = ["//src/google/protobuf:test_messages_proto3.proto"], + includes = [ + "src/google/protobuf", + # The above must come first. + "src", + ], proto_deps = [":well_known_protos"], - includes = ["src/google/protobuf", "src"], - testonly = 1, visibility = [ "//conformance:__pkg__", "//ruby:__subpackages__", diff --git a/benchmarks/BUILD.bazel b/benchmarks/BUILD.bazel index 46c908bd69..60a9092854 100644 --- a/benchmarks/BUILD.bazel +++ b/benchmarks/BUILD.bazel @@ -2,7 +2,7 @@ load("@rules_cc//cc:defs.bzl", "cc_proto_library") load("@rules_java//java:defs.bzl", "java_proto_library") load("@rules_pkg//:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix") load("@rules_proto//proto:defs.bzl", "proto_library") -load("//:protobuf.bzl", "internal_py_proto_library", "internal_php_proto_library") +load("//:protobuf.bzl", "internal_php_proto_library", "internal_py_proto_library") ################################################################################ # Benchmark Protos diff --git a/benchmarks/cpp/cpp_benchmark.cc b/benchmarks/cpp/cpp_benchmark.cc index 0cfb9169e1..ffb3a3dc66 100644 --- a/benchmarks/cpp/cpp_benchmark.cc +++ b/benchmarks/cpp/cpp_benchmark.cc @@ -30,6 +30,7 @@ #include #include + #include "benchmark/benchmark.h" #include "benchmarks.pb.h" #include "benchmarks/datasets/google_message1/proto2/benchmark_message1_proto2.pb.h" @@ -38,7 +39,6 @@ #include "benchmarks/datasets/google_message3/benchmark_message3.pb.h" #include "benchmarks/datasets/google_message4/benchmark_message4.pb.h" - #define PREFIX "dataset." #define SUFFIX ".pb" diff --git a/benchmarks/datasets/BUILD.bazel b/benchmarks/datasets/BUILD.bazel index 4d4b195e70..d3d8cdf027 100644 --- a/benchmarks/datasets/BUILD.bazel +++ b/benchmarks/datasets/BUILD.bazel @@ -90,21 +90,18 @@ py_library( filegroup( name = "php_protos", - visibility = [ - "//benchmarks:__subpackages__", - ], srcs = [ "//benchmarks/datasets/google_message1/proto2:benchmark_message1_proto2_php_proto", "//benchmarks/datasets/google_message1/proto3:benchmark_message1_proto3_php_proto", "//benchmarks/datasets/google_message2:benchmark_message2_php_proto", ], + visibility = [ + "//benchmarks:__subpackages__", + ], ) filegroup( name = "csharp_protos", - visibility = [ - "//benchmarks:__subpackages__", - ], srcs = [ "//benchmarks/datasets/google_message1/proto2:benchmark_message1_proto2_csharp_proto", "//benchmarks/datasets/google_message1/proto3:benchmark_message1_proto3_csharp_proto", @@ -112,6 +109,9 @@ filegroup( "//benchmarks/datasets/google_message3:benchmark_message3_csharp_proto", "//benchmarks/datasets/google_message4:benchmark_message4_csharp_proto", ], + visibility = [ + "//benchmarks:__subpackages__", + ], ) pkg_files( diff --git a/benchmarks/datasets/google_message1/proto2/BUILD.bazel b/benchmarks/datasets/google_message1/proto2/BUILD.bazel index b7ee62ccbb..0bde1e8e28 100644 --- a/benchmarks/datasets/google_message1/proto2/BUILD.bazel +++ b/benchmarks/datasets/google_message1/proto2/BUILD.bazel @@ -2,8 +2,8 @@ load("@rules_cc//cc:defs.bzl", "cc_proto_library") load("@rules_java//java:defs.bzl", "java_proto_library") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_proto//proto:defs.bzl", "proto_library") -load("//:protobuf.bzl", "internal_py_proto_library", "internal_csharp_proto_library") -load("//benchmarks/util:compatibility.bzl", "proto3_from_proto2_data", "php_proto3_from_proto2_library") +load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_py_proto_library") +load("//benchmarks/util:compatibility.bzl", "php_proto3_from_proto2_library", "proto3_from_proto2_data") filegroup( name = "datasets", diff --git a/benchmarks/datasets/google_message1/proto3/BUILD.bazel b/benchmarks/datasets/google_message1/proto3/BUILD.bazel index ae9b4009cd..37cbf09af5 100644 --- a/benchmarks/datasets/google_message1/proto3/BUILD.bazel +++ b/benchmarks/datasets/google_message1/proto3/BUILD.bazel @@ -2,7 +2,7 @@ load("@rules_cc//cc:defs.bzl", "cc_proto_library") load("@rules_java//java:defs.bzl", "java_proto_library") load("@rules_proto//proto:defs.bzl", "proto_library") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") -load("//:protobuf.bzl", "internal_py_proto_library", "internal_php_proto_library", "internal_csharp_proto_library") +load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_php_proto_library", "internal_py_proto_library") filegroup( name = "datasets", diff --git a/benchmarks/datasets/google_message2/BUILD.bazel b/benchmarks/datasets/google_message2/BUILD.bazel index 654a3e2fef..d4e52d4037 100644 --- a/benchmarks/datasets/google_message2/BUILD.bazel +++ b/benchmarks/datasets/google_message2/BUILD.bazel @@ -2,8 +2,8 @@ load("@rules_cc//cc:defs.bzl", "cc_proto_library") load("@rules_java//java:defs.bzl", "java_proto_library") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_proto//proto:defs.bzl", "proto_library") -load("//:protobuf.bzl", "internal_py_proto_library", "internal_csharp_proto_library") -load("//benchmarks/util:compatibility.bzl", "proto3_from_proto2_data", "php_proto3_from_proto2_library") +load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_py_proto_library") +load("//benchmarks/util:compatibility.bzl", "php_proto3_from_proto2_library", "proto3_from_proto2_data") filegroup( name = "datasets", diff --git a/benchmarks/datasets/google_message3/BUILD.bazel b/benchmarks/datasets/google_message3/BUILD.bazel index 68ad3f8df9..1f9d0c5353 100644 --- a/benchmarks/datasets/google_message3/BUILD.bazel +++ b/benchmarks/datasets/google_message3/BUILD.bazel @@ -2,7 +2,7 @@ load("@rules_cc//cc:defs.bzl", "cc_proto_library") load("@rules_java//java:defs.bzl", "java_proto_library") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_proto//proto:defs.bzl", "proto_library") -load("//:protobuf.bzl", "internal_py_proto_library", "internal_csharp_proto_library") +load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_py_proto_library") filegroup( name = "datasets", diff --git a/benchmarks/datasets/google_message4/BUILD.bazel b/benchmarks/datasets/google_message4/BUILD.bazel index 079a9a2d5b..a19ffbbf39 100644 --- a/benchmarks/datasets/google_message4/BUILD.bazel +++ b/benchmarks/datasets/google_message4/BUILD.bazel @@ -2,7 +2,7 @@ load("@rules_cc//cc:defs.bzl", "cc_proto_library") load("@rules_java//java:defs.bzl", "java_proto_library") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_proto//proto:defs.bzl", "proto_library") -load("//:protobuf.bzl", "internal_py_proto_library", "internal_csharp_proto_library") +load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_py_proto_library") filegroup( name = "datasets", diff --git a/benchmarks/internal.bzl b/benchmarks/internal.bzl index c060353d13..485921f5c6 100644 --- a/benchmarks/internal.bzl +++ b/benchmarks/internal.bzl @@ -2,6 +2,7 @@ PLEASE DO NOT DEPEND ON THE CONTENTS OF THIS FILE, IT IS UNSTABLE. """ + load("//build_defs:internal_shell.bzl", "inline_sh_binary") def internal_benchmark_test( @@ -31,7 +32,8 @@ def internal_benchmark_test( " ".join(env_vars), binary, " ".join(args), - " ".join(dataset_labels)), + " ".join(dataset_labels), + ), tags = ["benchmark"], testonly = 1, ) diff --git a/benchmarks/java/BUILD.bazel b/benchmarks/java/BUILD.bazel index f8976bf664..1e118091f7 100644 --- a/benchmarks/java/BUILD.bazel +++ b/benchmarks/java/BUILD.bazel @@ -1,4 +1,4 @@ -load("@rules_java//java:defs.bzl", "java_library", "java_binary") +load("@rules_java//java:defs.bzl", "java_binary", "java_library") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("//build_defs:internal_shell.bzl", "inline_sh_binary") load("//benchmarks:internal.bzl", "internal_benchmark_test") @@ -22,7 +22,6 @@ java_binary( inline_sh_binary( name = "java_benchmark", srcs = ["//benchmarks/datasets"], - tools = [":java_benchmark_jar"], cmd = """ data_files="" conf=() @@ -35,7 +34,8 @@ inline_sh_binary( done $(rootpath :java_benchmark_jar) com.google.protobuf.ProtoCaliperBenchmark \ -i runtime -DdataFile=$${data_files:0:-1} $${conf[*]} - """ + """, + tools = [":java_benchmark_jar"], ) # A pre-configured binary using the checked in datasets. diff --git a/benchmarks/php/BUILD.bazel b/benchmarks/php/BUILD.bazel index a1a9319909..a00f23b970 100644 --- a/benchmarks/php/BUILD.bazel +++ b/benchmarks/php/BUILD.bazel @@ -5,58 +5,58 @@ load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") # The benchmark binary which can be run over any dataset. inline_sh_binary( name = "php_benchmark", + testonly = 1, srcs = [ "PhpBenchmark.php", "autoload.php", ], - deps = [ - "//benchmarks:benchmarks_php_proto", - "//benchmarks/datasets:php_protos", - "//php:source_files", - ], cmd = """ php -d include_path=benchmarks:php/src \\ -d auto_prepend_file=$(rootpath autoload.php) \\ $(rootpath PhpBenchmark.php) $$@ """, - testonly = 1, + deps = [ + "//benchmarks:benchmarks_php_proto", + "//benchmarks/datasets:php_protos", + "//php:source_files", + ], ) # A pre-configured binary using the checked in datasets. internal_benchmark_test( name = "php", + args = ["--behavior_prefix='php'"], binary = ":php_benchmark", datasets = ["//benchmarks/datasets:proto3_datasets"], env_vars = ["PROTOBUF_PHP_SRCDIR=php/src"], - args = ["--behavior_prefix='php'"], ) # The benchmark binary which can be run over any dataset. inline_sh_binary( name = "php_c_benchmark", + testonly = 1, srcs = [ "PhpBenchmark.php", "//php:extension", ], - deps = [ - "//benchmarks:benchmarks_php_proto", - "//benchmarks/datasets:php_protos", - ], cmd = """ php -d include_path=benchmarks:php/src \\ -dextension=$(rootpath //php:extension) \\ $(rootpath PhpBenchmark.php) $$@ """, - testonly = 1, + deps = [ + "//benchmarks:benchmarks_php_proto", + "//benchmarks/datasets:php_protos", + ], ) # A pre-configured binary using the checked in datasets. internal_benchmark_test( name = "php_c", + args = ["--behavior_prefix='php_c'"], binary = ":php_c_benchmark", datasets = ["//benchmarks/datasets:proto3_datasets"], env_vars = ["PROTOBUF_PHP_SRCDIR=php/src"], - args = ["--behavior_prefix='php_c'"], ) pkg_files( diff --git a/benchmarks/python/BUILD.bazel b/benchmarks/python/BUILD.bazel index a8b1801fb9..f1d85f134c 100644 --- a/benchmarks/python/BUILD.bazel +++ b/benchmarks/python/BUILD.bazel @@ -1,37 +1,37 @@ load("@rules_cc//cc:defs.bzl", "cc_binary") -load("@rules_python//python:defs.bzl", "py_library", "py_binary") +load("@rules_python//python:defs.bzl", "py_binary", "py_library") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("//benchmarks:internal.bzl", "internal_benchmark_test") cc_binary( name = "libbenchmark_messages.so", srcs = ["python_benchmark_messages.cc"], + linkshared = True, + linkstatic = True, + visibility = ["//visibility:private"], deps = [ - "@local_config_python//:python_headers", - "//external:python_headers", "//benchmarks:benchmarks_cc_proto", "//benchmarks/datasets:cc_protos", + "//external:python_headers", + "@local_config_python//:python_headers", ], - linkshared = True, - linkstatic = True, - visibility = ["//visibility:private"], ) # The benchmark binary which can be run over any dataset. py_binary( name = "python_benchmark", srcs = ["py_benchmark.py"], - main = "py_benchmark.py", data = ["libbenchmark_messages.so"], + env = select({ + "//python:use_fast_cpp_protos": {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "cpp"}, + "//conditions:default": {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "python"}, + }), + main = "py_benchmark.py", deps = [ "//:protobuf_python", "//benchmarks:benchmarks_py_proto", "//benchmarks/datasets:py_protos", ], - env = select({ - "//python:use_fast_cpp_protos": {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "cpp"}, - "//conditions:default": {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "python"}, - }), ) # Pre-configured binaries using the checked in datasets. @@ -54,10 +54,10 @@ internal_benchmark_test( # Note: this requires --define=use_fast_cpp_protos=true internal_benchmark_test( name = "cpp_generated_code", + args = ["--cpp_generated"], binary = ":python_benchmark", datasets = ["//benchmarks/datasets"], env_vars = ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp"], - args = ["--cpp_generated"], ) pkg_files( diff --git a/benchmarks/python/py_benchmark.py b/benchmarks/python/py_benchmark.py index 053c78cc31..76784f2676 100644 --- a/benchmarks/python/py_benchmark.py +++ b/benchmarks/python/py_benchmark.py @@ -6,6 +6,12 @@ import math import argparse import fnmatch import json +from benchmarks.datasets.google_message1.proto2 import benchmark_message1_proto2_pb2 +from benchmarks.datasets.google_message1.proto3 import benchmark_message1_proto3_pb2 +from benchmarks.datasets.google_message2 import benchmark_message2_pb2 +from benchmarks.datasets.google_message3 import benchmark_message3_pb2 +from benchmarks.datasets.google_message4 import benchmark_message4_pb2 +from benchmarks import benchmarks_pb2 parser = argparse.ArgumentParser(description="Python protobuf benchmark") parser.add_argument("data_files", metavar="dataFile", nargs="+", @@ -27,19 +33,11 @@ args = parser.parse_args() # for the descriptor can be found in the pool if args.cpp_generated != "no": sys.path.append( os.path.dirname( os.path.dirname( os.path.abspath(__file__) ) ) + "/.libs" ) - from benchmarks.python import libbenchmark_messages + from benchmarks.python import libbenchmark_messages # pylint: disable=unused-import,g-import-not-at-top sys.path.append( os.path.dirname( os.path.dirname( os.path.abspath(__file__) ) ) + "/tmp" ) # END CPP GENERATED MESSAGE -import benchmarks.datasets.google_message1.proto2.benchmark_message1_proto2_pb2 as benchmark_message1_proto2_pb2 -import benchmarks.datasets.google_message1.proto3.benchmark_message1_proto3_pb2 as benchmark_message1_proto3_pb2 -import benchmarks.datasets.google_message2.benchmark_message2_pb2 as benchmark_message2_pb2 -import benchmarks.datasets.google_message3.benchmark_message3_pb2 as benchmark_message3_pb2 -import benchmarks.datasets.google_message4.benchmark_message4_pb2 as benchmark_message4_pb2 -import benchmarks.benchmarks_pb2 as benchmarks_pb2 - - def run_one_test(filename): data = open(filename, "rb").read() benchmark_dataset = benchmarks_pb2.BenchmarkDataset() @@ -135,9 +133,9 @@ class Benchmark: if t < 3 : reps = int(math.ceil(3 / t)) * self.full_iteration if reps != self.full_iteration: - t = timeit.timeit(stmt="%s(%s)" % (self.test_method, test_method_args), - setup=self.full_setup_code(setup_method_args), - number=reps); + t = timeit.timeit(stmt="%s(%s)" % (self.test_method, test_method_args), + setup=self.full_setup_code(setup_method_args), + number=reps); return self.total_bytes * 1.0 / 2 ** 20 / (1.0 * t / reps * self.full_iteration) diff --git a/benchmarks/util/BUILD.bazel b/benchmarks/util/BUILD.bazel index 6b3aa2beb1..256862e253 100644 --- a/benchmarks/util/BUILD.bazel +++ b/benchmarks/util/BUILD.bazel @@ -7,15 +7,15 @@ package(default_visibility = ["//benchmarks:__subpackages__"]) cc_binary( name = "protoc-gen-proto2_to_proto3", srcs = [ - "schema_proto2_to_proto3_util.h", "protoc-gen-proto2_to_proto3.cc", + "schema_proto2_to_proto3_util.h", ], + visibility = ["//benchmarks:__subpackages__"], deps = [ "//:protobuf", - "//src/google/protobuf/compiler:code_generator", "//benchmarks:benchmarks_cc_proto", + "//src/google/protobuf/compiler:code_generator", ], - visibility = ["//benchmarks:__subpackages__"], ) cc_binary( @@ -26,8 +26,8 @@ cc_binary( ], deps = [ "//:protobuf", - "//benchmarks/datasets:cc_protos", "//benchmarks:benchmarks_cc_proto", + "//benchmarks/datasets:cc_protos", ], ) diff --git a/benchmarks/util/compatibility.bzl b/benchmarks/util/compatibility.bzl index 044ade6d0f..840fd45b23 100644 --- a/benchmarks/util/compatibility.bzl +++ b/benchmarks/util/compatibility.bzl @@ -6,9 +6,9 @@ PLEASE DO NOT DEPEND ON THE CONTENTS OF THIS FILE, IT IS UNSTABLE. load("//:protobuf.bzl", "internal_php_proto_library") def proto3_from_proto2_data( - name, - srcs, - **kwargs): + name, + srcs, + **kwargs): """Transforms proto2 binary data into a proto3-compatible format, Args: @@ -22,7 +22,7 @@ def proto3_from_proto2_data( for src in srcs: outs.append("proto3/" + src) out_files.append("$(RULEDIR)/proto3/" + src) - src_files.append("$(rootpath %s)" % src); + src_files.append("$(rootpath %s)" % src) native.genrule( name = name + "_genrule", @@ -32,13 +32,15 @@ def proto3_from_proto2_data( ], outs = outs, cmd = "$(execpath //benchmarks/util:proto3_data_stripper) %s %s" % ( - " ".join(src_files), " ".join(out_files)), + " ".join(src_files), + " ".join(out_files), + ), ) native.filegroup( name = name, srcs = outs, - **kwargs, + **kwargs ) def _proto3_from_proto2_library( @@ -56,7 +58,7 @@ def _proto3_from_proto2_library( src_files = [] for src in srcs: outs.append(src + "3") - src_files.append("$(rootpath %s)" % src); + src_files.append("$(rootpath %s)" % src) native.genrule( name = name, @@ -74,7 +76,7 @@ def _proto3_from_proto2_library( --proto2_to_proto3_out=$(GENDIR) \ %s """ % (" ".join(src_files)), - **kwargs, + **kwargs ) def php_proto3_from_proto2_library( @@ -99,5 +101,5 @@ def php_proto3_from_proto2_library( name = name, srcs = [name + "_genrule"], outs = outs, - **kwargs, + **kwargs ) diff --git a/benchmarks/util/proto3_data_stripper.cc b/benchmarks/util/proto3_data_stripper.cc index 68f49054d4..c34745e940 100644 --- a/benchmarks/util/proto3_data_stripper.cc +++ b/benchmarks/util/proto3_data_stripper.cc @@ -1,3 +1,5 @@ +#include + #include "benchmarks.pb.h" #include "benchmarks/datasets/google_message1/proto2/benchmark_message1_proto2.pb.h" #include "benchmarks/datasets/google_message1/proto3/benchmark_message1_proto3.pb.h" @@ -6,8 +8,6 @@ #include "benchmarks/datasets/google_message4/benchmark_message4.pb.h" #include "data_proto2_to_proto3_util.h" -#include - using google::protobuf::util::Proto3DataStripper; std::string ReadFile(const std::string& name) { diff --git a/benchmarks/util/protoc-gen-proto2_to_proto3.cc b/benchmarks/util/protoc-gen-proto2_to_proto3.cc index 3073e0f02f..cc550ced76 100644 --- a/benchmarks/util/protoc-gen-proto2_to_proto3.cc +++ b/benchmarks/util/protoc-gen-proto2_to_proto3.cc @@ -7,15 +7,15 @@ #include "google/protobuf/compiler/plugin.h" -using google::protobuf::FileDescriptorProto; -using google::protobuf::FileDescriptor; using google::protobuf::DescriptorPool; +using google::protobuf::FileDescriptor; +using google::protobuf::FileDescriptorProto; using google::protobuf::io::Printer; -using google::protobuf::util::SchemaGroupStripper; using google::protobuf::util::EnumScrubber; using google::protobuf::util::ExtensionStripper; using google::protobuf::util::FieldScrubber; using google::protobuf::util::ImportScrubber; +using google::protobuf::util::SchemaGroupStripper; namespace google { namespace protobuf { @@ -37,9 +37,8 @@ DescriptorPool* GetPool() { class Proto2ToProto3Generator final : public CodeGenerator { public: bool GenerateAll(const std::vector& files, - const std::string& parameter, - GeneratorContext* context, - std::string* error) const { + const std::string& parameter, GeneratorContext* context, + std::string* error) const { for (int i = 0; i < files.size(); i++) { for (auto file : files) { if (CanGenerate(file)) { @@ -52,10 +51,8 @@ class Proto2ToProto3Generator final : public CodeGenerator { return true; } - bool Generate(const FileDescriptor* file, - const std::string& parameter, - GeneratorContext* context, - std::string* error) const { + bool Generate(const FileDescriptor* file, const std::string& parameter, + GeneratorContext* context, std::string* error) const { FileDescriptorProto new_file; file->CopyTo(&new_file); new_file.set_name(ImportScrubber::ScrubFilename(file->name())); @@ -71,7 +68,7 @@ class Proto2ToProto3Generator final : public CodeGenerator { std::string filename = file->name(); std::string basename = StripProtoExt(filename); - std::vector> option_pairs; + std::vector> option_pairs; ParseGeneratorParameter(parameter, &option_pairs); std::unique_ptr output( @@ -82,14 +79,16 @@ class Proto2ToProto3Generator final : public CodeGenerator { return true; } + private: bool CanGenerate(const FileDescriptor* file) const { - if (GetPool()->FindFileByName(ImportScrubber::ScrubFilename(file->name())) != nullptr) { + if (GetPool()->FindFileByName( + ImportScrubber::ScrubFilename(file->name())) != nullptr) { return false; } for (int j = 0; j < file->dependency_count(); j++) { if (GetPool()->FindFileByName(ImportScrubber::ScrubFilename( - file->dependency(j)->name())) == nullptr) { + file->dependency(j)->name())) == nullptr) { return false; } } diff --git a/benchmarks/util/result_parser.py b/benchmarks/util/result_parser.py index a35c1dbbb2..d61b3c0cd7 100644 --- a/benchmarks/util/result_parser.py +++ b/benchmarks/util/result_parser.py @@ -1,12 +1,14 @@ +"""Parses benchmark results into a standardized json output.""" + import argparse import json import re import os.path +from benchmarks import benchmarks_pb2 # BEGIN OPENSOURCE import sys sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) # END OPENSOURCE -from benchmarks import benchmarks_pb2 __file_size_map = {} diff --git a/benchmarks/util/schema_proto2_to_proto3_util.h b/benchmarks/util/schema_proto2_to_proto3_util.h index ea7b796560..bd6ff26ca4 100644 --- a/benchmarks/util/schema_proto2_to_proto3_util.h +++ b/benchmarks/util/schema_proto2_to_proto3_util.h @@ -189,11 +189,11 @@ class FieldScrubber { class ImportScrubber { public: - static std::string ScrubFilename(const std::string& filename) { + static std::string ScrubFilename(const std::string &filename) { return filename + "3"; } static void ScrubFile(FileDescriptorProto *file) { - for(std::string& import: *file->mutable_dependency()) { + for (std::string &import : *file->mutable_dependency()) { import += "3"; } } diff --git a/build_defs/arch_tests.bzl b/build_defs/arch_tests.bzl index 7f8a6af515..61fa00c944 100644 --- a/build_defs/arch_tests.bzl +++ b/build_defs/arch_tests.bzl @@ -9,9 +9,7 @@ def _arch_test_impl( bazel_binaries = [], system_binaries = [], **kwargs): - """ - Bazel rule to verify that a Bazel or system binary is built for the - aarch64 architecture. + """Bazel rule to verify that a Bazel or system binary is built for the aarch64 architecture. Args: name: the name of the test. @@ -19,7 +17,7 @@ def _arch_test_impl( file_platform: the expected output of `file`. bazel_binaries: a set of binary targets to inspect. system_binaries: a set of paths to system executables to inspect. - **kargs: other keyword arguments that are passed to the test. + **kwargs: other keyword arguments that are passed to the test. """ inline_sh_test( @@ -30,27 +28,29 @@ def _arch_test_impl( (file -L $$binary | grep -q "%s") \ || (echo "Test binary is not an %s binary: "; file -L $$binary; exit 1) done - """ % (" ".join(bazel_binaries), - " ".join(system_binaries), - file_platform, - platform), + """ % ( + " ".join(bazel_binaries), + " ".join(system_binaries), + file_platform, + platform, + ), target_compatible_with = select({ - "//build_defs:"+platform: [], - "//conditions:default": ["@platforms//:incompatible"], + "//build_defs:" + platform: [], + "//conditions:default": ["@platforms//:incompatible"], }), + **kwargs ) - def aarch64_test(**kwargs): - _arch_test_impl( - platform = "aarch64", - file_platform = "ELF 64-bit LSB executable, ARM aarch64", - **kwargs, - ) + _arch_test_impl( + platform = "aarch64", + file_platform = "ELF 64-bit LSB executable, ARM aarch64", + **kwargs + ) def x86_64_test(**kwargs): - _arch_test_impl( - platform = "x86_64", - file_platform = "ELF 64-bit LSB executable, ARM x86_64", - **kwargs, - ) + _arch_test_impl( + platform = "x86_64", + file_platform = "ELF 64-bit LSB executable, ARM x86_64", + **kwargs + ) diff --git a/build_defs/internal_shell.bzl b/build_defs/internal_shell.bzl index 0dda1464a5..670c4d5f56 100644 --- a/build_defs/internal_shell.bzl +++ b/build_defs/internal_shell.bzl @@ -1,3 +1,8 @@ +""" +Internal tools to migrate shell commands to Bazel as an intermediate step +to wider Bazelification. +""" + def inline_sh_binary( name, srcs = [], @@ -6,9 +11,11 @@ def inline_sh_binary( cmd = "", testonly = None, **kwargs): - """Bazel rule to wrap up an inline bash script in a binary. This is most - useful as a stop-gap solution for migrating off Autotools. These binaries - are likely to be non-hermetic, with implicit system dependencies. + """Bazel rule to wrap up an inline bash script in a binary. + + This is most useful as a stop-gap solution for migrating off Autotools. + These binaries are likely to be non-hermetic, with implicit system + dependencies. NOTE: the rule is only an internal workaround. The interface may change and the rule may be removed when everything is properly "Bazelified". @@ -19,12 +26,12 @@ def inline_sh_binary( tools: the executable tools used directly by the script. Any target used with rootpath/execpath/location must be declared here or in `srcs`. deps: a list of dependency labels that are required to run this binary. - **kargs: other keyword arguments that are passed to sh_binary. + cmd: the inline sh command to run. + **kwargs: other keyword arguments that are passed to sh_binary. testonly: common rule attribute (see: https://bazel.build/reference/be/common-definitions#common-attributes) """ - native.genrule( name = name + "_genrule", srcs = srcs, @@ -50,9 +57,10 @@ def inline_sh_test( deps = [], cmd = "", **kwargs): - """Bazel rule to wrap up an inline bash script in a test. This is most - useful as a stop-gap solution for migrating off Autotools. These tests - are likely to be non-hermetic, with implicit system dependencies. + """Bazel rule to wrap up an inline bash script in a test. + + This is most useful as a stop-gap solution for migrating off Autotools. + These tests are likely to be non-hermetic, with implicit system dependencies. NOTE: the rule is only an internal workaround. The interface may change and the rule may be removed when everything is properly "Bazelified". @@ -63,11 +71,11 @@ def inline_sh_test( tools: the executable tools used directly by the script. Any target used with rootpath/execpath/location must be declared here or in `srcs`. deps: a list of dependency labels that are required to run this binary. - **kargs: other keyword arguments that are passed to sh_binary. + cmd: the inline sh command to run. + **kwargs: other keyword arguments that are passed to sh_binary. https://bazel.build/reference/be/common-definitions#common-attributes) """ - native.genrule( name = name + "_genrule", srcs = srcs, diff --git a/conformance/BUILD.bazel b/conformance/BUILD.bazel index 0e7ad5929b..28b3628d6f 100644 --- a/conformance/BUILD.bazel +++ b/conformance/BUILD.bazel @@ -1,8 +1,7 @@ # Conformance testing for Protobuf. load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library", "objc_library") -load("@rules_python//python:defs.bzl", "py_library") -load("//:protobuf.bzl", "internal_py_proto_library", "internal_php_proto_library", "internal_csharp_proto_library", "internal_ruby_proto_library", "internal_objc_proto_library") +load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library", "internal_ruby_proto_library") load("//build_defs:internal_shell.bzl", "inline_sh_binary") load( "@rules_pkg//:mappings.bzl", @@ -94,10 +93,10 @@ internal_objc_proto_library( internal_py_proto_library( name = "conformance_py_proto", srcs = ["conformance.proto"], + srcs_version = "PY2AND3", visibility = [ "//python:__subpackages__", ], - srcs_version = "PY2AND3", ) internal_php_proto_library( @@ -183,21 +182,25 @@ cc_binary( cc_binary( name = "conformance_cpp", + testonly = 1, srcs = ["conformance_cpp.cc"], + visibility = ["//src:__subpackages__"], deps = [ ":conformance_cc_proto", "//:protobuf", "//:test_messages_proto2_cc_proto", "//:test_messages_proto3_cc_proto", ], - testonly = 1, - visibility = ["//src:__subpackages__"], ) java_binary( name = "conformance_java", + testonly = 1, srcs = ["ConformanceJava.java"], main_class = "ConformanceJava", + visibility = [ + "//java:__subpackages__", + ], deps = [ ":conformance_java_proto", "//:protobuf_java", @@ -205,14 +208,12 @@ java_binary( "//:test_messages_proto2_java_proto", "//:test_messages_proto3_java_proto", ], - testonly = 1, - visibility = [ - "//java:__subpackages__", - ], ) java_binary( name = "conformance_java_lite", + testonly = 1, + srcs = ["ConformanceJavaLite.java"], main_class = "ConformanceJavaLite", visibility = [ "//java:__subpackages__", @@ -224,110 +225,111 @@ java_binary( "//:test_messages_proto2_java_proto_lite", "//:test_messages_proto3_java_proto_lite", ], - testonly = 1, - srcs = ["ConformanceJavaLite.java"], ) py_binary( name = "conformance_python", + testonly = 1, srcs = ["conformance_python.py"], + imports = [ + "..", + "../python", + ], + srcs_version = "PY2AND3", + visibility = ["//python:__subpackages__"], deps = [ ":conformance_py_proto", "//:protobuf_python", "//python:test_messages_proto2_py_proto", "//python:test_messages_proto3_py_proto", ], - imports = [".", "../python"], - srcs_version = "PY2AND3", - testonly = 1, - visibility = ["//python:__subpackages__"], ) inline_sh_binary( name = "conformance_php", + testonly = 1, srcs = [ - "conformance_php.php", "autoload.php", - ], - deps = [ - ":conformance_php_proto", - "//php:source_files", - "//:test_messages_proto3_php_proto", + "conformance_php.php", ], cmd = """ php -d include_path=conformance:src/google/protobuf \\ -d auto_prepend_file=$(rootpath autoload.php) \\ $(rootpath conformance_php.php) """, - testonly = 1, visibility = ["//php:__subpackages__"], + deps = [ + ":conformance_php_proto", + "//:test_messages_proto3_php_proto", + "//php:source_files", + ], ) inline_sh_binary( name = "conformance_php_c", + testonly = 1, srcs = [ "conformance_php.php", "//php:extension", ], - deps = [ - ":conformance_php_proto", - "//:test_messages_proto3_php_proto", - ], cmd = """ php -dextension=$(rootpath //php:extension) \\ -d include_path=conformance:src/google/protobuf \\ $(rootpath conformance_php.php) """, - testonly = 1, visibility = ["//php:__subpackages__"], + deps = [ + ":conformance_php_proto", + "//:test_messages_proto3_php_proto", + ], ) inline_sh_binary( name = "conformance_csharp", + testonly = 1, srcs = ["//csharp/src/Google.Protobuf.Conformance:conformance_dll"], + cmd = "dotnet $(rootpath //csharp/src/Google.Protobuf.Conformance:conformance_dll)", + visibility = ["//csharp:__subpackages__"], deps = [ "//csharp/src/Google.Protobuf.Conformance:conformance_runfiles", ], - cmd = "dotnet $(rootpath //csharp/src/Google.Protobuf.Conformance:conformance_dll)", - testonly = 1, - visibility = ["//csharp:__subpackages__"], ) objc_library( name = "conformance_objc_lib", + testonly = 1, non_arc_srcs = ["conformance_objc.m"], + # See https://github.com/bazelbuild/bazel/issues/12897. + tags = ["manual"], deps = [ ":conformance_objc_proto", "//:test_messages_proto2_objc_proto", "//:test_messages_proto3_objc_proto", ], - testonly = 1, - # See https://github.com/bazelbuild/bazel/issues/12897. - tags = ["manual"], ) cc_binary( name = "conformance_objc", - deps = [":conformance_objc_lib"], testonly = 1, # See https://github.com/bazelbuild/bazel/issues/12897. tags = ["manual"], visibility = ["//objectivec:__subpackages__"], + deps = [":conformance_objc_lib"], ) inline_sh_binary( name = "conformance_ruby", + testonly = 1, srcs = ["conformance_ruby.rb"], + cmd = "RUBYLIB=ruby/lib:conformance:src $(rootpath conformance_ruby.rb)", + visibility = ["//ruby:__subpackages__"], deps = [ ":conformance_ruby_proto", - "//:well_known_ruby_protos", "//:test_messages_proto2_ruby_proto", "//:test_messages_proto3_ruby_proto", + "//:well_known_ruby_protos", "//ruby:protobuf", ], - cmd = "RUBYLIB=ruby/lib:conformance:src $(rootpath conformance_ruby.rb)", - testonly = 1, - visibility = ["//ruby:__subpackages__"], ) ################################################################################ diff --git a/conformance/conformance_cpp.cc b/conformance/conformance_cpp.cc index cbfd8b53bd..85fa9d0b5c 100644 --- a/conformance/conformance_cpp.cc +++ b/conformance/conformance_cpp.cc @@ -45,6 +45,7 @@ #include #include #include "conformance/conformance.pb.h" +#include "conformance/conformance.pb.h" #include #include #include diff --git a/conformance/conformance_python.py b/conformance/conformance_python.py index 37ee36e24c..cffadc9478 100755 --- a/conformance/conformance_python.py +++ b/conformance/conformance_python.py @@ -42,7 +42,7 @@ from google.protobuf import message from google.protobuf import test_messages_proto3_pb2 from google.protobuf import test_messages_proto2_pb2 from google.protobuf import text_format -import conformance_pb2 +from conformance import conformance_pb2 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) diff --git a/conformance/conformance_test.cc b/conformance/conformance_test.cc index 2c856f5874..9b8b71f984 100644 --- a/conformance/conformance_test.cc +++ b/conformance/conformance_test.cc @@ -42,7 +42,7 @@ #include #include #include -#include "conformance.pb.h" +#include "conformance/conformance.pb.h" using conformance::ConformanceRequest; using conformance::ConformanceResponse; diff --git a/conformance/conformance_test.h b/conformance/conformance_test.h index 286159837a..298e780f2b 100644 --- a/conformance/conformance_test.h +++ b/conformance/conformance_test.h @@ -45,7 +45,7 @@ #include #include #include -#include "conformance.pb.h" +#include "conformance/conformance.pb.h" namespace conformance { class ConformanceRequest; diff --git a/conformance/conformance_test_runner.cc b/conformance/conformance_test_runner.cc index 2aac35c5cf..6dfb25ba2c 100644 --- a/conformance/conformance_test_runner.cc +++ b/conformance/conformance_test_runner.cc @@ -63,7 +63,7 @@ #include #include -#include "conformance.pb.h" +#include "conformance/conformance.pb.h" #include "conformance_test.h" using conformance::ConformanceResponse; diff --git a/conformance/defs.bzl b/conformance/defs.bzl index e6eceb8a1f..c7357e02bb 100644 --- a/conformance/defs.bzl +++ b/conformance/defs.bzl @@ -39,7 +39,7 @@ def conformance_test( "@bazel_tools//tools/bash/runfiles", ], tags = ["conformance"], - **kwargs, + **kwargs ) def _strip_bazel(testee): diff --git a/generate_descriptor_proto.sh b/generate_descriptor_proto.sh index d1b13d122c..91c1217aba 100755 --- a/generate_descriptor_proto.sh +++ b/generate_descriptor_proto.sh @@ -62,7 +62,7 @@ do PROTOC=$BOOTSTRAP_PROTOC BOOTSTRAP_PROTOC="" else - bazel build -j$(nproc) $@ //:protoc + bazel build $@ //:protoc if test $? -ne 0; then echo "Failed to build protoc." exit 1 diff --git a/java/internal/BUILD.bazel b/java/internal/BUILD.bazel index 598b8b562f..ff111e7aa5 100644 --- a/java/internal/BUILD.bazel +++ b/java/internal/BUILD.bazel @@ -1,7 +1,7 @@ -package(default_visibility = ["//java:__subpackages__"]) - load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") +package(default_visibility = ["//java:__subpackages__"]) + pkg_files( name = "dist_files", srcs = [ @@ -15,8 +15,8 @@ pkg_files( java_test( name = "java_version", - test_class = "JavaVersionTest", srcs = ["JavaVersionTest.java"], + test_class = "JavaVersionTest", deps = [ "@maven//:com_google_truth_truth", "@maven//:junit_junit", diff --git a/java/kotlin/BUILD.bazel b/java/kotlin/BUILD.bazel index 9814c7a0d3..c809117afc 100644 --- a/java/kotlin/BUILD.bazel +++ b/java/kotlin/BUILD.bazel @@ -25,6 +25,7 @@ kt_jvm_library( deps = [ ":only_for_use_in_proto_generated_code_its_generator_and_tests", "//java/lite", + "@maven//:com_google_errorprone_error_prone_annotations", ], ) diff --git a/java/kotlin/pom.xml b/java/kotlin/pom.xml index f2d034e9c8..5fefa8157c 100644 --- a/java/kotlin/pom.xml +++ b/java/kotlin/pom.xml @@ -34,6 +34,11 @@ mockito-core test + + com.google.errorprone + error_prone_annotations + 2.5.1 + com.google.guava guava diff --git a/maven_install.json b/maven_install.json index 1a938afc22..efb3f03c2a 100644 --- a/maven_install.json +++ b/maven_install.json @@ -1,10 +1,10 @@ { "dependency_tree": { "__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL", - "__INPUT_ARTIFACTS_HASH": 579287445, + "__INPUT_ARTIFACTS_HASH": -1875290602, "__RESOLVED_ARTIFACTS_HASH": -1248440885, "conflict_resolution": { - "com.google.errorprone:error_prone_annotations:2.3.2": "com.google.errorprone:error_prone_annotations:2.11.0" + "com.google.errorprone:error_prone_annotations:2.5.1": "com.google.errorprone:error_prone_annotations:2.11.0" }, "dependencies": [ { diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 6f35175a03..f9111ce06c 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -381,9 +381,9 @@ cc_dist_library( "//src/google/protobuf/util:differencer", "//src/google/protobuf/util:field_mask_util", "//src/google/protobuf/util:json_util", - "//src/google/protobuf/util:zero_copy_sink", "//src/google/protobuf/util:time_util", "//src/google/protobuf/util:type_resolver_util", + "//src/google/protobuf/util:zero_copy_sink", "//src/google/protobuf/util/internal:datapiece", "//src/google/protobuf/util/internal:default_value", "//src/google/protobuf/util/internal:field_mask_utility", @@ -427,23 +427,23 @@ cc_dist_library( cc_dist_library( name = "compiler_annotation_test_util", - deps = ["//src/google/protobuf/compiler:annotation_test_util"], testonly = 1, tags = ["manual"], + deps = ["//src/google/protobuf/compiler:annotation_test_util"], ) cc_dist_library( name = "compiler_mock_code_generator", - deps = ["//src/google/protobuf/compiler:mock_code_generator"], testonly = 1, tags = ["manual"], + deps = ["//src/google/protobuf/compiler:mock_code_generator"], ) cc_dist_library( name = "testinglib", - deps = ["//src/google/protobuf/testing"], testonly = 1, tags = ["manual"], + deps = ["//src/google/protobuf/testing"], ) ################################################################################ diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl index d002ef3231..cd60b534d3 100644 --- a/protobuf_deps.bzl +++ b/protobuf_deps.bzl @@ -6,7 +6,7 @@ PROTOBUF_MAVEN_ARTIFACTS = [ "com.google.caliper:caliper:1.0-beta-3", "com.google.code.findbugs:jsr305:3.0.2", "com.google.code.gson:gson:2.8.9", - "com.google.errorprone:error_prone_annotations:2.3.2", + "com.google.errorprone:error_prone_annotations:2.5.1", "com.google.j2objc:j2objc-annotations:1.3", "com.google.guava:guava:31.1-jre", "com.google.guava:guava-testlib:31.1-jre", diff --git a/python/BUILD.bazel b/python/BUILD.bazel index 75d3a5d91d..118bb4d6af 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -7,7 +7,6 @@ # //:well_known_types_py_pb2 load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") -load("@rules_proto//proto:defs.bzl", "proto_library") load("@rules_python//python:defs.bzl", "py_library") load("//:protobuf.bzl", "internal_py_proto_library") load("//build_defs:cpp_opts.bzl", "COPTS") @@ -53,8 +52,8 @@ internal_py_proto_library( internal_copy_files( name = "copied_wkt_proto_files", srcs = [ - "//src/google/protobuf:descriptor_proto_srcs", "//:well_known_type_protos", + "//src/google/protobuf:descriptor_proto_srcs", ], strip_prefix = "src", ) @@ -216,28 +215,28 @@ internal_py_proto_library( internal_py_proto_library( name = "test_messages_proto2_py_proto", testonly = 1, - visibility = [ - "//conformance:__pkg__", - "//python:__subpackages__", - ], srcs = [":copied_test_messages_proto2_files"], include = ".", default_runtime = "//:protobuf_python", protoc = "//:protoc", + visibility = [ + "//conformance:__pkg__", + "//python:__subpackages__", + ], ) internal_py_proto_library( name = "test_messages_proto3_py_proto", testonly = 1, + srcs = [":copied_test_messages_proto3_files"], + include = ".", + default_runtime = "//:protobuf_python", + protoc = "//:protoc", visibility = [ "//conformance:__pkg__", "//python:__subpackages__", ], - srcs = [":copied_test_messages_proto3_files"], deps = [":well_known_types_py_pb2"], - include = ".", - default_runtime = "//:protobuf_python", - protoc = "//:protoc", ) py_library( @@ -400,27 +399,27 @@ py_test( conformance_test( name = "conformance_test", - failure_list = "//conformance:failure_list_python.txt", - testee = "//conformance:conformance_python", - text_format_failure_list = "//conformance:text_format_failure_list_python.txt", env = {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "python"}, + failure_list = "//conformance:failure_list_python.txt", target_compatible_with = select({ ":use_fast_cpp_protos": ["@platforms//:incompatible"], "//conditions:default": [], }), + testee = "//conformance:conformance_python", + text_format_failure_list = "//conformance:text_format_failure_list_python.txt", ) # Note: this requires --define=use_fast_cpp_protos=true conformance_test( name = "conformance_test_cpp", - failure_list = "//conformance:failure_list_python.txt", - testee = "//conformance:conformance_python", - text_format_failure_list = "//conformance:text_format_failure_list_python_cpp.txt", env = {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "cpp"}, + failure_list = "//conformance:failure_list_python.txt", target_compatible_with = select({ ":use_fast_cpp_protos": [], "//conditions:default": ["@platforms//:incompatible"], }), + testee = "//conformance:conformance_python", + text_format_failure_list = "//conformance:text_format_failure_list_python_cpp.txt", ) ################################################################################ diff --git a/python/python_version.py b/python/python_version.py index 5c2b5a6257..5bf8d21a31 100644 --- a/python/python_version.py +++ b/python/python_version.py @@ -27,12 +27,12 @@ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - """Test that Kokoro is using the expected version of python.""" import os -import unittest import sys +import unittest + class PythonVersionTest(unittest.TestCase): @@ -40,11 +40,12 @@ class PythonVersionTest(unittest.TestCase): """Test that we can import nested import public messages.""" exp = os.getenv('KOKORO_PYTHON_VERSION', '') - if exp == '': - print("No kokoro python version found, skipping check", file=sys.stderr) + if not exp: + print('No kokoro python version found, skipping check', file=sys.stderr) return - self.assertTrue(sys.version.startswith(exp), - "Expected Python %s but found Python %s" % (exp, sys.version)) + self.assertTrue( + sys.version.startswith(exp), + 'Expected Python %s but found Python %s' % (exp, sys.version)) if __name__ == '__main__': diff --git a/python/setup.py b/python/setup.py index 3364ba9f7a..b86886f03b 100755 --- a/python/setup.py +++ b/python/setup.py @@ -306,7 +306,8 @@ if __name__ == '__main__': else: library_dirs = ['..'] - TestConformanceCmd.target = '//python:conformance_test_cpp --define=use_fast_cpp_protos=true' + TestConformanceCmd.target = ('//python:conformance_test_cpp ' + '--define=use_fast_cpp_protos=true') extra_compile_args = [] diff --git a/src/google/protobuf/BUILD.bazel b/src/google/protobuf/BUILD.bazel index b5e1a5e44c..ffcf9e6b8e 100644 --- a/src/google/protobuf/BUILD.bazel +++ b/src/google/protobuf/BUILD.bazel @@ -216,8 +216,8 @@ cc_library( "field_mask.pb.cc", "generated_message_bases.cc", "generated_message_reflection.cc", - "generated_message_tctable_gen.cc", "generated_message_tctable_full.cc", + "generated_message_tctable_gen.cc", "map_field.cc", "message.cc", "reflection_ops.cc", @@ -458,13 +458,16 @@ proto_library( ], ) -exports_files([ - "test_messages_proto2.proto", - "test_messages_proto3.proto", -], visibility = [ - "//:__pkg__", - "//python:__pkg__", -]) +exports_files( + [ + "test_messages_proto2.proto", + "test_messages_proto3.proto", + ], + visibility = [ + "//:__pkg__", + "//python:__pkg__", + ], +) proto_library( name = "test_messages_proto2_proto", @@ -480,6 +483,10 @@ proto_library( name = "test_messages_proto3_proto", srcs = ["test_messages_proto3.proto"], strip_import_prefix = "/src", + visibility = [ + "//:__pkg__", + "//conformance:__pkg__", + ], deps = [ ":any_proto", ":duration_proto", @@ -488,10 +495,6 @@ proto_library( ":timestamp_proto", ":wrappers_proto", ], - visibility = [ - "//:__pkg__", - "//conformance:__pkg__", - ], ) cc_proto_library( diff --git a/src/google/protobuf/compiler/BUILD.bazel b/src/google/protobuf/compiler/BUILD.bazel index e3cb41b0c2..4a4c9aab30 100644 --- a/src/google/protobuf/compiler/BUILD.bazel +++ b/src/google/protobuf/compiler/BUILD.bazel @@ -6,7 +6,6 @@ load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") load( "@rules_pkg//:mappings.bzl", "pkg_attributes", - "pkg_filegroup", "pkg_files", "strip_prefix", ) @@ -116,7 +115,6 @@ alias( visibility = ["//visibility:public"], ) - # Test that the protoc binary is built for the correct architecture. aarch64_test( name = "protoc_aarch64_test", diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index eb969f5f72..b2cf3d5d10 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -359,7 +359,8 @@ void CommandLineInterfaceTest::RunWithArgs(std::vector args) { if (plugin_path.empty() || !FileExists(plugin_path)) { GOOGLE_LOG(ERROR) - << "Plugin tests are likely to fail. Plugin executable not found at: " << plugin_path; + << "Plugin executable not found. Plugin tests are likely to fail." + << plugin_path; } else { args.push_back("--plugin=prefix-gen-plug=" + plugin_path); } diff --git a/src/google/protobuf/compiler/java/context.cc b/src/google/protobuf/compiler/java/context.cc index cdc0d4475f..3272efbcb8 100644 --- a/src/google/protobuf/compiler/java/context.cc +++ b/src/google/protobuf/compiler/java/context.cc @@ -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); } diff --git a/src/google/protobuf/compiler/java/doc_comment.h b/src/google/protobuf/compiler/java/doc_comment.h index 03672bf52d..acfdfcd643 100644 --- a/src/google/protobuf/compiler/java/doc_comment.h +++ b/src/google/protobuf/compiler/java/doc_comment.h @@ -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 diff --git a/src/google/protobuf/compiler/java/enum.cc b/src/google/protobuf/compiler/java/enum.cc index 0cab93c391..ae3cb117a9 100644 --- a/src/google/protobuf/compiler/java/enum.cc +++ b/src/google/protobuf/compiler/java/enum.cc @@ -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()); diff --git a/src/google/protobuf/compiler/java/enum_field.cc b/src/google/protobuf/compiler/java/enum_field.cc index aa3a51f65e..6518d003ee 100644 --- a/src/google/protobuf/compiler/java/enum_field.cc +++ b/src/google/protobuf/compiler/java/enum_field.cc @@ -60,7 +60,8 @@ namespace { void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* 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() {} diff --git a/src/google/protobuf/compiler/java/enum_field_lite.cc b/src/google/protobuf/compiler/java/enum_field_lite.cc index a256824714..2f94bedc53 100644 --- a/src/google/protobuf/compiler/java/enum_field_lite.cc +++ b/src/google/protobuf/compiler/java/enum_field_lite.cc @@ -68,7 +68,8 @@ bool EnableExperimentalRuntimeForLite() { void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* 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" diff --git a/src/google/protobuf/compiler/java/enum_lite.cc b/src/google/protobuf/compiler/java/enum_lite.cc index 4387090a05..692f38c5f4 100644 --- a/src/google/protobuf/compiler/java/enum_lite.cc +++ b/src/google/protobuf/compiler/java/enum_lite.cc @@ -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" + " *

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" diff --git a/src/google/protobuf/compiler/java/extension.cc b/src/google/protobuf/compiler/java/extension.cc index 8b93eb18df..41aca0b43b 100644 --- a/src/google/protobuf/compiler/java/extension.cc +++ b/src/google/protobuf/compiler/java/extension.cc @@ -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* vars_pointer) { + std::map* vars_pointer, Context* context) { std::map& 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 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_); diff --git a/src/google/protobuf/compiler/java/extension.h b/src/google/protobuf/compiler/java/extension.h index 318cfa4cc9..e09ba8f515 100644 --- a/src/google/protobuf/compiler/java/extension.h +++ b/src/google/protobuf/compiler/java/extension.h @@ -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* vars_pointer); + static void InitTemplateVars(const FieldDescriptor* descriptor, + const std::string& scope, bool immutable, + ClassNameResolver* name_resolver, + std::map* 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); diff --git a/src/google/protobuf/compiler/java/extension_lite.cc b/src/google/protobuf/compiler/java/extension_lite.cc index bffb1d6507..2914c2776c 100644 --- a/src/google/protobuf/compiler/java/extension_lite.cc +++ b/src/google/protobuf/compiler/java/extension_lite.cc @@ -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 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_); diff --git a/src/google/protobuf/compiler/java/extension_lite.h b/src/google/protobuf/compiler/java/extension_lite.h index 264230cb08..c22af1bdd3 100644 --- a/src/google/protobuf/compiler/java/extension_lite.h +++ b/src/google/protobuf/compiler/java/extension_lite.h @@ -63,6 +63,7 @@ class ImmutableExtensionLiteGenerator : public ExtensionGenerator { const FieldDescriptor* descriptor_; ClassNameResolver* name_resolver_; std::string scope_; + Context* context_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableExtensionLiteGenerator); }; diff --git a/src/google/protobuf/compiler/java/file.cc b/src/google/protobuf/compiler/java/file.cc index c4665182d4..8ca5e37745 100644 --- a/src/google/protobuf/compiler/java/file.cc +++ b/src/google/protobuf/compiler/java/file.cc @@ -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"; } diff --git a/src/google/protobuf/compiler/java/generator.cc b/src/google/protobuf/compiler/java/generator.cc index 85e3991357..84d8307020 100644 --- a/src/google/protobuf/compiler/java/generator.cc +++ b/src/google/protobuf/compiler/java/generator.cc @@ -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; diff --git a/src/google/protobuf/compiler/java/generator.h b/src/google/protobuf/compiler/java/generator.h index 39d2445e7d..522a997cc9 100644 --- a/src/google/protobuf/compiler/java/generator.h +++ b/src/google/protobuf/compiler/java/generator.h @@ -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); }; diff --git a/src/google/protobuf/compiler/java/helpers.cc b/src/google/protobuf/compiler/java/helpers.cc index 4593991f30..849f922c48 100644 --- a/src/google/protobuf/compiler/java/helpers.cc +++ b/src/google/protobuf/compiler/java/helpers.cc @@ -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"; diff --git a/src/google/protobuf/compiler/java/helpers.h b/src/google/protobuf/compiler/java/helpers.h index f4554324ed..0b6b260b98 100644 --- a/src/google/protobuf/compiler/java/helpers.h +++ b/src/google/protobuf/compiler/java/helpers.h @@ -41,8 +41,12 @@ #include #include #include +#include #include +// Must be last. +#include + 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 GetTableDrivenNumberOfEntriesAndLookUpStartFieldNumber( } // namespace protobuf } // namespace google +#include #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__ diff --git a/src/google/protobuf/compiler/java/map_field.cc b/src/google/protobuf/compiler/java/map_field.cc index c430b69939..23bcfce95b 100644 --- a/src/google/protobuf/compiler/java/map_field.cc +++ b/src/google/protobuf/compiler/java/map_field.cc @@ -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" diff --git a/src/google/protobuf/compiler/java/map_field.h b/src/google/protobuf/compiler/java/map_field.h index 7fee10a07e..7d39c9f1bb 100644 --- a/src/google/protobuf/compiler/java/map_field.h +++ b/src/google/protobuf/compiler/java/map_field.h @@ -71,6 +71,7 @@ class ImmutableMapFieldGenerator : public ImmutableFieldGenerator { const FieldDescriptor* descriptor_; std::map variables_; ClassNameResolver* name_resolver_; + Context* context_; void GenerateMapGetters(io::Printer* printer) const; }; diff --git a/src/google/protobuf/compiler/java/map_field_lite.cc b/src/google/protobuf/compiler/java/map_field_lite.cc index 7657829c86..29736ad641 100644 --- a/src/google/protobuf/compiler/java/map_field_lite.cc +++ b/src/google/protobuf/compiler/java/map_field_lite.cc @@ -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" diff --git a/src/google/protobuf/compiler/java/message.cc b/src/google/protobuf/compiler/java/message.cc index c1d8238a66..943c7b32cb 100644 --- a/src/google/protobuf/compiler/java/message.cc +++ b/src/google/protobuf/compiler/java/message.cc @@ -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 unpack(\n" " java.lang.Class 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" diff --git a/src/google/protobuf/compiler/java/message_builder.cc b/src/google/protobuf/compiler/java/message_builder.cc index 2366c0c8d5..b126daa36c 100644 --- a/src/google/protobuf/compiler/java/message_builder.cc +++ b/src/google/protobuf/compiler/java/message_builder.cc @@ -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 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 Builder setExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, java.util.List> 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 Builder addExtension(\n" - " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" - " $classname$, java.util.List> 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 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 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 Builder setExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, java.util.List> extension,\n" + " int index, Type value) {\n" + " return super.setExtension(extension, index, value);\n" + "}\n" + "@java.lang.Override\n" + "public Builder addExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, java.util.List> extension,\n" + " Type value) {\n" + " return super.addExtension(extension, value);\n" + "}\n" + "@java.lang.Override\n" + "public Builder clearExtension(\n" + " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" + " $classname$, ?> extension) {\n" + " return super.clearExtension(extension);\n" + "}\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); + } } // ----------------------------------------------------------------- diff --git a/src/google/protobuf/compiler/java/message_field.cc b/src/google/protobuf/compiler/java/message_field.cc index d1cf12928f..3bd651df8f 100644 --- a/src/google/protobuf/compiler/java/message_field.cc +++ b/src/google/protobuf/compiler/java/message_field.cc @@ -59,7 +59,8 @@ namespace { void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* 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:: diff --git a/src/google/protobuf/compiler/java/message_field.h b/src/google/protobuf/compiler/java/message_field.h index 32dd4f0f8c..f717e3dfad 100644 --- a/src/google/protobuf/compiler/java/message_field.h +++ b/src/google/protobuf/compiler/java/message_field.h @@ -91,6 +91,7 @@ class ImmutableMessageFieldGenerator : public ImmutableFieldGenerator { const FieldDescriptor* descriptor_; std::map variables_; ClassNameResolver* name_resolver_; + Context* context_; void PrintNestedBuilderCondition(io::Printer* printer, const char* regular_case, diff --git a/src/google/protobuf/compiler/java/message_field_lite.cc b/src/google/protobuf/compiler/java/message_field_lite.cc index bf082fbf1d..03e658fb3f 100644 --- a/src/google/protobuf/compiler/java/message_field_lite.cc +++ b/src/google/protobuf/compiler/java/message_field_lite.cc @@ -59,7 +59,8 @@ namespace { void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* 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"); diff --git a/src/google/protobuf/compiler/java/message_field_lite.h b/src/google/protobuf/compiler/java/message_field_lite.h index 4253acc8b0..24ee91f999 100644 --- a/src/google/protobuf/compiler/java/message_field_lite.h +++ b/src/google/protobuf/compiler/java/message_field_lite.h @@ -82,6 +82,7 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator { std::map 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 variables_; ClassNameResolver* name_resolver_; + Context* context_; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutableMessageFieldLiteGenerator); diff --git a/src/google/protobuf/compiler/java/message_lite.cc b/src/google/protobuf/compiler/java/message_lite.cc index 6091c175fe..cee165a6bf 100644 --- a/src/google/protobuf/compiler/java/message_lite.cc +++ b/src/google/protobuf/compiler/java/message_lite.cc @@ -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" diff --git a/src/google/protobuf/compiler/java/name_resolver.cc b/src/google/protobuf/compiler/java/name_resolver.cc index 06a637e3c2..b2bb74e23f 100644 --- a/src/google/protobuf/compiler/java/name_resolver.cc +++ b/src/google/protobuf/compiler/java/name_resolver.cc @@ -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); } diff --git a/src/google/protobuf/compiler/java/name_resolver.h b/src/google/protobuf/compiler/java/name_resolver.h index 103cace43e..6d2ec0143d 100644 --- a/src/google/protobuf/compiler/java/name_resolver.h +++ b/src/google/protobuf/compiler/java/name_resolver.h @@ -34,7 +34,9 @@ #include #include +#include #include +#include // Must be last. #include @@ -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 file_immutable_outer_class_names_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ClassNameResolver); }; } // namespace java diff --git a/src/google/protobuf/compiler/java/names.h b/src/google/protobuf/compiler/java/names.h index 313ace4feb..bf1465b90f 100644 --- a/src/google/protobuf/compiler/java/names.h +++ b/src/google/protobuf/compiler/java/names.h @@ -40,6 +40,8 @@ #include +#include + 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 diff --git a/src/google/protobuf/compiler/java/options.h b/src/google/protobuf/compiler/java/options.h index 6c29be1500..6dfb2a85dc 100644 --- a/src/google/protobuf/compiler/java/options.h +++ b/src/google/protobuf/compiler/java/options.h @@ -33,6 +33,8 @@ #include +#include + 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 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_OPTIONS_H__ diff --git a/src/google/protobuf/compiler/java/primitive_field.cc b/src/google/protobuf/compiler/java/primitive_field.cc index 69de34bc10..a60705ce2f 100644 --- a/src/google/protobuf/compiler/java/primitive_field.cc +++ b/src/google/protobuf/compiler/java/primitive_field.cc @@ -61,7 +61,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* 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(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:: diff --git a/src/google/protobuf/compiler/java/primitive_field_lite.cc b/src/google/protobuf/compiler/java/primitive_field_lite.cc index daf6566f62..869b304c85 100644 --- a/src/google/protobuf/compiler/java/primitive_field_lite.cc +++ b/src/google/protobuf/compiler/java/primitive_field_lite.cc @@ -69,16 +69,18 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* 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(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:: diff --git a/src/google/protobuf/compiler/java/primitive_field_lite.h b/src/google/protobuf/compiler/java/primitive_field_lite.h index 2da0cd8f07..392f1c4693 100644 --- a/src/google/protobuf/compiler/java/primitive_field_lite.h +++ b/src/google/protobuf/compiler/java/primitive_field_lite.h @@ -81,6 +81,7 @@ class ImmutablePrimitiveFieldLiteGenerator const FieldDescriptor* descriptor_; std::map variables_; const int messageBitIndex_; + Context* context_; ClassNameResolver* name_resolver_; private: diff --git a/src/google/protobuf/compiler/java/service.cc b/src/google/protobuf/compiler/java/service.cc index 9e20620823..bf98277aec 100644 --- a/src/google/protobuf/compiler/java/service.cc +++ b/src/google/protobuf/compiler/java/service.cc @@ -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", diff --git a/src/google/protobuf/compiler/java/shared_code_generator.cc b/src/google/protobuf/compiler/java/shared_code_generator.cc index 39b96eec60..477b35ee62 100644 --- a/src/google/protobuf/compiler/java/shared_code_generator.cc +++ b/src/google/protobuf/compiler/java/shared_code_generator.cc @@ -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* file_list, std::vector* 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 > 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"); diff --git a/src/google/protobuf/compiler/java/string_field.cc b/src/google/protobuf/compiler/java/string_field.cc index d3162a4dc7..90593306fb 100644 --- a/src/google/protobuf/compiler/java/string_field.cc +++ b/src/google/protobuf/compiler/java/string_field.cc @@ -63,14 +63,17 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* 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(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:: diff --git a/src/google/protobuf/compiler/java/string_field_lite.cc b/src/google/protobuf/compiler/java/string_field_lite.cc index b8861977b1..a7ba88f7f6 100644 --- a/src/google/protobuf/compiler/java/string_field_lite.cc +++ b/src/google/protobuf/compiler/java/string_field_lite.cc @@ -63,15 +63,18 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex, const FieldGeneratorInfo* info, ClassNameResolver* name_resolver, - std::map* variables) { + std::map* 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(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 " diff --git a/src/google/protobuf/compiler/java/string_field_lite.h b/src/google/protobuf/compiler/java/string_field_lite.h index b6ad1ea83c..2f9b4931e4 100644 --- a/src/google/protobuf/compiler/java/string_field_lite.h +++ b/src/google/protobuf/compiler/java/string_field_lite.h @@ -83,6 +83,7 @@ class ImmutableStringFieldLiteGenerator : public ImmutableFieldLiteGenerator { std::map 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 variables_; ClassNameResolver* name_resolver_; + Context* context_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutableStringFieldLiteGenerator); }; diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 22843652e8..268aa74cde 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -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. diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc index d0ab7a8af7..b52ee72e91 100644 --- a/src/google/protobuf/port_undef.inc +++ b/src/google/protobuf/port_undef.inc @@ -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 diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc index 3fa3d04c75..feff7a1ac0 100644 --- a/src/google/protobuf/stubs/common.cc +++ b/src/google/protobuf/stubs/common.cc @@ -118,10 +118,9 @@ std::string ProtocVersionString(int version) { snprintf(buffer, sizeof(buffer), "%d.%d", minor, micro); // Guard against broken MSVC snprintf(). - buffer[sizeof(buffer)-1] = '\0'; + buffer[sizeof(buffer) - 1] = '\0'; return buffer; - } } // namespace internal diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h index 64787684bb..892c66feb8 100644 --- a/src/google/protobuf/stubs/common.h +++ b/src/google/protobuf/stubs/common.h @@ -106,10 +106,12 @@ void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion, const char* filename); // Converts a numeric version number to a string. -std::string PROTOBUF_EXPORT VersionString(int version); +std::string PROTOBUF_EXPORT +VersionString(int version); // NOLINT(runtime/string) // Prints the protoc compiler version (no major version) -std::string PROTOBUF_EXPORT ProtocVersionString(int version); +std::string PROTOBUF_EXPORT +ProtocVersionString(int version); // NOLINT(runtime/string) } // namespace internal diff --git a/src/google/protobuf/wire_format_lite.h b/src/google/protobuf/wire_format_lite.h index 8f38f7c8d0..f3097702bf 100644 --- a/src/google/protobuf/wire_format_lite.h +++ b/src/google/protobuf/wire_format_lite.h @@ -101,7 +101,11 @@ class PROTOBUF_EXPORT WireFormatLite { // identifies the encoding of this data, it is possible to skip // unrecognized fields for forwards compatibility. - enum WireType { + enum WireType +#ifndef SWIG + : int +#endif // !SWIG + { WIRETYPE_VARINT = 0, WIRETYPE_FIXED64 = 1, WIRETYPE_LENGTH_DELIMITED = 2,