Scala Programming Language
  1. Scala Programming Language
  2. SI-6325

cannot reflect upon annotations on a trait member

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Not a Bug
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Reflection
    • Labels:
      None

      Description

      scala> class W extends scala.annotation.Annotation
      defined class W
      
      scala> trait A {
           | @W val a: Int
           | }
      defined trait A
      
      scala> typeOf[A].members.last
      res0: $r.intp.global.Symbol = value a
      
      scala> res0.getAnnotations
      res1: List[$r.intp.global.AnnotationInfo] = List()
      

        Activity

        Hide
        Eugene Burmako added a comment - - edited

        This issue exposes two important annotation-related gotchas.

        1) To have an annotation visible by Scala reflection, you need to extend either StaticAnnotation or ClassfileAnnotation. Just Annotation won't cut it.

        2) Annotation on a val is by default added to the underlying field of a val. Since val in question is abstract, there's no underlying field, so the annotation goes to waste. Writing @(W @scala.annotation.meta.getter) would attach the annotation to the getter as desired in this situation.

        All in all, knowing these gotchas, it's possible to make abstract annotated vals in traits work as desired. Hence I'm closing this as not a bug, but I'm planning to work on making #1 and #2 more obvious.
        #1: http://groups.google.com/group/scala-internals/browse_thread/thread/22fa25dca37db40d
        #2: https://issues.scala-lang.org/browse/SI-6375

        Discussion: http://groups.google.com/group/scala-internals/browse_thread/thread/6f0e90ae1a81b491

        Show
        Eugene Burmako added a comment - - edited This issue exposes two important annotation-related gotchas. 1) To have an annotation visible by Scala reflection, you need to extend either StaticAnnotation or ClassfileAnnotation. Just Annotation won't cut it. 2) Annotation on a val is by default added to the underlying field of a val. Since val in question is abstract, there's no underlying field, so the annotation goes to waste. Writing @(W @scala.annotation.meta.getter) would attach the annotation to the getter as desired in this situation. All in all, knowing these gotchas, it's possible to make abstract annotated vals in traits work as desired. Hence I'm closing this as not a bug, but I'm planning to work on making #1 and #2 more obvious. #1: http://groups.google.com/group/scala-internals/browse_thread/thread/22fa25dca37db40d #2: https://issues.scala-lang.org/browse/SI-6375 Discussion: http://groups.google.com/group/scala-internals/browse_thread/thread/6f0e90ae1a81b491

          People

          • Assignee:
            Eugene Burmako
            Reporter:
            Eugene Burmako
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development