You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by "bowenliang123 (via GitHub)" <gi...@apache.org> on 2023/05/23 08:03:33 UTC

[GitHub] [kyuubi] bowenliang123 opened a new pull request, #4879: Promote usage of relection utils and remove redundant similar methods

bowenliang123 opened a new pull request, #4879:
URL: https://github.com/apache/kyuubi/pull/4879

   <!--
   Thanks for sending a pull request!
   
   Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/CONTRIBUTING.html
     2. If the PR is related to an issue in https://github.com/apache/kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'.
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'.
   -->
   
   ### _Why are the changes needed?_
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you add a feature, you can talk about the use case of it.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   - apply the usage of `ReflectUtils` to the modules of engines and plugins
   - remove similar redundant methods for calling reflected methods or getting field values
   
   ### _How was this patch tested?_
   - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible
   
   - [ ] Add screenshots for manual tests if appropriate
   
   - [ ] [Run test](https://kyuubi.readthedocs.io/en/master/develop_tools/testing.html#running-tests) locally before make a pull request
   


-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218818529


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,57 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
-    Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
-    } match {
-      case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
-    }
-
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
-
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
+  /**
+   * get the field value of the given object
+   * @param target the target object
+   * @param fieldName the field name from declared field names
+   * @tparam T the expected return class type
+   * @return
+   */
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     try {
-      val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
+      DynFields.builder()
+        .hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target)
+        .get()
     } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      case e: Exception =>
+        val candidates = targetClass.getDeclaredFields.map(_.getName).sorted
+        throw new RuntimeException(
+          s"Field $fieldName not in $targetClass [${candidates.mkString(",")}]",
+          e)
     }
+  }
 
-  def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T =
-    invoke(target, methodName, args: _*).asInstanceOf[T]
+  /**
+   * Invoke a method with the given name and arguments on the given target object.
+   * @param target the target object
+   * @param methodName the method name
+   * @param args pairs of class and values for the arguments
+   * @tparam T the expected return class type,
+   *           returning type Nothing if it's not provided or inferable
+   * @return
+   */
+  def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    val argClasses = args.map(_._1)
+    Try {

Review Comment:
   ditto, `try ... catch`



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1204165007


##########
tools/spark-block-cleaner/pom.xml:
##########
@@ -37,6 +37,12 @@
             <version>${project.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.kyuubi</groupId>
+            <artifactId>kyuubi-util-scala_${scala.binary.version}</artifactId>

Review Comment:
   Oh, good catch. Removed.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1204321229


##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/shim/CatalogShim_v3_0.scala:
##########
@@ -35,9 +36,8 @@ class CatalogShim_v3_0 extends CatalogShim_v2_4 {
     val defaultCatalog = catalogMgr.currentCatalog
 
     val defaults = Seq(sessionCatalog, defaultCatalog).distinct
-      .map(invoke(_, "name").asInstanceOf[String])
-    val catalogs = getField(catalogMgr, "catalogs")
-      .asInstanceOf[scala.collection.Map[String, _]]

Review Comment:
   OK, reverted.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#issuecomment-1577790649

   Overall LGTM, please 
   
   1. fix the failures
   2. test if Kyuubi Engine Tab works expect in Spark UI


-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1204152256


##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/shim/CatalogShim_v3_0.scala:
##########
@@ -35,9 +36,8 @@ class CatalogShim_v3_0 extends CatalogShim_v2_4 {
     val defaultCatalog = catalogMgr.currentCatalog
 
     val defaults = Seq(sessionCatalog, defaultCatalog).distinct
-      .map(invoke(_, "name").asInstanceOf[String])
-    val catalogs = getField(catalogMgr, "catalogs")
-      .asInstanceOf[scala.collection.Map[String, _]]

Review Comment:
   the SparkShim could be deleted directly since there is no plan to support spark 2, please don't touch it.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1204158794


##########
tools/spark-block-cleaner/src/test/scala/org.apache.kyuubi.tools/KubernetesSparkBlockCleanerSuite.scala:
##########
@@ -83,10 +84,7 @@ class KubernetesSparkBlockCleanerSuite extends KyuubiFunSuite {
   }
 
   private def updateEnv(name: String, value: String): Unit = {
-    val env = System.getenv
-    val field = env.getClass.getDeclaredField("m")
-    field.setAccessible(true)
-    field.get(env).asInstanceOf[java.util.Map[String, String]].put(name, value)
+    getFieldVal[java.util.Map[String, String]](System.getenv, "m").put(name, value)

Review Comment:
   `java.util.Map` => `util.Map` or `JMap`
   
   



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1219417529


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -33,9 +33,7 @@ object ReflectUtils {
   def isClassLoadable(
       className: String,
       cl: ClassLoader = Thread.currentThread().getContextClassLoader): Boolean =
-    Try {
-      DynClasses.builder().loader(cl).impl(className).buildChecked()
-    }.isSuccess
+    Option(DynClasses.builder().loader(cl).impl(className).orNull().buildChecked()).isDefined

Review Comment:
   All right. Let me revert this over-optimzation.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1219552788


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -33,9 +33,7 @@ object ReflectUtils {
   def isClassLoadable(
       className: String,
       cl: ClassLoader = Thread.currentThread().getContextClassLoader): Boolean =
-    Try {
-      DynClasses.builder().loader(cl).impl(className).buildChecked()
-    }.isSuccess
+    Option(DynClasses.builder().loader(cl).impl(className).orNull().buildChecked()).isDefined

Review Comment:
   Sorry that I just missed your last comment. Shall I issue a follow-up PR for this ?



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218049520


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {

Review Comment:
   Adopted.



##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      DynFields.builder().hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target).get()

Review Comment:
   Adopted.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218819020


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,57 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
-    Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
-    } match {
-      case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
-    }
-
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
-
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
+  /**
+   * get the field value of the given object
+   * @param target the target object
+   * @param fieldName the field name from declared field names
+   * @tparam T the expected return class type
+   * @return
+   */
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     try {
-      val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
+      DynFields.builder()
+        .hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target)
+        .get()
     } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      case e: Exception =>
+        val candidates = targetClass.getDeclaredFields.map(_.getName).sorted
+        throw new RuntimeException(
+          s"Field $fieldName not in $targetClass [${candidates.mkString(",")}]",
+          e)
     }
+  }
 
-  def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T =
-    invoke(target, methodName, args: _*).asInstanceOf[T]
+  /**
+   * Invoke a method with the given name and arguments on the given target object.
+   * @param target the target object
+   * @param methodName the method name
+   * @param args pairs of class and values for the arguments
+   * @tparam T the expected return class type,
+   *           returning type Nothing if it's not provided or inferable
+   * @return
+   */
+  def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    val argClasses = args.map(_._1)
+    Try {
+      DynMethods.builder(methodName)
+        .hiddenImpl(targetClass, argClasses: _*)
+        .buildChecked(target)
+        .invoke[T](args.map(_._2): _*)
+    } match {
+      case Success(value) => value
+      case Failure(t) =>
+        val candidates = targetClass.getMethods.map(_.getName).sorted

Review Comment:
   ditto: Declared



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1219558408


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -33,9 +33,7 @@ object ReflectUtils {
   def isClassLoadable(
       className: String,
       cl: ClassLoader = Thread.currentThread().getContextClassLoader): Boolean =
-    Try {
-      DynClasses.builder().loader(cl).impl(className).buildChecked()
-    }.isSuccess
+    Option(DynClasses.builder().loader(cl).impl(className).orNull().buildChecked()).isDefined

Review Comment:
   leave it, not a big deal



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 closed pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 closed pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods
URL: https://github.com/apache/kyuubi/pull/4879


-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217921524


##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/ui/EngineTab.scala:
##########
@@ -69,29 +69,28 @@ case class EngineTab(
         .impl("org.sparkproject.jetty.servlet.ServletContextHandler")
         .impl("org.eclipse.jetty.servlet.ServletContextHandler")
         .build()
-      val attachHandlerMethod = Class.forName("org.apache.spark.ui.SparkUI")
-        .getMethod("attachHandler", sparkServletContextHandlerClz)
-      val createRedirectHandlerMethod = Class.forName("org.apache.spark.ui.JettyUtils")
-        .getMethod(
-          "createRedirectHandler",
+      val attachHandlerMethod = DynMethods.builder("attachHandler")
+        .impl("org.apache.spark.ui.SparkUI", sparkServletContextHandlerClz)
+        .buildChecked(ui)
+      val createRedirectHandlerMethod = DynMethods.builder("createRedirectHandler")
+        .impl(
+          "org.apache.spark.ui.JettyUtils",
           classOf[String],
           classOf[String],
-          classOf[(HttpServletRequest) => Unit],
+          classOf[HttpServletRequest => Unit],
           classOf[String],
           classOf[Set[String]])
+        .buildChecked(null)

Review Comment:
   buildStaticChecked?



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1212543859


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +38,76 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  private def getFieldInternal[T](target: Any, fieldName: String, isDeclaredField: Boolean): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      Some(DynFields.builder())
+        .map(if (isDeclaredField) {
+          _.hiddenImpl(targetClass, fieldName)
+        } else {
+          _.impl(targetClass, fieldName)
+        }).get.buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = if (isDeclaredField) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getFields
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$fieldName not in $targetClass $candidates", t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
+  def getField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = false)
 
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
+  def getFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getField[T](target, name)).toOption
+
+  def getDeclaredField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = true)
+
+  def getDeclaredFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getDeclaredField[T](target, name)).toOption
+
+  private def invokeInternal[T](
+      target: AnyRef,
+      methodName: String,
+      isDeclaredMethod: Boolean,
+      args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    Try {
       val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      Some(DynMethods.builder(methodName))
+        .map(if (isDeclaredMethod) {
+          _.hiddenImpl(targetClass, types: _*)
+        } else {
+          _.impl(targetClass, types: _*)
+        }).get.buildChecked(target).invoke[T](values: _*)
+    } match {
+      case Success(value) => value
+      case Failure(t) =>
+        val candidates = if (isDeclaredMethod) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getMethods
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$methodName not in $targetClass $candidates", t)
     }
+  }
+
+  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
+    invokeInternal(target, methodName, isDeclaredMethod = false, args: _*)
 
   def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T =
-    invoke(target, methodName, args: _*).asInstanceOf[T]
+    invokeInternal[T](target, methodName, isDeclaredMethod = false, args: _*)
+
+  def invokeDeclared(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =

Review Comment:
   must we distinguish whether the method/field is declared or not?



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217427582


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +38,76 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  private def getFieldInternal[T](target: Any, fieldName: String, isDeclaredField: Boolean): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      Some(DynFields.builder())
+        .map(if (isDeclaredField) {
+          _.hiddenImpl(targetClass, fieldName)
+        } else {
+          _.impl(targetClass, fieldName)
+        }).get.buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = if (isDeclaredField) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getFields
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$fieldName not in $targetClass $candidates", t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
+  def getField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = false)
 
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
+  def getFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getField[T](target, name)).toOption
+
+  def getDeclaredField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = true)
+
+  def getDeclaredFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getDeclaredField[T](target, name)).toOption
+
+  private def invokeInternal[T](
+      target: AnyRef,
+      methodName: String,
+      isDeclaredMethod: Boolean,
+      args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    Try {
       val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      Some(DynMethods.builder(methodName))
+        .map(if (isDeclaredMethod) {
+          _.hiddenImpl(targetClass, types: _*)
+        } else {
+          _.impl(targetClass, types: _*)

Review Comment:
   Let me have a try on this.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217915290


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      DynFields.builder().hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target).get()

Review Comment:
   let's break the line to make code more readable
   ```suggestion
         DynFields.builder()
           .hiddenImpl(targetClass, fieldName)
           .buildChecked[T](target)
           .get()
   ```



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217920145


##########
kyuubi-common/src/main/scala/org/apache/kyuubi/util/KyuubiHadoopUtils.scala:
##########
@@ -77,8 +75,7 @@ object KyuubiHadoopUtils extends Logging {
    * Hadoop 3.2.1.
    */
   def getTokenMap(credentials: Credentials): Map[Text, Token[_ <: TokenIdentifier]] = {
-    tokenMapField.get(credentials)
-      .asInstanceOf[JMap[Text, Token[_ <: TokenIdentifier]]]
+    getField[JMap[Text, Token[_ <: TokenIdentifier]]](credentials, "tokenMap")

Review Comment:
   merge L79-80 into one line?



##########
kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/EngineSecuritySecretProvider.scala:
##########
@@ -50,9 +51,9 @@ class SimpleEngineSecuritySecretProviderImpl extends EngineSecuritySecretProvide
 
 object EngineSecuritySecretProvider {
   def create(conf: KyuubiConf): EngineSecuritySecretProvider = {
-    val providerClass = Class.forName(conf.get(ENGINE_SECURITY_SECRET_PROVIDER))
-    val provider = providerClass.getConstructor().newInstance()
-      .asInstanceOf[EngineSecuritySecretProvider]
+    val provider = DynConstructors.builder().impl(conf.get(ENGINE_SECURITY_SECRET_PROVIDER))
+      .buildChecked[EngineSecuritySecretProvider]()
+      .newInstance(conf)

Review Comment:
   ```suggestion
       val provider = DynConstructors.builder()
         .impl(conf.get(ENGINE_SECURITY_SECRET_PROVIDER))
         .buildChecked[EngineSecuritySecretProvider]()
         .newInstance(conf)
   ```



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217916816


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {

Review Comment:
   the scala `Try` does not help here and may miss FatalError, instead, `try ... catch ...` is more suitable



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218824105


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,57 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
-    Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
-    } match {
-      case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
-    }
-
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
-
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
+  /**
+   * get the field value of the given object
+   * @param target the target object
+   * @param fieldName the field name from declared field names
+   * @tparam T the expected return class type
+   * @return
+   */
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     try {
-      val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
+      DynFields.builder()
+        .hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target)
+        .get()
     } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      case e: Exception =>
+        val candidates = targetClass.getDeclaredFields.map(_.getName).sorted
+        throw new RuntimeException(
+          s"Field $fieldName not in $targetClass [${candidates.mkString(",")}]",
+          e)
     }
+  }
 
-  def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T =
-    invoke(target, methodName, args: _*).asInstanceOf[T]
+  /**
+   * Invoke a method with the given name and arguments on the given target object.
+   * @param target the target object
+   * @param methodName the method name
+   * @param args pairs of class and values for the arguments
+   * @tparam T the expected return class type,
+   *           returning type Nothing if it's not provided or inferable
+   * @return
+   */
+  def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    val argClasses = args.map(_._1)
+    Try {
+      DynMethods.builder(methodName)
+        .hiddenImpl(targetClass, argClasses: _*)
+        .buildChecked(target)
+        .invoke[T](args.map(_._2): _*)
+    } match {
+      case Success(value) => value
+      case Failure(t) =>
+        val candidates = targetClass.getMethods.map(_.getName).sorted

Review Comment:
   OK, adopted.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218120270


##########
kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/EngineSecuritySecretProvider.scala:
##########
@@ -50,9 +51,9 @@ class SimpleEngineSecuritySecretProviderImpl extends EngineSecuritySecretProvide
 
 object EngineSecuritySecretProvider {
   def create(conf: KyuubiConf): EngineSecuritySecretProvider = {
-    val providerClass = Class.forName(conf.get(ENGINE_SECURITY_SECRET_PROVIDER))
-    val provider = providerClass.getConstructor().newInstance()
-      .asInstanceOf[EngineSecuritySecretProvider]
+    val provider = DynConstructors.builder().impl(conf.get(ENGINE_SECURITY_SECRET_PROVIDER))
+      .buildChecked[EngineSecuritySecretProvider]()
+      .newInstance(conf)

Review Comment:
   Adopted.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#issuecomment-1578471512

   Thanks, merged to master.


-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217913514


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      DynFields.builder().hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = targetClass.getFields.map(_.getName).sorted

Review Comment:
   maybe targetClass.getDeclaredFields ?



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#issuecomment-1577912868

   <img width="1042" alt="image" src="https://github.com/apache/kyuubi/assets/1935105/d6ab8f0f-fde4-47bd-b485-f5c3456d912b">
   <img width="453" alt="image" src="https://github.com/apache/kyuubi/assets/1935105/e7d89fe4-eac8-43f7-8c97-6a2bd12be86f">
   
   Spark UI's Engine Tab works as expected.
   - shows engine tab
   - 'stop immediately' button
   


-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1204173748


##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/shim/CatalogShim_v3_0.scala:
##########
@@ -35,9 +36,8 @@ class CatalogShim_v3_0 extends CatalogShim_v2_4 {
     val defaultCatalog = catalogMgr.currentCatalog
 
     val defaults = Seq(sessionCatalog, defaultCatalog).distinct
-      .map(invoke(_, "name").asInstanceOf[String])
-    val catalogs = getField(catalogMgr, "catalogs")
-      .asInstanceOf[scala.collection.Map[String, _]]

Review Comment:
   Did you suggest reverting all changes in `CatalogShim_v3_0.scala` and leaving it to be deleted soon?



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1204156317


##########
tools/spark-block-cleaner/pom.xml:
##########
@@ -37,6 +37,12 @@
             <version>${project.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.kyuubi</groupId>
+            <artifactId>kyuubi-util-scala_${scala.binary.version}</artifactId>

Review Comment:
   seems redundant, since kyuubi-common depends on it



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#issuecomment-1570312276

   Conflicts resolved. Ready for reviewing. cc @pan3793 


-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217917366


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      DynFields.builder().hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = targetClass.getFields.map(_.getName).sorted
+        throw new RuntimeException(
+          s"Field $fieldName not in $targetClass [${candidates.mkString(",")}]",
+          t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
-
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
-      val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+  /**
+   * Invoke a method with the given name and arguments on the given target object.
+   * @param target the target object
+   * @param methodName the method name
+   * @param args pairs of class and values for the arguments
+   * @tparam T the expected return class type,
+   *           returning type Nothing if it's not provided or inferable
+   * @return
+   */
+  def invoke[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    val argClasses = args.map(_._1)
+    Try {
+      DynMethods.builder(methodName).hiddenImpl(targetClass, argClasses: _*)
+        .buildChecked(target).invoke[T](args.map(_._2): _*)

Review Comment:
   ```suggestion
         DynMethods.builder(methodName)
           .hiddenImpl(targetClass, argClasses: _*)
           .buildChecked(target)
           .invoke[T](args.map(_._2): _*)
   ```



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218824789


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,57 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
-    Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
-    } match {
-      case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
-    }
-
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
-
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
+  /**
+   * get the field value of the given object
+   * @param target the target object
+   * @param fieldName the field name from declared field names
+   * @tparam T the expected return class type
+   * @return
+   */
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     try {
-      val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
+      DynFields.builder()
+        .hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target)
+        .get()
     } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      case e: Exception =>
+        val candidates = targetClass.getDeclaredFields.map(_.getName).sorted
+        throw new RuntimeException(
+          s"Field $fieldName not in $targetClass [${candidates.mkString(",")}]",
+          e)
     }
+  }
 
-  def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T =
-    invoke(target, methodName, args: _*).asInstanceOf[T]
+  /**
+   * Invoke a method with the given name and arguments on the given target object.
+   * @param target the target object
+   * @param methodName the method name
+   * @param args pairs of class and values for the arguments
+   * @tparam T the expected return class type,
+   *           returning type Nothing if it's not provided or inferable
+   * @return
+   */
+  def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    val argClasses = args.map(_._1)
+    Try {

Review Comment:
   OK.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1219230729


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -33,9 +33,7 @@ object ReflectUtils {
   def isClassLoadable(
       className: String,
       cl: ClassLoader = Thread.currentThread().getContextClassLoader): Boolean =
-    Try {
-      DynClasses.builder().loader(cl).impl(className).buildChecked()
-    }.isSuccess
+    Option(DynClasses.builder().loader(cl).impl(className).orNull().buildChecked()).isDefined

Review Comment:
   maybe `Try(...).isSuccess` is more intuitive?



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1219691315


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -33,9 +33,7 @@ object ReflectUtils {
   def isClassLoadable(
       className: String,
       cl: ClassLoader = Thread.currentThread().getContextClassLoader): Boolean =
-    Try {
-      DynClasses.builder().loader(cl).impl(className).buildChecked()
-    }.isSuccess
+    Option(DynClasses.builder().loader(cl).impl(className).orNull().buildChecked()).isDefined

Review Comment:
   All right, thx.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1204186398


##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/shim/CatalogShim_v3_0.scala:
##########
@@ -35,9 +36,8 @@ class CatalogShim_v3_0 extends CatalogShim_v2_4 {
     val defaultCatalog = catalogMgr.currentCatalog
 
     val defaults = Seq(sessionCatalog, defaultCatalog).distinct
-      .map(invoke(_, "name").asInstanceOf[String])
-    val catalogs = getField(catalogMgr, "catalogs")
-      .asInstanceOf[scala.collection.Map[String, _]]

Review Comment:
   yes



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] codecov-commenter commented on pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#issuecomment-1560359120

   ## [Codecov](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   > Merging [#4879](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (38bc5de) into [master](https://app.codecov.io/gh/apache/kyuubi/commit/320178bb685081d99ed1474504a318f34ee1f19e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (320178b) will **not change** coverage.
   > The diff coverage is `0.00%`.
   
   ```diff
   @@          Coverage Diff           @@
   ##           master   #4879   +/-   ##
   ======================================
     Coverage    0.00%   0.00%           
   ======================================
     Files         556     556           
     Lines       30677   30648   -29     
     Branches     3996    3992    -4     
   ======================================
   + Misses      30677   30648   -29     
   ```
   
   
   | [Impacted Files](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [.../kyuubi/plugin/spark/authz/PrivilegesBuilder.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZXh0ZW5zaW9ucy9zcGFyay9reXV1Ymktc3BhcmstYXV0aHovc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9reXV1YmkvcGx1Z2luL3NwYXJrL2F1dGh6L1ByaXZpbGVnZXNCdWlsZGVyLnNjYWxh) | `0.00% <0.00%> (ø)` | |
   | [...uubi/plugin/spark/authz/ranger/AccessRequest.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZXh0ZW5zaW9ucy9zcGFyay9reXV1Ymktc3BhcmstYXV0aHovc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9reXV1YmkvcGx1Z2luL3NwYXJrL2F1dGh6L3Jhbmdlci9BY2Nlc3NSZXF1ZXN0LnNjYWxh) | `0.00% <0.00%> (ø)` | |
   | [...i/plugin/spark/authz/serde/catalogExtractors.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZXh0ZW5zaW9ucy9zcGFyay9reXV1Ymktc3BhcmstYXV0aHovc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9reXV1YmkvcGx1Z2luL3NwYXJrL2F1dGh6L3NlcmRlL2NhdGFsb2dFeHRyYWN0b3JzLnNjYWxh) | `0.00% <0.00%> (ø)` | |
   | [...ubi/plugin/spark/authz/serde/tableExtractors.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZXh0ZW5zaW9ucy9zcGFyay9reXV1Ymktc3BhcmstYXV0aHovc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9reXV1YmkvcGx1Z2luL3NwYXJrL2F1dGh6L3NlcmRlL3RhYmxlRXh0cmFjdG9ycy5zY2FsYQ==) | `0.00% <0.00%> (ø)` | |
   | [...in/lineage/helper/SparkSQLLineageParseHelper.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZXh0ZW5zaW9ucy9zcGFyay9reXV1Ymktc3BhcmstbGluZWFnZS9zcmMvbWFpbi9zY2FsYS9vcmcvYXBhY2hlL2t5dXViaS9wbHVnaW4vbGluZWFnZS9oZWxwZXIvU3BhcmtTUUxMaW5lYWdlUGFyc2VIZWxwZXIuc2NhbGE=) | `0.00% <0.00%> (ø)` | |
   | [...he/kyuubi/engine/spark/shim/CatalogShim\_v3\_0.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZXh0ZXJuYWxzL2t5dXViaS1zcGFyay1zcWwtZW5naW5lL3NyYy9tYWluL3NjYWxhL29yZy9hcGFjaGUva3l1dWJpL2VuZ2luZS9zcGFyay9zaGltL0NhdGFsb2dTaGltX3YzXzAuc2NhbGE=) | `0.00% <0.00%> (ø)` | |
   | [...he/kyuubi/engine/spark/shim/SparkCatalogShim.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZXh0ZXJuYWxzL2t5dXViaS1zcGFyay1zcWwtZW5naW5lL3NyYy9tYWluL3NjYWxhL29yZy9hcGFjaGUva3l1dWJpL2VuZ2luZS9zcGFyay9zaGltL1NwYXJrQ2F0YWxvZ1NoaW0uc2NhbGE=) | `0.00% <ø> (ø)` | |
   | [...n/scala/org/apache/kyuubi/KyuubiSQLException.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-a3l1dWJpLWNvbW1vbi9zcmMvbWFpbi9zY2FsYS9vcmcvYXBhY2hlL2t5dXViaS9LeXV1YmlTUUxFeGNlcHRpb24uc2NhbGE=) | `0.00% <0.00%> (ø)` | |
   | [...ala/org/apache/kyuubi/util/KyuubiHadoopUtils.scala](https://app.codecov.io/gh/apache/kyuubi/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-a3l1dWJpLWNvbW1vbi9zcmMvbWFpbi9zY2FsYS9vcmcvYXBhY2hlL2t5dXViaS91dGlsL0t5dXViaUhhZG9vcFV0aWxzLnNjYWxh) | `0.00% <0.00%> (ø)` | |
   
   :mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   


-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: [WIP] Promote usage of relection utils and cleanup redundant methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1204173748


##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/shim/CatalogShim_v3_0.scala:
##########
@@ -35,9 +36,8 @@ class CatalogShim_v3_0 extends CatalogShim_v2_4 {
     val defaultCatalog = catalogMgr.currentCatalog
 
     val defaults = Seq(sessionCatalog, defaultCatalog).distinct
-      .map(invoke(_, "name").asInstanceOf[String])
-    val catalogs = getField(catalogMgr, "catalogs")
-      .asInstanceOf[scala.collection.Map[String, _]]

Review Comment:
   Did you suggest reverting all changes in `CatalogShim_v3_0` and `SparkCatalogShim`, leaving it to be deleted soon?



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#issuecomment-1577940428

   ![image](https://github.com/apache/kyuubi/assets/1935105/71940023-0d91-43a2-9850-79b7fefa2176)
   
   Fixed a bug when executing `bin/beeline`.


-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217425916


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +38,76 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  private def getFieldInternal[T](target: Any, fieldName: String, isDeclaredField: Boolean): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      Some(DynFields.builder())
+        .map(if (isDeclaredField) {
+          _.hiddenImpl(targetClass, fieldName)
+        } else {
+          _.impl(targetClass, fieldName)
+        }).get.buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = if (isDeclaredField) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getFields
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$fieldName not in $targetClass $candidates", t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
+  def getField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = false)
 
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
+  def getFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getField[T](target, name)).toOption
+
+  def getDeclaredField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = true)
+
+  def getDeclaredFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getDeclaredField[T](target, name)).toOption
+
+  private def invokeInternal[T](
+      target: AnyRef,
+      methodName: String,
+      isDeclaredMethod: Boolean,
+      args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    Try {
       val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      Some(DynMethods.builder(methodName))
+        .map(if (isDeclaredMethod) {
+          _.hiddenImpl(targetClass, types: _*)
+        } else {
+          _.impl(targetClass, types: _*)

Review Comment:
   what if we always call `hiddenImpl`, then we don't need to expose `isDeclared*` concept to caller



##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +38,76 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  private def getFieldInternal[T](target: Any, fieldName: String, isDeclaredField: Boolean): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      Some(DynFields.builder())
+        .map(if (isDeclaredField) {
+          _.hiddenImpl(targetClass, fieldName)
+        } else {
+          _.impl(targetClass, fieldName)
+        }).get.buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = if (isDeclaredField) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getFields
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$fieldName not in $targetClass $candidates", t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
+  def getField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = false)
 
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
+  def getFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getField[T](target, name)).toOption
+
+  def getDeclaredField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = true)
+
+  def getDeclaredFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getDeclaredField[T](target, name)).toOption
+
+  private def invokeInternal[T](
+      target: AnyRef,
+      methodName: String,
+      isDeclaredMethod: Boolean,
+      args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    Try {
       val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      Some(DynMethods.builder(methodName))
+        .map(if (isDeclaredMethod) {
+          _.hiddenImpl(targetClass, types: _*)
+        } else {
+          _.impl(targetClass, types: _*)

Review Comment:
   what if always call `hiddenImpl`, then we don't need to expose `isDeclared*` concept to caller



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1217914173


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      DynFields.builder().hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = targetClass.getFields.map(_.getName).sorted
+        throw new RuntimeException(
+          s"Field $fieldName not in $targetClass [${candidates.mkString(",")}]",
+          t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
-
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
-      val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+  /**
+   * Invoke a method with the given name and arguments on the given target object.
+   * @param target the target object
+   * @param methodName the method name
+   * @param args pairs of class and values for the arguments
+   * @tparam T the expected return class type,
+   *           returning type Nothing if it's not provided or inferable
+   * @return
+   */
+  def invoke[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T = {

Review Comment:
   the `invokeAs` should be fine which also reduces the diff



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218120575


##########
kyuubi-common/src/main/scala/org/apache/kyuubi/util/KyuubiHadoopUtils.scala:
##########
@@ -77,8 +75,7 @@ object KyuubiHadoopUtils extends Logging {
    * Hadoop 3.2.1.
    */
   def getTokenMap(credentials: Credentials): Map[Text, Token[_ <: TokenIdentifier]] = {
-    tokenMapField.get(credentials)
-      .asInstanceOf[JMap[Text, Token[_ <: TokenIdentifier]]]
+    getField[JMap[Text, Token[_ <: TokenIdentifier]]](credentials, "tokenMap")

Review Comment:
   Adopted.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1212543859


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +38,76 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  private def getFieldInternal[T](target: Any, fieldName: String, isDeclaredField: Boolean): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      Some(DynFields.builder())
+        .map(if (isDeclaredField) {
+          _.hiddenImpl(targetClass, fieldName)
+        } else {
+          _.impl(targetClass, fieldName)
+        }).get.buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = if (isDeclaredField) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getFields
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$fieldName not in $targetClass $candidates", t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
+  def getField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = false)
 
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
+  def getFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getField[T](target, name)).toOption
+
+  def getDeclaredField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = true)
+
+  def getDeclaredFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getDeclaredField[T](target, name)).toOption
+
+  private def invokeInternal[T](
+      target: AnyRef,
+      methodName: String,
+      isDeclaredMethod: Boolean,
+      args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    Try {
       val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      Some(DynMethods.builder(methodName))
+        .map(if (isDeclaredMethod) {
+          _.hiddenImpl(targetClass, types: _*)
+        } else {
+          _.impl(targetClass, types: _*)
+        }).get.buildChecked(target).invoke[T](values: _*)
+    } match {
+      case Success(value) => value
+      case Failure(t) =>
+        val candidates = if (isDeclaredMethod) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getMethods
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$methodName not in $targetClass $candidates", t)
     }
+  }
+
+  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
+    invokeInternal(target, methodName, isDeclaredMethod = false, args: _*)
 
   def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T =
-    invoke(target, methodName, args: _*).asInstanceOf[T]
+    invokeInternal[T](target, methodName, isDeclaredMethod = false, args: _*)
+
+  def invokeDeclared(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =

Review Comment:
   must we distinguish is the method/field declared or not?



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1212582247


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +38,76 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  private def getFieldInternal[T](target: Any, fieldName: String, isDeclaredField: Boolean): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      Some(DynFields.builder())
+        .map(if (isDeclaredField) {
+          _.hiddenImpl(targetClass, fieldName)
+        } else {
+          _.impl(targetClass, fieldName)
+        }).get.buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = if (isDeclaredField) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getFields
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$fieldName not in $targetClass $candidates", t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
+  def getField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = false)
 
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
+  def getFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getField[T](target, name)).toOption
+
+  def getDeclaredField[T](target: Any, fieldName: String): T =
+    getFieldInternal[T](target, fieldName, isDeclaredField = true)
+
+  def getDeclaredFieldOpt[T](target: Any, name: String): Option[T] =
+    Try(getDeclaredField[T](target, name)).toOption
+
+  private def invokeInternal[T](
+      target: AnyRef,
+      methodName: String,
+      isDeclaredMethod: Boolean,
+      args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    Try {
       val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+      Some(DynMethods.builder(methodName))
+        .map(if (isDeclaredMethod) {
+          _.hiddenImpl(targetClass, types: _*)
+        } else {
+          _.impl(targetClass, types: _*)
+        }).get.buildChecked(target).invoke[T](values: _*)
+    } match {
+      case Success(value) => value
+      case Failure(t) =>
+        val candidates = if (isDeclaredMethod) {
+          targetClass.getDeclaredFields
+        } else {
+          targetClass.getMethods
+        }.map(_.getName).mkString("[", ",", "]")
+        throw new RuntimeException(s"$methodName not in $targetClass $candidates", t)
     }
+  }
+
+  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
+    invokeInternal(target, methodName, isDeclaredMethod = false, args: _*)
 
   def invokeAs[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T =
-    invoke(target, methodName, args: _*).asInstanceOf[T]
+    invokeInternal[T](target, methodName, isDeclaredMethod = false, args: _*)
+
+  def invokeDeclared(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =

Review Comment:
   I think so. As we are calling the method or getting fields on purpose, we must realize and decide whether it is a declared and normal method/field.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218049857


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      DynFields.builder().hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = targetClass.getFields.map(_.getName).sorted
+        throw new RuntimeException(
+          s"Field $fieldName not in $targetClass [${candidates.mkString(",")}]",
+          t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
-
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
-      val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+  /**
+   * Invoke a method with the given name and arguments on the given target object.
+   * @param target the target object
+   * @param methodName the method name
+   * @param args pairs of class and values for the arguments
+   * @tparam T the expected return class type,
+   *           returning type Nothing if it's not provided or inferable
+   * @return
+   */
+  def invoke[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T = {

Review Comment:
   Adopted.



##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      DynFields.builder().hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = targetClass.getFields.map(_.getName).sorted
+        throw new RuntimeException(
+          s"Field $fieldName not in $targetClass [${candidates.mkString(",")}]",
+          t)
     }
+  }
 
-  def getFieldValOpt[T](target: Any, name: String): Option[T] =
-    Try(getFieldVal[T](target, name)).toOption
-
-  def invoke(target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): AnyRef =
-    try {
-      val (types, values) = args.unzip
-      DynMethods.builder(methodName).hiddenImpl(target.getClass, types: _*).build()
-        .invoke(target, values: _*)
-    } catch {
-      case e: NoSuchMethodException =>
-        val candidates = target.getClass.getMethods.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$methodName not in ${target.getClass} $candidates", e)
+  /**
+   * Invoke a method with the given name and arguments on the given target object.
+   * @param target the target object
+   * @param methodName the method name
+   * @param args pairs of class and values for the arguments
+   * @tparam T the expected return class type,
+   *           returning type Nothing if it's not provided or inferable
+   * @return
+   */
+  def invoke[T](target: AnyRef, methodName: String, args: (Class[_], AnyRef)*): T = {
+    val targetClass = target.getClass
+    val argClasses = args.map(_._1)
+    Try {
+      DynMethods.builder(methodName).hiddenImpl(targetClass, argClasses: _*)
+        .buildChecked(target).invoke[T](args.map(_._2): _*)

Review Comment:
   Adopted.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218121351


##########
externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/ui/EngineTab.scala:
##########
@@ -69,29 +69,28 @@ case class EngineTab(
         .impl("org.sparkproject.jetty.servlet.ServletContextHandler")
         .impl("org.eclipse.jetty.servlet.ServletContextHandler")
         .build()
-      val attachHandlerMethod = Class.forName("org.apache.spark.ui.SparkUI")
-        .getMethod("attachHandler", sparkServletContextHandlerClz)
-      val createRedirectHandlerMethod = Class.forName("org.apache.spark.ui.JettyUtils")
-        .getMethod(
-          "createRedirectHandler",
+      val attachHandlerMethod = DynMethods.builder("attachHandler")
+        .impl("org.apache.spark.ui.SparkUI", sparkServletContextHandlerClz)
+        .buildChecked(ui)
+      val createRedirectHandlerMethod = DynMethods.builder("createRedirectHandler")
+        .impl(
+          "org.apache.spark.ui.JettyUtils",
           classOf[String],
           classOf[String],
-          classOf[(HttpServletRequest) => Unit],
+          classOf[HttpServletRequest => Unit],
           classOf[String],
           classOf[Set[String]])
+        .buildChecked(null)

Review Comment:
   Adopted.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "bowenliang123 (via GitHub)" <gi...@apache.org>.
bowenliang123 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1218119144


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -37,32 +37,47 @@ object ReflectUtils {
       DynClasses.builder().loader(cl).impl(className).buildChecked()
     }.isSuccess
 
-  def getFieldVal[T](target: Any, fieldName: String): T =
+  def getField[T](target: Any, fieldName: String): T = {
+    val targetClass = target.getClass
     Try {
-      DynFields.builder().hiddenImpl(target.getClass, fieldName).build[T]().get(target)
+      DynFields.builder().hiddenImpl(targetClass, fieldName)
+        .buildChecked[T](target).get()
     } match {
       case Success(value) => value
-      case Failure(e) =>
-        val candidates = target.getClass.getDeclaredFields.map(_.getName).mkString("[", ",", "]")
-        throw new RuntimeException(s"$fieldName not in ${target.getClass} $candidates", e)
+      case Failure(t) =>
+        val candidates = targetClass.getFields.map(_.getName).sorted

Review Comment:
   Fixed.



-- 
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@kyuubi.apache.org

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


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


[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4879: Refactor and promote relection utils and cleanup similar reflection methods

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #4879:
URL: https://github.com/apache/kyuubi/pull/4879#discussion_r1219422702


##########
kyuubi-util-scala/src/main/scala/org/apache/kyuubi/util/reflect/ReflectUtils.scala:
##########
@@ -33,9 +33,7 @@ object ReflectUtils {
   def isClassLoadable(
       className: String,
       cl: ClassLoader = Thread.currentThread().getContextClassLoader): Boolean =
-    Try {
-      DynClasses.builder().loader(cl).impl(className).buildChecked()
-    }.isSuccess
+    Option(DynClasses.builder().loader(cl).impl(className).orNull().buildChecked()).isDefined

Review Comment:
   but maybe we can replace `{}` w/ `()` and merged the code into one line



-- 
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@kyuubi.apache.org

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


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