Skip to content

Commit 315c625

Browse files
dongjoon-hyunpeter-toth
authored andcommitted
[SPARK-52815][CORE] Improve SparkClassUtils to support getAllInterfaces
### What changes were proposed in this pull request? This PR aims to improve `SparkClassUtils` to support `getAllInterfaces`. ### Why are the changes needed? To provide a more API in `SparkClassUtils` instead of depending on `org.apache.commons.lang3.ClassUtils.getAllInterfaces` ### Does this PR introduce _any_ user-facing change? No. `SparkClassUtils` are a private trait. ### How was this patch tested? Pass the CIs. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #51509 from dongjoon-hyun/SPARK-52815. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Peter Toth <[email protected]>
1 parent fac4433 commit 315c625

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

common/utils/src/main/scala/org/apache/spark/util/ClosureCleaner.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import java.lang.reflect.{Field, Modifier}
2424
import scala.collection.mutable.{Map, Queue, Set, Stack}
2525
import scala.jdk.CollectionConverters._
2626

27-
import org.apache.commons.lang3.ClassUtils
2827
import org.apache.xbean.asm9.{ClassReader, ClassVisitor, Handle, MethodVisitor, Type}
2928
import org.apache.xbean.asm9.Opcodes._
3029
import org.apache.xbean.asm9.tree.{ClassNode, MethodNode}
@@ -619,7 +618,7 @@ private[spark] object IndylambdaScalaClosures extends Logging {
619618
def getSerializationProxy(maybeClosure: AnyRef): Option[SerializedLambda] = {
620619
def isClosureCandidate(cls: Class[_]): Boolean = {
621620
// TODO: maybe lift this restriction to support other functional interfaces in the future
622-
val implementedInterfaces = ClassUtils.getAllInterfaces(cls).asScala
621+
val implementedInterfaces = SparkClassUtils.getAllInterfaces(cls)
623622
implementedInterfaces.exists(_.getName.startsWith("scala.Function"))
624623
}
625624

common/utils/src/main/scala/org/apache/spark/util/SparkClassUtils.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.apache.spark.util
1818

1919
import java.util.Random
2020

21+
import scala.collection.mutable.LinkedHashSet
2122
import scala.util.Try
2223

2324
private[spark] trait SparkClassUtils {
@@ -136,6 +137,33 @@ private[spark] trait SparkClassUtils {
136137
}
137138
}
138139
}
140+
141+
/**
142+
* Gets a list of all interfaces implemented by the given class and its superclasses.
143+
*/
144+
def getAllInterfaces(cls: Class[_]): List[Class[_]] = {
145+
if (cls == null) {
146+
return null
147+
}
148+
val interfacesFound = LinkedHashSet[Class[_]]()
149+
getAllInterfacesHelper(cls, interfacesFound)
150+
interfacesFound.toList
151+
}
152+
153+
private def getAllInterfacesHelper(
154+
clazz: Class[_],
155+
interfacesFound: LinkedHashSet[Class[_]]): Unit = {
156+
var currentClass = clazz
157+
while (currentClass != null) {
158+
val interfaces = currentClass.getInterfaces
159+
for (i <- interfaces) {
160+
if (interfacesFound.add(i)) {
161+
getAllInterfacesHelper(i, interfacesFound)
162+
}
163+
}
164+
currentClass = currentClass.getSuperclass
165+
}
166+
}
139167
}
140168

141169
private[spark] object SparkClassUtils extends SparkClassUtils

0 commit comments

Comments
 (0)