Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package database
- import scala.reflect.ClassTag
- trait Mapper[T] {
- def map(row: List[Any]): T
- }
- object TestMapper {
- def getMapper[T: ClassTag](outer: Option[AnyRef] = None) = {
- val tt = implicitly[ClassTag[T]]
- val rclass = tt.runtimeClass
- val ctor = rclass.getConstructors()(0)
- val ctypes = ctor.getParameterTypes
- ctypes.foreach(println)
- val cl = getClass
- new Mapper[T] {
- def map(row: List[Any]): T = {
- if(outer.isDefined)
- ctor.newInstance(outer.get::row.map(_.asInstanceOf[Object]):_*).asInstanceOf[T]
- else
- ctor.newInstance(row.map(_.asInstanceOf[Object]):_*).asInstanceOf[T]
- }
- }
- }
- }
- object ExternalCaseClasses {
- case class ExternalYoba(size: Int, color: String)
- }
- class TestMapper {
- import TestMapper._
- import ExternalCaseClasses._
- def callAnonByName(f: => Unit) = f
- callAnonByName { // никакого отсоса
- getMapper[ExternalYoba]().map(List(10, "yellow"))
- } //работает
- def outerMethod() = { // отсос средней величины, но пока что терпимо
- case class InternalYoba(size: Int, color: String)
- getMapper[InternalYoba](Some(this)).map(List(100, "red"))
- }
- outerMethod() //работает
- val anonFunc: () => Unit = () => { // пукан нехило разогрелся
- case class InternalYoba(size: Int, color: String)
- getMapper[InternalYoba](Some(anonFunc)).map(List(100, "red"))
- }
- anonFunc() //работает
- callAnonByName { // непреодолимый отсос, пукан на небесах
- case class InternalYoba(size: Int, color: String)
- getMapper[InternalYoba](Some(this)).map(List(10, "jack"))
- } // кровь кишки экспешон
- }
- object Run extends App {
- new TestMapper
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement