Replace Constructor.newInstance with MethodHandle for code generator loading#782
Open
He-Pin wants to merge 1 commit into
Open
Replace Constructor.newInstance with MethodHandle for code generator loading#782He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
pjfanning
reviewed
Jul 6, 2026
97f22c9 to
d0a8d36
Compare
pjfanning
reviewed
Jul 7, 2026
Motivation: Avoid reflective constructor, method, and Scala module invocation in gRPC code generation and plugin bridge paths while keeping generator loading compatible across Scala versions. Modification: Cache MethodHandles for logger forwarding and extra generator constructors. Use public MethodHandles for Scala singleton module access and generator bridge calls. Remove the Scala 2.12 ToolBox path in ReflectiveCodeGen and use fixed-arity invoke() call sites with explicit Object values where needed. Result: Codegen, Maven plugin, sbt plugin, and reflective test build generation use MethodHandles consistently while preserving public access semantics and avoiding avoidable invokeWithArguments boxing. Tests: - JDK 17: sbt "codegen / compile" "maven-plugin / compile" "sbt-plugin / compile" - success - JDK 17: sbt "++2.13.18!" codegen/test "++3.3.8!" codegen/test - success, 2 tests per version - JDK 17: sbt "++2.13.*" "sbt-plugin/scripted gen-scala-server/06-compatibility-plugins" - first failed because protoc-gen-go was missing; installed protoc-gen-go v1.32.0, rerun success - JDK 17: sbt "++3.8.4!" "sbt-plugin/scripted scala3/*" - success, 3 scripted tests - JDK 17: sbt "++2.13.*" "sbt-plugin/scripted gen-scala-server/06-compatibility-plugins" "++3.8.4!" "sbt-plugin/scripted scala3/03-sbt2-basic" - success after explicit Object annotations - scalafmt --mode diff-ref=origin/main - success - scalafmt --list --mode diff-ref=origin/main - success - git diff --check - success - Qoder stdout review: /tmp/project-qoder-review.log - No must-fix findings References: None - internal refactoring
5c86af2 to
e0ff402
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Replace reflection-based generator loading and bridge calls with MethodHandle-based access, avoiding reflective constructor, method, and Scala module invocation in code generation paths.
Modification
ReflectiveCodeGenand use the same MethodHandle target construction across Scala versions.invoke()with explicitObjectcall-site values where the JVM adapts MethodHandle types.Result
Codegen, Maven plugin, sbt plugin, and reflective test build generation use MethodHandles consistently while preserving public access semantics.
Tests
sbt "codegen / compile" "maven-plugin / compile" "sbt-plugin / compile"- successsbt "++2.13.18!" codegen/test "++3.3.8!" codegen/test- success, 2 tests per versionsbt "++2.13.*" "sbt-plugin/scripted gen-scala-server/06-compatibility-plugins"- first failed becauseprotoc-gen-gowas missing; installedprotoc-gen-go v1.32.0, rerun successsbt "++3.8.4!" "sbt-plugin/scripted scala3/*"- success, 3 scripted testssbt "++2.13.*" "sbt-plugin/scripted gen-scala-server/06-compatibility-plugins" "++3.8.4!" "sbt-plugin/scripted scala3/03-sbt2-basic"- success after explicitObjectannotationsscalafmt --mode diff-ref=origin/main- successscalafmt --list --mode diff-ref=origin/main- successgit diff --check- successReferences
None - internal refactoring