diff --git a/src/google/protobuf/compiler/java/field_common.cc b/src/google/protobuf/compiler/java/field_common.cc index cf5cdcbf0a..d8d7255e7b 100644 --- a/src/google/protobuf/compiler/java/field_common.cc +++ b/src/google/protobuf/compiler/java/field_common.cc @@ -13,8 +13,6 @@ namespace protobuf { namespace compiler { namespace java { -std::string GetKotlinPropertyName(std::string capitalized_name); - void SetCommonFieldVariables( const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, absl::flat_hash_map* variables) { @@ -68,25 +66,6 @@ static bool IsUpper(char c) { static char ToLower(char c) { return IsUpper(c) ? c - 'A' + 'a' : c; } -// Returns the name by which the generated Java getters and setters should be -// referenced from Kotlin as properties. In the simplest case, the original name -// is something like `foo_bar`, which gets translated into `getFooBar()` etc, -// and that in turn can be referenced from Kotlin as `fooBar`. -// -// The algorithm for translating proto names into Java getters and setters is -// straightforward. The first letter of each underscore-separated word gets -// uppercased and the underscores are deleted. There are no other changes, so in -// particular if the proto name has a string of capitals then those remain -// as-is. -// -// The algorithm that the Kotlin compiler uses to derive the property name is -// slightly more complicated. If the first character after `get` (etc) is a -// capital and the second isn't, then the property name is just that string with -// its first letter lowercased. So `getFoo` becomes `foo` and `getX` becomes -// `x`. But if there is more than one capital, then all but the last get -// lowercased. So `getHTMLPage` becomes `htmlPage`. If there are only capitals -// then they all get lowercased, so `getID` becomes `id`. -// TODO: move this to a Kotlin-specific location std::string GetKotlinPropertyName(std::string capitalized_name) { // Find the first non-capital. If it is the second character, then we just // need to lowercase the first one. Otherwise we need to lowercase everything diff --git a/src/google/protobuf/compiler/java/field_common.h b/src/google/protobuf/compiler/java/field_common.h index 54e58a0ecb..c29d1c25e9 100644 --- a/src/google/protobuf/compiler/java/field_common.h +++ b/src/google/protobuf/compiler/java/field_common.h @@ -1,6 +1,8 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ +#include + #include "google/protobuf/descriptor.h" namespace google { @@ -36,6 +38,27 @@ void PrintExtraFieldInfo( const absl::flat_hash_map& variables, io::Printer* printer); +// Returns the name by which the generated Java getters and setters should be +// referenced from Kotlin as properties. In the simplest case, the original name +// is something like `foo_bar`, which gets translated into `getFooBar()` etc, +// and that in turn can be referenced from Kotlin as `fooBar`. +// +// The algorithm for translating proto names into Java getters and setters is +// straightforward. The first letter of each underscore-separated word gets +// uppercased and the underscores are deleted. There are no other changes, so in +// particular if the proto name has a string of capitals then those remain +// as-is. +// +// The algorithm that the Kotlin compiler uses to derive the property name is +// slightly more complicated. If the first character after `get` (etc) is a +// capital and the second isn't, then the property name is just that string with +// its first letter lowercased. So `getFoo` becomes `foo` and `getX` becomes +// `x`. But if there is more than one capital, then all but the last get +// lowercased. So `getHTMLPage` becomes `htmlPage`. If there are only capitals +// then they all get lowercased, so `getID` becomes `id`. +// TODO: move this to a Kotlin-specific location +std::string GetKotlinPropertyName(std::string capitalized_name); + } // namespace java } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/java/lite/message.cc b/src/google/protobuf/compiler/java/lite/message.cc index 81e95242aa..1d240672d8 100644 --- a/src/google/protobuf/compiler/java/lite/message.cc +++ b/src/google/protobuf/compiler/java/lite/message.cc @@ -773,16 +773,17 @@ void ImmutableMessageLiteGenerator::GenerateKotlinDsl( for (auto& kv : oneofs_) { const OneofDescriptor* oneof = kv.second; + auto oneof_name = context_->GetOneofGeneratorInfo(oneof)->name; printer->Print( "public val $oneof_name$Case: $message$.$oneof_capitalized_name$Case\n" " @JvmName(\"get$oneof_capitalized_name$Case\")\n" - " get() = _builder.get$oneof_capitalized_name$Case()\n\n" + " get() = _builder.$oneof_property_name$Case\n\n" "public fun clear$oneof_capitalized_name$() {\n" " _builder.clear$oneof_capitalized_name$()\n" "}\n", - "oneof_name", context_->GetOneofGeneratorInfo(oneof)->name, - "oneof_capitalized_name", - context_->GetOneofGeneratorInfo(oneof)->capitalized_name, "message", + "oneof_name", oneof_name, "oneof_capitalized_name", + context_->GetOneofGeneratorInfo(oneof)->capitalized_name, + "oneof_property_name", GetKotlinPropertyName(oneof_name), "message", EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true))); }