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

Java annotations don't show up using Scala reflection's getAnnotations

    Details

      Description

      Consider the following Java files:

      FooAnnotation.java
      import java.lang.annotation.Target;
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
      public @interface FooAnnotation {}
      
      Foo.java
      @FooAnnotation
      public class Foo {}
      

      Compilation with javac succeeds as expected:

      javac *.java

      Starting the Scala REPL with those files on the classpath...

      scala -cp .

      Java reflection works:

      scala> val javaFooClass = classOf[Foo]
      javaFooClass: Class[Foo] = class Foo
      
      scala> val javaFooClassAnnotations = javaFooClass.getAnnotations
      javaFooClassAnnotations: Array[java.lang.annotation.Annotation] = Array(@FooAnnotation())
      
      val javaFooAnnotationAnnotations = javaFooClassAnnotations(0).annotationType.getAnnotations
      javaFooAnnotationAnnotations: Array[java.lang.annotation.Annotation] = Array(@java.lang.annotation.Retention(value=RUNTIME), @java.lang.annotation.Target(value=[FIELD, METHOD, TYPE]))
      

      But Scala reflection doesn't see anything:

      scala> import reflect.runtime.universe.typeOf
      import reflect.runtime.universe.typeOf
      
      scala> val scalaFooType = typeOf[Foo]
      scalaFooType: reflect.runtime.universe.Type = Foo
      
      scala> val scalaFooTypeAnnotations = scalaFooType.typeSymbol.getAnnotations
      scalaFooTypeAnnotations: List[reflect.runtime.universe.AnnotationInfo] = List()
      

      The annotations should show up in Scala reflection, because they are clearly present at runtime, as shown with Java reflection.

      The problem is probably only slightly related to SI-32, because scalac's nonsupport of creating runtime-visible annotations doesn't get involved here (javac was used for that).

        Issue Links

          Activity

          Hide
          Simon Ochsenreither added a comment -

          The cause of the issue is probably scala.tools.nsc.javac.JavaParsers around line 339.

          The whole annotation handling there seems to be a large no-op ...

          Show
          Simon Ochsenreither added a comment - The cause of the issue is probably scala.tools.nsc.javac.JavaParsers around line 339. The whole annotation handling there seems to be a large no-op ...
          Hide
          Eugene Burmako added a comment -

          Probably the problem is that we understand only Scala annotations, i.e. the ones pickled by scalac. I'll discuss this today on a Scala meeting

          Show
          Eugene Burmako added a comment - Probably the problem is that we understand only Scala annotations, i.e. the ones pickled by scalac. I'll discuss this today on a Scala meeting
          Show
          Eugene Burmako added a comment - Fixed in https://github.com/scala/scala/commit/cf819b7756de917912807322259ebb993a52ce57

            People

            • Assignee:
              Eugene Burmako
              Reporter:
              Simon Ochsenreither
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development