Skip to content

Replace Constructor.newInstance with MethodHandle for code generator loading#782

Open
He-Pin wants to merge 1 commit into
mainfrom
refactor/replace-reflection-with-methodhandles
Open

Replace Constructor.newInstance with MethodHandle for code generator loading#782
He-Pin wants to merge 1 commit into
mainfrom
refactor/replace-reflection-with-methodhandles

Conversation

@He-Pin

@He-Pin He-Pin commented Jul 6, 2026

Copy link
Copy Markdown
Member

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

  • Cache MethodHandles for logger forwarding and extra generator constructors.
  • Load Scala singleton modules and generator bridge run methods through public MethodHandles.
  • Remove the Scala 2.12 ToolBox path in ReflectiveCodeGen and use the same MethodHandle target construction across Scala versions.
  • Use fixed-arity invoke() with explicit Object call-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

  • 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

References

None - internal refactoring

Comment thread codegen/src/main/scala/org/apache/pekko/grpc/gen/Main.scala Outdated
@He-Pin He-Pin marked this pull request as draft July 6, 2026 09:20
@He-Pin He-Pin force-pushed the refactor/replace-reflection-with-methodhandles branch from 97f22c9 to d0a8d36 Compare July 6, 2026 15:35
@He-Pin He-Pin marked this pull request as ready for review July 6, 2026 15:36
@He-Pin He-Pin requested a review from pjfanning July 6, 2026 15:36
Comment thread project/ReflectiveCodeGen.scala
@He-Pin He-Pin requested a review from pjfanning July 8, 2026 12:17
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
@He-Pin He-Pin force-pushed the refactor/replace-reflection-with-methodhandles branch from 5c86af2 to e0ff402 Compare July 8, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants