Skip to content

Use originating mirror for types instantiated from TypeTag. #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ import java.lang.reflect.Constructor

import scala.reflect.runtime.universe.Type
import scala.reflect.runtime.{universe => ru}

import ai.deepsense.deeplang.params.exceptions.NoArgumentConstructorRequiredException
import ai.deepsense.sparkutils

import scala.util.Try

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused


/**
* Holds methods used for manipulating objects representing types.
*/
object TypeUtils {
private val mirror = ru.runtimeMirror(getClass.getClassLoader)
private def classMirror(c: Class[_]) = ru.runtimeMirror(c.getClassLoader)

def classToType[T](c: Class[T]): ru.Type = classMirror(c).classSymbol(c).toType

def classToType(c: Class[_]): ru.Type = mirror.classSymbol(c).toType
def typeToClass(t: ru.Type): Class[_] = classMirror(t.getClass).runtimeClass(t.typeSymbol.asClass)

def typeToClass(t: ru.Type): Class[_] = mirror.runtimeClass(t.typeSymbol.asClass)
def typeTagToClass[T](t: ru.TypeTag[T]): Class[T] =
t.mirror.runtimeClass(t.tpe.typeSymbol.asClass).asInstanceOf[Class[T]]

def symbolToType(s: ru.Symbol): ru.Type = s.asClass.toType

Expand All @@ -41,26 +45,30 @@ object TypeUtils {
def isAbstract(c: Class[_]): Boolean =
sparkutils.TypeUtils.isAbstract(classToType(c).typeSymbol.asClass)

def constructorForClass(c: Class[_]): Option[Constructor[_]] = {
def constructorForClass[T](c: Class[T]): Option[Constructor[T]] = {
val constructors = c.getConstructors
val isParameterLess: (Constructor[_] => Boolean) = constructor =>
constructor.getParameterTypes.isEmpty
constructors.find(isParameterLess)
constructors.find(isParameterLess).map(_.asInstanceOf[Constructor[T]])
}

def constructorForTypeTag[T](t: ru.TypeTag[T]): Option[Constructor[T]] = {
constructorForClass(typeTagToClass(t))
}

def constructorForType(t: ru.Type): Option[Constructor[_]] = {
constructorForClass(typeToClass(t))
}

def createInstance[T](constructor: Constructor[_]): T = {
constructor.newInstance().asInstanceOf[T]
def createInstance[T](constructor: Constructor[T]): T = {
constructor.newInstance()
}

def instanceOfType[T](typeTag: ru.TypeTag[T]): T = {
val constructorT = constructorForType(typeTag.tpe).getOrElse {
val constructorT = constructorForTypeTag(typeTag).getOrElse {
throw NoArgumentConstructorRequiredException(typeTag.tpe.typeSymbol.asClass.name.decodedName.toString)
}
createInstance(constructorT).asInstanceOf[T]
createInstance(constructorT)
}

private val TypeSeparator = " with "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private[doperable] class ConcreteClassNode(javaType: Class[_]) extends ClassNode
* Invokes first constructor and assumes that it takes no parameters.
*/
private[doperable] def createInstance[T <: DOperable]: T = {
TypeUtils.createInstance[T](constructor)
TypeUtils.createInstance[T](constructor.asInstanceOf[Constructor[T]])
}

override private[doperable] def subclassesInstances: Set[ConcreteClassNode] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package ai.deepsense.deeplang.catalogs.doperable

import scala.reflect.runtime.{universe => ru}

import ru.{TypeTag, typeTag}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. One of those mornings.

import ai.deepsense.deeplang.TypeUtils

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ case class ParamsSequence[T <: Params](
JsArray(cells: _*)
}

private val constructor: Constructor[_] = TypeUtils.constructorForType(tag.tpe).getOrElse {
private val constructor: Constructor[_] = TypeUtils.constructorForTypeTag(tag).getOrElse {
throw NoArgumentConstructorRequiredException(tag.tpe.typeSymbol.asClass.name.decodedName.toString)
}

Expand Down