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/06/01 05:17:58 UTC

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

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