카테고리 없음

[스칼라] 스칼라 리플렉션 : 리플렉션을 통해 인수가없는 생성자를 찾는 방법 (생성자가 여러 개인 경우)?

행복을전해요 2021. 2. 5. 01:21

다음과 같은 것 :

scala> class X(i: Int) { def this() = this(1) }
defined class X

scala> typeOf[X].declarations.filter { s => s.isMethod && {
     | val m = s.asMethod
          | m.isConstructor && m.paramss.flatten.isEmpty }}
          res2: Iterable[reflect.runtime.universe.Symbol] = SynchronizedOps(constructor X)
          

유형과는 조금 다릅니다.

scala> res5 filter (_ match { case MethodType(ps, t) if ps.isEmpty => true case _ => false })
res7: Iterable[reflect.runtime.universe.Type] = List(()X)


출처
https://stackoverflow.com/questions/22050094