Navigation Menu

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 with RetentionPolicy.CLASS are retained at runtime (scala 2.8.1) #4788

Closed
scabug opened this issue Jul 11, 2011 · 12 comments
Closed
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jul 11, 2011

If I annotate a class in scala, using a annotation defined in java with RetentionPolicy.CLASS, the annotation is retained at runtime.

Here's a typical example using Peter Kriens' annotations for OSGi:

@aQute.bnd.annotation.component.Component class MyComp

If I launch the REPL with my class in the classpath, but without the definition of the annotation, and I simply try to instantiate the class, I get the following error:

scala> val c = new MyComp
error: error while loading MyComp, Missing dependency 'class aQute.bnd.annotation.component.Component', required by ./MyComp.class
<console>:5: error: MyComp does not have a constructor
       val c = new MyComp

And if I use Peter Kriens' class dump utility, it shows the following information:

2: attribute                   RuntimeVisibleAnnotations(#16)
2: Number of annotations       2
2: type                        LaQute/bnd/annotation/component/Component;(#12)2: num_element_value_pairs     0
2: type                        Lscala/reflect/ScalaSignature;(#13)2: num_element_value_pairs     1

This is very problematic in an OSGi environment where the package should not even need to be imported.

@scabug
Copy link
Author

scabug commented Jul 11, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4788?orig=1
Reporter: Arjun Panday (joune)
Affected Versions: 2.8.1
See #5420

@scabug
Copy link
Author

scabug commented Jul 12, 2011

@paulp said:
This should be easy to fix, assigning to meeting looking for a victim.

@scabug
Copy link
Author

scabug commented May 12, 2012

@soc said:
This is related to (and could probably be the cause of) #5420.

@scabug
Copy link
Author

scabug commented Mar 15, 2013

@adriaanm said:
Un-assigning to foster work stealing, as announced in https://groups.google.com/forum/?fromgroups=#!topic/scala-internals/o8WG4plpNkw

@scabug
Copy link
Author

scabug commented Jul 10, 2013

@adriaanm said:
Unassigning and rescheduling to M6 as previous deadline was missed.

@scabug
Copy link
Author

scabug commented Jan 27, 2014

@soc said:
Funny coincidence ... I just fixed this and #5420 5 minutes ago. Is it ok if I create a PR on Thursday?

@scabug
Copy link
Author

scabug commented Jan 28, 2014

@adriaanm said:
Sure, doesn't seem like it cause regressions. Thanks!

@scabug
Copy link
Author

scabug commented Feb 4, 2014

@soc said:
scala/scala#3459

@scabug
Copy link
Author

scabug commented Oct 1, 2014

@soc said (edited on Oct 1, 2014 4:35:29 PM UTC):
Just tried to update this patch to finally get rid of this ticket: https://github.com/soc/scala/tree/SI-4788-new ... apart from the known issue of scalac thinking that classOf doesn't exist in Predef, the test suite now fails with over 400 failures + nondeterminism:

test/partest --failed
Selected 402 tests drawn from previously failed tests

# starting 60 tests in pos
ok  1 - pos/NoCyclicReference.scala             
ok  2 - pos/A.scala                             
ok  3 - pos/S1.scala                            
ok  4 - pos/FPTest.scala                        
ok  5 - pos/S3.scala                            
ok  6 - pos/List1.scala                         
ok  7 - pos/MailBox.scala                       
!!  8 - pos/inline-access-levels                  [compilation failed]
...

Many of the failures are related to annotated elements just "disappearing":

@foo val bar = 1
//Somewhere else:
println(bar)
// scalac: Hey dude, bar doesn't exist, amiright?!

But not all of the failures use annotations.

I'm running out of ideas how a fix to make scalac determine correctly whether a Java annotation should be visible at runtime can wreak such a havoc all over the place.

I'd be happy about any help in this regard.

@scabug
Copy link
Author

scabug commented Oct 8, 2014

@lrytz said:
scala/scala#4026

@scabug
Copy link
Author

scabug commented Dec 20, 2016

@lrytz said:
Need to re-open this.

In scala/scala#5606 we upgraded partest to a version that changes the mixed compilation mode. This caused a change in the retained annotations, which (probably) should not be the case.

I'll take a look.

@scabug
Copy link
Author

scabug commented Dec 21, 2016

@lrytz said:
The fix in scala/scala#4026 only works under separate compilation, because annotations are skipped by the Java parser. We have an open issue for this: #8928, so this one can be closed.

@Retention(value=SOURCE) // not parsed by the Java parser
@interface SAnnotation {}

Tests updated accordingly in scala/scala#5606.

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