You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "He-Pin (via GitHub)" <gi...@apache.org> on 2026/07/06 08:29:42 UTC

[PR] Replace Constructor.newInstance with MethodHandle for code generator loading [pekko-grpc]

He-Pin opened a new pull request, #782:
URL: https://github.com/apache/pekko-grpc/pull/782

   ### Motivation
   `Class.forName().getDeclaredConstructor().newInstance()` bypasses JIT optimization for constructor invocation in code generator loading.
   
   ### Modification
   Replace `getDeclaredConstructor().newInstance()` with `MethodHandles.lookup().unreflectConstructor().invoke()` in both the protoc plugin `Main` and the Maven plugin `AbstractGenerateMojo` for extra generator instantiation.
   
   ### Result
   `MethodHandle`-based invocation enables JIT inlining of constructor calls, providing a consistent reflection migration across the project.
   
   ### Tests
   - `sbt "codegen / compile"` - success
   - `sbt "maven-plugin / compile"` - success
   
   ### References
   None - internal refactoring


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace Constructor.newInstance with MethodHandle for code generator loading [pekko-grpc]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #782:
URL: https://github.com/apache/pekko-grpc/pull/782#discussion_r3539383217


##########
project/ReflectiveCodeGen.scala:
##########
@@ -122,59 +121,15 @@ object ReflectiveCodeGen extends AutoPlugin {
       generatorSettings: Seq[String],
       targets: ListBuffer[Target],
       scalaBinaryVersion: String): Unit = {
-    if (scalaBinaryVersion != "2.12") {

Review Comment:
   this change is not described in the PR description



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace Constructor.newInstance with MethodHandle for code generator loading [pekko-grpc]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #782:
URL: https://github.com/apache/pekko-grpc/pull/782#discussion_r3527647598


##########
codegen/src/main/scala/org/apache/pekko/grpc/gen/Main.scala:
##########
@@ -83,7 +84,12 @@ object Main {
         else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
       }
     val loadedExtraGenerators =
-      extraGenerators.map(cls => Class.forName(cls).getDeclaredConstructor().newInstance().asInstanceOf[CodeGenerator])
+      extraGenerators.map { cls =>
+        val clazz = Class.forName(cls)
+        val ctor = clazz.getDeclaredConstructor()
+        ctor.setAccessible(true)

Review Comment:
   setAccessible needed? cache MethodHandles.lookup()?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace Constructor.newInstance with MethodHandle for code generator loading [pekko-grpc]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #782:
URL: https://github.com/apache/pekko-grpc/pull/782#discussion_r3527673812


##########
codegen/src/main/scala/org/apache/pekko/grpc/gen/Main.scala:
##########
@@ -83,7 +84,12 @@ object Main {
         else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
       }
     val loadedExtraGenerators =
-      extraGenerators.map(cls => Class.forName(cls).getDeclaredConstructor().newInstance().asInstanceOf[CodeGenerator])
+      extraGenerators.map { cls =>
+        val clazz = Class.forName(cls)
+        val ctor = clazz.getDeclaredConstructor()
+        ctor.setAccessible(true)

Review Comment:
   this is not right , stay turn



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace Constructor.newInstance with MethodHandle for code generator loading [pekko-grpc]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin merged PR #782:
URL: https://github.com/apache/pekko-grpc/pull/782


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org