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

Wrong annotation visibility in class files #5948

Closed
scabug opened this issue Jun 19, 2012 · 4 comments
Closed

Wrong annotation visibility in class files #5948

scabug opened this issue Jun 19, 2012 · 4 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jun 19, 2012

I am trying to apply some annotations, defined by Java code, to Scala classes and methods. The annotations have @retention(RetentionPolicy.CLASS) as they are defined in Java. When applied to Java code, they show up as RuntimeInvisibleAnnotations in the class file generated by javac. However, when applied to Scala code, scalac generates them as RuntimeVisibleAnnotations. This semantic change breaks the framework I'm using.

Here is a minimal test case.

/* file MyAnnotation.java */
import java.lang.annotation.Retention;
@retention(java.lang.annotation.RetentionPolicy.CLASS)
@interface MyAnnotation {
public String blah();
}

/* file UsesAnnotation.scala */
@MyAnnotation(blah = "blah")
class UsesAnnotation {}

$ javac -version
javac 1.7.0_03
$ javac MyAnnotation.java
$ scalac -version
Scala compiler version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL
$ scalac UsesAnnotation.java
$ javap -verbose UsesAnnotation.class | grep -i visible
RuntimeVisibleAnnotations:
#17 = Utf8 RuntimeVisibleAnnotations

Whereas if I write a class JUsesAnnotation:

/* file JUsesAnnotation.java */
@MyAnnotation(blah = "blah")
class JUsesAnnotation {}

$ javac JUsesAnnotation.java
$ javap -verbose JUsesAnnotation.class | grep -i visible
RuntimeInvisibleAnnotations:
#10 = Utf8 RuntimeInvisibleAnnotations

I've attached the .java, .scala and .class files, and also put them here:
http://www.inf.usi.ch/postdoc/kells/private/scala-annotations-mintest.tar.gz.
If there is a fix or workaround, it would be much appreciated! Let me know if I can supply any more details.

@scabug
Copy link
Author

scabug commented Jun 19, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5948?orig=1
Reporter: Stephen Kell (stephenrkell)
Affected Versions: 2.9.2
See #4788, #32
Attachments:

@scabug
Copy link
Author

scabug commented Jun 20, 2012

@lrytz said:
this is a known limitation - it "simply" has not been implemented, scalac emits all java annotations as RuntimeVisibleAnnotations.

@scabug
Copy link
Author

scabug commented Jun 21, 2012

Stephen Kell (stephenrkell) said:
Thanks for the confirmation. In the meantime I have worked around my problem by hacking the framework I'm using so that it doesn't care about attribute visibility. If I continue in this direction I'll have a go at patching the compiler to do the right thing (though this is looking unlikely for the moment).

@scabug
Copy link
Author

scabug commented Oct 8, 2014

@lrytz said:
scala/scala#4026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants