From e760984a59b64ad4181424ec1f7722ce04ac49f8 Mon Sep 17 00:00:00 2001 From: Deanna Garcia Date: Fri, 11 Feb 2022 19:13:07 +0000 Subject: [PATCH] First try --- BUILD | 16 ++++++++++++++ java/kotlin/BUILD | 15 ++++++++++--- protobuf_deps.bzl | 6 ++--- .../protobuf/compiler/java/java_message.cc | 22 +++++++++---------- .../compiler/java/java_message_field.cc | 12 +++++----- .../compiler/java/java_message_field_lite.cc | 12 +++++----- .../compiler/java/java_message_lite.cc | 16 +++++++------- 7 files changed, 62 insertions(+), 37 deletions(-) diff --git a/BUILD b/BUILD index 8a84954c64..e4393d9d52 100644 --- a/BUILD +++ b/BUILD @@ -1485,6 +1485,7 @@ genrule( outs = [ "TestAllTypesKt.kt", "ForeignMessageKt.kt", + "Int32MessageKt.kt", "TestAllExtensionsKt.kt", "TestEmptyMessageKt.kt", "TestEmptyMessageWithExtensionsKt.kt", @@ -1505,6 +1506,8 @@ genrule( "$(location TestAllTypesKt.kt) && " + "cp $(@D)/protobuf_unittest/ForeignMessageKt.kt " + "$(location ForeignMessageKt.kt) && " + + "cp $(@D)/protobuf_unittest/Int32MessageKt.kt " + + "$(location Int32MessageKt.kt) && " + "cp $(@D)/protobuf_unittest/TestAllExtensionsKt.kt " + "$(location TestAllExtensionsKt.kt) && " + "cp $(@D)/protobuf_unittest/TestEmptyMessageKt.kt " + @@ -1568,3 +1571,16 @@ genrule( tools = ["//:protoc"], ) +genrule( + name = "gen_kotlin_any", + srcs = ["src/google/protobuf/any.proto"], + outs = ["AnyKt.kt"], + visibility = ["//java:__subpackages__"], + cmd = "$(location //:protoc) " + + "--kotlin_out=shared,immutable:$(@D) -Isrc/ " + + "$(location src/google/protobuf/any.proto) && " + + "cp $(@D)/com/google/protobuf/AnyKt.kt " + + "$(location AnyKt.kt)", + tools = ["//:protoc"], +) + diff --git a/java/kotlin/BUILD b/java/kotlin/BUILD index 5403e6f201..df6b0f50cc 100644 --- a/java/kotlin/BUILD +++ b/java/kotlin/BUILD @@ -1,5 +1,6 @@ load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") load("@rules_java//java:defs.bzl", "java_proto_library") +load("@rules_jvm_external//:kt_defs.bzl", "kt_jvm_export") load("@rules_proto//proto:defs.bzl", "proto_library") load("//:protobuf_version.bzl", "PROTOBUF_VERSION") @@ -35,7 +36,10 @@ kt_jvm_library( kt_jvm_library( name = "full_extensions", - srcs = ["src/main/kotlin/com/google/protobuf/ExtendableMessageExtensions.kt"], + srcs = [ + "src/main/kotlin/com/google/protobuf/Anies.kt", + "src/main/kotlin/com/google/protobuf/ExtendableMessageExtensions.kt", + ], deps = ["//java/core"], ) @@ -106,11 +110,15 @@ java_test( kt_jvm_library( name = "test_extensions_library", - srcs = ["src/test/kotlin/com/google/protobuf/ExtendableMessageExtensionsTest.kt"], + srcs = [ + "src/test/kotlin/com/google/protobuf/AniesTest.kt", + "src/test/kotlin/com/google/protobuf/ExtendableMessageExtensionsTest.kt", + ], deps = [ ":example_extensible_message_java_proto", ":full_extensions", - "//java/lite", + "//java/core:core", + ":kotlin_unittest", ":only_for_use_in_proto_generated_code_its_generator_and_tests", ":shared_runtime", "@com_github_jetbrains_kotlin//:kotlin-test", @@ -221,6 +229,7 @@ kt_jvm_library( name = "kotlin_unittest", srcs = [ ":gen_evil_names_proto2", + "//:gen_kotlin_any", "//:gen_kotlin_unittest", ], deps = [ diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl index 7e2caa3302..27b8bee258 100644 --- a/protobuf_deps.bzl +++ b/protobuf_deps.bzl @@ -70,9 +70,9 @@ def protobuf_deps(): if not native.existing_rule("rules_jvm_external"): http_archive( name = "rules_jvm_external", - sha256 = "f36441aa876c4f6427bfb2d1f2d723b48e9d930b62662bf723ddfb8fc80f0140", - strip_prefix = "rules_jvm_external-4.1", - urls = ["https://github.com/bazelbuild/rules_jvm_external/archive/4.1.zip"], + sha256 = "744bd7436f63af7e9872948773b8b106016dc164acb3960b4963f86754532ee7", + strip_prefix = "rules_jvm_external-906875b0d5eaaf61a8ca2c9c3835bde6f435d011", + urls = ["https://github.com/bazelbuild/rules_jvm_external/archive/906875b0d5eaaf61a8ca2c9c3835bde6f435d011.zip"], ) if not native.existing_rule("rules_pkg"): diff --git a/src/google/protobuf/compiler/java/java_message.cc b/src/google/protobuf/compiler/java/java_message.cc index 7fbad3f842..6c45f98b53 100644 --- a/src/google/protobuf/compiler/java/java_message.cc +++ b/src/google/protobuf/compiler/java/java_message.cc @@ -1526,7 +1526,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( printer->Print( "@Suppress(\"UNCHECKED_CAST\")\n" "@kotlin.jvm.JvmSynthetic\n" - "public operator fun get(extension: " + "public operator fun get(extension: " "com.google.protobuf.ExtensionLite<$message$, T>): T {\n" " return if (extension.isRepeated) {\n" " get(extension as com.google.protobuf.ExtensionLite<$message$, " @@ -1542,7 +1542,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( "@kotlin.OptIn" "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" "@kotlin.jvm.JvmName(\"-getRepeatedExtension\")\n" - "public operator fun get(\n" + "public operator fun get(\n" " extension: com.google.protobuf.ExtensionLite<$message$, List>\n" "): com.google.protobuf.kotlin.ExtensionList {\n" " return com.google.protobuf.kotlin.ExtensionList(extension, " @@ -1571,7 +1571,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" "@kotlin.PublishedApi\n" - "internal fun setExtension(extension: " + "internal fun setExtension(extension: " "com.google.protobuf.ExtensionLite<$message$, T>, " "value: T) {\n" " _builder.setExtension(extension, value)\n" @@ -1585,7 +1585,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( " extension: com.google.protobuf.ExtensionLite<$message$, T>,\n" " value: T\n" ") {\n" - " setExtension(extension, value)\n" + " setExtension(extension, value!!)\n" "}\n\n", "message", message_name); @@ -1597,7 +1597,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( "com.google.protobuf.ByteString>,\n" " value: com.google.protobuf.ByteString\n" ") {\n" - " setExtension(extension, value)\n" + " setExtension(extension, value!!)\n" "}\n\n", "message", message_name); @@ -1608,13 +1608,13 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( " extension: com.google.protobuf.ExtensionLite<$message$, T>,\n" " value: T\n" ") {\n" - " setExtension(extension, value)\n" + " setExtension(extension, value!!)\n" "}\n\n", "message", message_name); printer->Print( "@kotlin.jvm.JvmSynthetic\n" - "public fun com.google.protobuf.kotlin.ExtensionList com.google.protobuf.kotlin.ExtensionList.add(value: E) {\n" " _builder.addExtension(this.extension, value)\n" "}\n\n", @@ -1623,7 +1623,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun " + "public inline operator fun " "com.google.protobuf.kotlin.ExtensionList.plusAssign" "(value: E) {\n" @@ -1633,7 +1633,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" - "public fun com.google.protobuf.kotlin.ExtensionList com.google.protobuf.kotlin.ExtensionList.addAll(values: Iterable) {\n" " for (value in values) {\n" " add(value)\n" @@ -1644,7 +1644,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun " + "public inline operator fun " "com.google.protobuf.kotlin.ExtensionList.plusAssign(values: " "Iterable) {\n" @@ -1654,7 +1654,7 @@ void ImmutableMessageGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" - "public operator fun " + "public operator fun " "com.google.protobuf.kotlin.ExtensionList.set(index: Int, value: " "E) {\n" diff --git a/src/google/protobuf/compiler/java/java_message_field.cc b/src/google/protobuf/compiler/java/java_message_field.cc index 1705c32d40..694218c1d4 100644 --- a/src/google/protobuf/compiler/java/java_message_field.cc +++ b/src/google/protobuf/compiler/java/java_message_field.cc @@ -1438,7 +1438,7 @@ void RepeatedImmutableMessageFieldGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "add(value: $kt_type$) {\n" " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, /* builder */ false); @@ -1450,7 +1450,7 @@ void RepeatedImmutableMessageFieldGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "plusAssign(value: $kt_type$) {\n" " add(value)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, /* builder */ false); @@ -1461,7 +1461,7 @@ void RepeatedImmutableMessageFieldGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, /* builder */ false); @@ -1474,7 +1474,7 @@ void RepeatedImmutableMessageFieldGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" " addAll(values)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, /* builder */ false); @@ -1486,7 +1486,7 @@ void RepeatedImmutableMessageFieldGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "set(index: kotlin.Int, value: $kt_type$) {\n" " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, /* builder */ false); @@ -1497,7 +1497,7 @@ void RepeatedImmutableMessageFieldGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "clear() {\n" " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}"); + "}\n\n"); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_message_field_lite.cc b/src/google/protobuf/compiler/java/java_message_field_lite.cc index 658d105c72..4f4265fdf8 100644 --- a/src/google/protobuf/compiler/java/java_message_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_field_lite.cc @@ -825,7 +825,7 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "add(value: $kt_type$) {\n" " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, /* builder */ false); @@ -837,7 +837,7 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "plusAssign(value: $kt_type$) {\n" " add(value)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, /* builder */ false); @@ -848,7 +848,7 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, /* builder */ false); @@ -861,7 +861,7 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" " addAll(values)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, /* builder */ false); @@ -873,7 +873,7 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "set(index: kotlin.Int, value: $kt_type$) {\n" " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}"); + "}\n"); WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, /* builder */ false); @@ -884,7 +884,7 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers( "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." "clear() {\n" " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}"); + "}\n"); } } // namespace java diff --git a/src/google/protobuf/compiler/java/java_message_lite.cc b/src/google/protobuf/compiler/java/java_message_lite.cc index 5be4b6b3ee..dd32c261ec 100644 --- a/src/google/protobuf/compiler/java/java_message_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_lite.cc @@ -847,7 +847,7 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions( printer->Print( "@Suppress(\"UNCHECKED_CAST\")\n" "@kotlin.jvm.JvmSynthetic\n" - "public operator fun get(extension: " + "public operator fun get(extension: " "com.google.protobuf.ExtensionLite<$message$, T>): T {\n" " return if (extension.isRepeated) {\n" " get(extension as com.google.protobuf.ExtensionLite<$message$, " @@ -863,7 +863,7 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions( "@kotlin.OptIn" "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" "@kotlin.jvm.JvmName(\"-getRepeatedExtension\")\n" - "public operator fun get(\n" + "public operator fun get(\n" " extension: com.google.protobuf.ExtensionLite<$message$, List>\n" "): com.google.protobuf.kotlin.ExtensionList {\n" " return com.google.protobuf.kotlin.ExtensionList(extension, " @@ -892,7 +892,7 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" "@kotlin.PublishedApi\n" - "internal fun setExtension(extension: " + "internal fun setExtension(extension: " "com.google.protobuf.ExtensionLite<$message$, T>, " "value: T) {\n" " _builder.setExtension(extension, value)\n" @@ -935,7 +935,7 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" - "public fun com.google.protobuf.kotlin.ExtensionList com.google.protobuf.kotlin.ExtensionList.add(value: E) {\n" " _builder.addExtension(this.extension, value)\n" "}\n\n", @@ -944,7 +944,7 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun " + "public inline operator fun " "com.google.protobuf.kotlin.ExtensionList.plusAssign" "(value: E) {\n" @@ -954,7 +954,7 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" - "public fun com.google.protobuf.kotlin.ExtensionList com.google.protobuf.kotlin.ExtensionList.addAll(values: Iterable) {\n" " for (value in values) {\n" " add(value)\n" @@ -965,7 +965,7 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun " + "public inline operator fun " "com.google.protobuf.kotlin.ExtensionList.plusAssign(values: " "Iterable) {\n" @@ -975,7 +975,7 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions( printer->Print( "@kotlin.jvm.JvmSynthetic\n" - "public operator fun " + "public operator fun " "com.google.protobuf.kotlin.ExtensionList.set(index: Int, value: " "E) {\n"