Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
scabug opened this issue Sep 10, 2012 · 4 comments
Closed

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

scabug opened this issue Sep 10, 2012 · 4 comments
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Sep 10, 2012

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 #32, because scalac's nonsupport of creating runtime-visible annotations doesn't get involved here (javac was used for that).

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 10, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6356?orig=1
Reporter: @soc
Affected Versions: 2.10.0-M7
See #32

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 10, 2012

@soc said:
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 ...

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 11, 2012

@xeno-by said:
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

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 16, 2012

@xeno-by said:
Fixed in scala/scala@cf819b7

@scabug scabug closed this Sep 16, 2012
@scabug scabug added this to the 2.10.0-M7 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.