Skip to content

Commit 9c7453d

Browse files
shivaspeaksAgraVator
authored andcommitted
compiler: Mangle generated method names that conflict with java.lang.Object methods (#12332)
Generated gRPC method names in the BlockingV2Stub can conflict with final methods on `java.lang.Object` (e.g., `toString()`, `hashCode()`) for client-streaming and bidi-streaming RPCs. This occurs because they are generated with no arguments, leading to a compilation error. One such case in #12331. This change introduces a dedicated list of no-argument method names from java.lang.Object and applies name-mangling (appending an underscore) only when generating these specific methods in the v2 blocking stub. This resolves the compilation failure while ensuring that the behavior for all other stubs remains unchanged. Fixes: #12331
1 parent a8200d0 commit 9c7453d

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

compiler/src/java_plugin/cpp/java_generator.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,24 @@ static std::set<std::string> java_keywords = {
143143
"false",
144144
};
145145

146+
// Methods on java.lang.Object that take no arguments.
147+
static std::set<std::string> java_object_methods = {
148+
"clone",
149+
"finalize",
150+
"getClass",
151+
"hashCode",
152+
"notify",
153+
"notifyAll",
154+
"toString",
155+
"wait",
156+
};
157+
146158
// Adjust a method name prefix identifier to follow the JavaBean spec:
147159
// - decapitalize the first letter
148160
// - remove embedded underscores & capitalize the following letter
149-
// Finally, if the result is a reserved java keyword, append an underscore.
150-
static std::string MixedLower(std::string word) {
161+
// Finally, if the result is a reserved java keyword or an Object method,
162+
// append an underscore.
163+
static std::string MixedLower(std::string word, bool mangle_object_methods = false) {
151164
std::string w;
152165
w += tolower(word[0]);
153166
bool after_underscore = false;
@@ -159,7 +172,9 @@ static std::string MixedLower(std::string word) {
159172
after_underscore = false;
160173
}
161174
}
162-
if (java_keywords.find(w) != java_keywords.end()) {
175+
if (java_keywords.find(w) != java_keywords.end() ||
176+
(mangle_object_methods &&
177+
java_object_methods.find(w) != java_object_methods.end())) {
163178
return w + "_";
164179
}
165180
return w;
@@ -180,8 +195,9 @@ static std::string ToAllUpperCase(std::string word) {
180195
return w;
181196
}
182197

183-
static inline std::string LowerMethodName(const MethodDescriptor* method) {
184-
return MixedLower(std::string(method->name()));
198+
static inline std::string LowerMethodName(const MethodDescriptor* method,
199+
bool mangle_object_methods = false) {
200+
return MixedLower(std::string(method->name()), mangle_object_methods);
185201
}
186202

187203
static inline std::string MethodPropertiesFieldName(const MethodDescriptor* method) {
@@ -676,10 +692,12 @@ static void PrintStub(
676692
const MethodDescriptor* method = service->method(i);
677693
(*vars)["input_type"] = MessageFullJavaName(method->input_type());
678694
(*vars)["output_type"] = MessageFullJavaName(method->output_type());
679-
(*vars)["lower_method_name"] = LowerMethodName(method);
680-
(*vars)["method_method_name"] = MethodPropertiesGetterName(method);
681695
bool client_streaming = method->client_streaming();
682696
bool server_streaming = method->server_streaming();
697+
bool mangle_object_methods = (call_type == BLOCKING_V2_CALL && client_streaming)
698+
|| (call_type == BLOCKING_CALL && client_streaming && server_streaming);
699+
(*vars)["lower_method_name"] = LowerMethodName(method, mangle_object_methods);
700+
(*vars)["method_method_name"] = MethodPropertiesGetterName(method);
683701

684702
if (call_type == BLOCKING_CALL && client_streaming) {
685703
// Blocking client interface with client streaming is not available

0 commit comments

Comments
 (0)