maropu commented on a change in pull request #28463:
URL: https://github.com/apache/spark/pull/28463#discussion_r422011000



##########
File path: core/src/main/scala/org/apache/spark/util/ClosureCleaner.scala
##########
@@ -414,6 +434,296 @@ private[spark] object ClosureCleaner extends Logging {
   }
 }
 
+private[spark] object IndylambdaScalaClosures extends Logging {
+  // internal name of java.lang.invoke.LambdaMetafactory
+  val LambdaMetafactoryClassName = "java/lang/invoke/LambdaMetafactory"
+  // the method that Scala indylambda use for bootstrap method
+  val LambdaMetafactoryMethodName = "altMetafactory"
+  val LambdaMetafactoryMethodDesc = "(Ljava/lang/invoke/MethodHandles$Lookup;" 
+
+    "Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)" +
+    "Ljava/lang/invoke/CallSite;"
+
+  /**
+   * Check if the given reference is a indylambda style Scala closure.
+   * If so, return a non-empty serialization proxy (SerializedLambda) of the 
closure;
+   * otherwise return None.
+   *
+   * @param maybeClosure the closure to check.
+   */
+  def getSerializationProxy(maybeClosure: AnyRef): Option[SerializedLambda] = {
+    val maybeClosureClass = maybeClosure.getClass
+
+    // shortcut the fast check:
+    // 1. indylambda closure classes are generated by Java's 
LambdaMetafactory, and they're always
+    //    synthetic.
+    // 2. We only care about Serializable closures, so let's check that as well
+    if (!maybeClosureClass.isSynthetic || 
!maybeClosure.isInstanceOf[Serializable]) return None

Review comment:
       nit: How about using pattern-matching here like this;
   ```
       
       def isClosureCandidate(clazz: Class[_]): Boolean = {
         val implementedInterfaces = ClassUtils.getAllInterfaces(clazz).asScala
         implementedInterfaces.exists(_.getName.startsWith("scala.Function"))
       }
   
       maybeClosure.getClass match {
         // shortcut the fast check:
         // 1. indylambda closure classes are generated by Java's 
LambdaMetafactory, and they're always
         //    synthetic.
         // 2. We only care about Serializable closures, so let's check that as 
well
         case c if !c.isSynthetic || !maybeClosure.isInstanceOf[Serializable] 
=> None
   
         case c if isClosureCandidate(c) =>
           try {
             val lambdaProxy = inspect(maybeClosure)
             Option(lambdaProxy).filter(isIndylambdaScalaClosure)
           } catch {
             case e: Exception =>
               // no need to check if debug is enabled here the Spark logging 
api covers this.
               logDebug("The given reference is not an indylambda Scala 
closure.", e)
               None
           }
   
         case _ => None
       }
   ```




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to