Github user srowen commented on a diff in the pull request:
https://github.com/apache/spark/pull/23062#discussion_r235033743
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/ScalaReflection.scala
---
@@ -788,12 +788,37 @@ object ScalaReflection extends ScalaReflection {
}
/**
- * Finds an accessible constructor with compatible parameters. This is a
more flexible search
- * than the exact matching algorithm in `Class.getConstructor`. The
first assignment-compatible
- * matching constructor is returned. Otherwise, it returns `None`.
+ * Finds an accessible constructor with compatible parameters. This is a
more flexible search than
+ * the exact matching algorithm in `Class.getConstructor`. The first
assignment-compatible
+ * matching constructor is returned if it exists. Otherwise, we check
for additional compatible
+ * constructors defined in the companion object as `apply` methods.
Otherwise, it returns `None`.
*/
- def findConstructor(cls: Class[_], paramTypes: Seq[Class[_]]):
Option[Constructor[_]] = {
- Option(ConstructorUtils.getMatchingAccessibleConstructor(cls,
paramTypes: _*))
+ def findConstructor[T](cls: Class[T], paramTypes: Seq[Class[_]]):
Option[Seq[AnyRef] => T] = {
+ Option(ConstructorUtils.getMatchingAccessibleConstructor(cls,
paramTypes: _*)) match {
+ case Some(c) => Some(x => c.newInstance(x: _*).asInstanceOf[T])
--- End diff --
I think this cast isn't needed because Class[T].newInstance returns T
already, but it's fine to leave.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]