2012-06-13 4 views
27

스칼라의 새로운 리플렉션 API를 사용하면 클래스의 컴패니언 개체에 대한 참조를 가져올 수 있습니까? 이 줄을 따라 뭔가 생각하고 있습니다.새 스칼라 리플렉션 API로 컴패니언 개체 인스턴스 가져 오기

trait Base { 
    def companion: MetaBase = someReflectionMagic(this).asInstanceOf[MetaBase] 
} 

trait MetaBase { 
    // stuff 
} 

// --- 

class Foo extends Base 

object Foo extends MetaBase 

assert(new Foo.companion == Foo) 
+0

나는 당신이 이것과 같이 떠날 수있을 것이라고 생각한다. http://stackoverflow.com/questions/1913092/getting-object-instance-by-string-name-in-scala – Noah

+3

나는 무엇인가를 기대하고 있었다. 새로운 리플렉션 API를 사용하여 – leedm777

답변

37

Dave! 새로운 반성에 관심을 가져 주셔서 감사합니다. 얼리 어답터는 리플렉션 및 매크로의 개발 프로세스를 상당 부분 수행하도록 이끌었으며, 나는 놀라운 커뮤니티에 참여하게되어 매우 기쁩니다.

질문에 답하기 전에 면책 조항부터 시작하겠습니다. 2.10.0-M4에서 스칼라 리플렉션 API의 토대를 마련했습니다. 언론으로부터 아직 뜨거워서 문서가 아주 희소하고 API가 편의성으로 가득 차 있지는 않습니다. 작동하지만 테스트 및 피드백이 필요합니다. 물론 출시 전 API를 망쳐 놓는 일은 번거롭지 만 언제나 도움이됩니다.

지금까지 우리는 향후 반사 SIP가 될 내용에 대한 초안을 작성했습니다 : https://docs.google.com/document/d/1Z1VhhNPplbUpaZPIYdc0_EUv5RiGQ2X4oqp0i-vz1qw/edit#heading=h.pqwdkl1226tc. 당신은 즉시 그것을 읽을 수 있습니다, 또는 아래에 내 대답을 통해 먼저 탈지 수 있습니다.

trait Base { 
    def companion: MetaBase = { 
    // runtime reflection is typically done 
    // by importing things from scala.reflect.runtime package 
    import scala.reflect.runtime._ 

    // the new Scala reflection API is mirror based 
    // mirrors constitute a hierarchy of objects 
    // that closely follows the hierarchy of the things they reflect 
    // for example, for a class you'll have a ClassMirror 
    // for a method you'll have a MethodMirror and so on 
    // why go the extra mile? 
    // because this provides more flexibility than traditional approaches 
    // you can read more about mirror-based designs here: 
    // https://dl.dropbox.com/u/10497693/Library/Computer%20Science/Metaprogramming/Reflection/mirrors.pdf 
    // https://dl.dropbox.com/u/10497693/Library/Computer%20Science/Metaprogramming/Reflection/reflecting-scala.pdf 

    // bottom line is that to do anything you will need a mirror 
    // for example, in your case, you need a ClassMirror 

    // remember I said that mirrors provide more flexibility? 
    // for one, this means that mirror-based reflection facilities 
    // might have multiple implementations 
    // in a paper linked above, Gilad Bracha muses over a runtime 
    // that loads things remotely over the network 
    // in our case we might have different mirrors for JVM and CLR 
    // well, anyways 

    // the canonical (and the only one now) implementation of the mirror API 
    // is Java-based reflection that uses out of the box classloaders 
    // here's its root: https://github.com/scalamacros/kepler/blob/9f71e9f114c10b52350c6c4ec757159f06e55daa/src/reflect/scala/reflect/api/Mirrors.scala#L178 
    // yeah, right, I've just linked a source file from trunk 
    // we'll have Scaladocs for that soon, but for now take a look 
    // this file is interfaces-only and is heavy on comments 

    // to start with Java-based reflection implementation you need a classloader 
    // let's grab one and instantiate the root mirror 
    // btw, the same effect could be achieved by writing 
    // `scala.reflect.runtime.currentMirror` 
    val rootMirror = universe.runtimeMirror(getClass.getClassLoader) 

    // now when we've finally entered the reflective world 
    // we can get the stuff done 
    // first we obtain a ClassSymbol that corresponds to the current instance 
    // (ClassSymbols are to Scala the same as Classes are to Java) 
    var classSymbol = rootMirror.classSymbol(getClass) 

    // having a Scala reflection entity 
    // we can obtain its reflection using the rootMirror 
    val classMirror = rootMirror.reflectClass(classSymbol) 

    // now we just traverse the conceptual hierarchy of mirrors 
    // that closely follows the hierarchy of Scala reflection concepts 
    // for example, a ClassMirror has a companion ModuleMirror and vice versa 
    val moduleMirror = classMirror.companion.get 

    // finally, we've arrived at our destination 
    moduleMirror.instance.asInstanceOf[MetaBase] 
    } 
} 

trait MetaBase { 
    // stuff 
} 

// --- 

class Foo extends Base 

object Foo extends MetaBase 

object Test extends App { 
    assert(new Foo().companion == Foo) 
} 

업데이트. Daniel Sobral의 우수한 게시물 인 http://dcsobral.blogspot.ch/2012/07/json-serialization-with-reflection-in.html도 참조하십시오.

+0

[이 질문을보십시오] (http://stackoverflow.com/q/10893712/53013) 물론 거기에는 괜찮은 대답이 있지만. –

+0

마지막으로 scala 2.10.0에서 작동하도록이 코드를 업데이트하는 방법은 무엇입니까? –

+3

https://gist.github.com/xeno-by/4985929 –

5

나는 Eugene의 마지막 코멘트를 보지 못했고 이것을 생각해 냈습니다. 그것은 scala 2.10에서 작동합니다.

trait ReflectionSugars{ 
    import scala.reflect.runtime.{universe => ru} 
    private lazy val universeMirror = ru.runtimeMirror(getClass.getClassLoader) 

    def companionOf[T](implicit tt: ru.TypeTag[T]) = { 
    val companionMirror = universeMirror.reflectModule(ru.typeOf[T].typeSymbol.companionSymbol.asModule) 
    companionMirror.instance 
    } 

} 

trait X extends ReflectionSugars{ 
    def companion = companionOf[X] 
} 

https://gist.github.com/piotrga/5928581

나는 희망이 도움이!

관련 문제