forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, `JavaParsers` and `ClassfileParser` would "fix-up" Java annotations to be non-abstract classes with `Annotation` and `StaticAnnotation` as parents. This caused all manner of strangeness and invalid classfiles (see linked tickets) and was surprising, to say the least. Now, the only special dispensation given to Java annotations is that they get a public, no-args class constructor, because that's what it takes to get `typedAnnotation` to be able to look up the type. This means that `new Foo`, where `Foo` is a Java annotation, still compiles (as it does right now, actually). Part of the reason for this commit is to provoke a discussion about how to fix that. Fixes scala/bug#8778. Fixes scala/bug#9400. Fixes scala/bug#9644.
- Loading branch information
Showing
19 changed files
with
182 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Test_1.scala:12: error: Java annotation Ann_0 is abstract; cannot be instantiated | ||
val a: Ann_0 = new Ann_0 // nok | ||
^ | ||
Test_1.scala:13: error: Java annotation Ann_0 is abstract; cannot be instantiated | ||
val b: Ann_0 = new Ann_0(Array()) // nok | ||
^ | ||
Test_1.scala:14: error: Java annotation Ann_1 is abstract; cannot be instantiated | ||
val c: Ann_1 = new Ann_1 // nok | ||
^ | ||
Test_1.scala:15: error: Java annotation Ann_1 is abstract; cannot be instantiated | ||
val d: Ann_1 = new Ann_1(Array()) // nok | ||
^ | ||
Test_1.scala:18: error: type mismatch; | ||
found : ann.Ann_0 | ||
required: scala.annotation.Annotation | ||
val e: Annotation = a // nok | ||
^ | ||
Test_1.scala:19: error: type mismatch; | ||
found : ann.Ann_1 | ||
required: scala.annotation.Annotation | ||
val f: Annotation = c // nok | ||
^ | ||
Test_1.scala:20: error: type mismatch; | ||
found : ann.Ann_0 | ||
required: scala.annotation.StaticAnnotation | ||
val g: StaticAnnotation = a // nok | ||
^ | ||
Test_1.scala:21: error: type mismatch; | ||
found : ann.Ann_1 | ||
required: scala.annotation.StaticAnnotation | ||
val h: StaticAnnotation = c // nok | ||
^ | ||
Test_1.scala:22: error: type mismatch; | ||
found : ann.Ann_0 | ||
required: scala.annotation.ConstantAnnotation | ||
val i: ConstantAnnotation = a // nok | ||
^ | ||
Test_1.scala:23: error: type mismatch; | ||
found : ann.Ann_1 | ||
required: scala.annotation.ConstantAnnotation | ||
val j: ConstantAnnotation = c // nok | ||
^ | ||
10 errors found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ann; | ||
|
||
public @interface Ann_0 { | ||
N[] value(); | ||
|
||
public @interface N {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ann; | ||
|
||
public @interface Ann_1 { | ||
N[] value(); | ||
|
||
public @interface N {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
object Test { | ||
import ann._ | ||
|
||
// ok | ||
@Ann_0(Array(new Ann_0.N, new Ann_0.N)) | ||
class A | ||
|
||
// ok | ||
@Ann_1(Array(new Ann_1.N, new Ann_1.N)) | ||
class B | ||
|
||
val a: Ann_0 = new Ann_0 // nok | ||
val b: Ann_0 = new Ann_0(Array()) // nok | ||
val c: Ann_1 = new Ann_1 // nok | ||
val d: Ann_1 = new Ann_1(Array()) // nok | ||
|
||
import scala.annotation._, java.lang.{annotation => jla} | ||
val e: Annotation = a // nok | ||
val f: Annotation = c // nok | ||
val g: StaticAnnotation = a // nok | ||
val h: StaticAnnotation = c // nok | ||
val i: ConstantAnnotation = a // nok | ||
val j: ConstantAnnotation = c // nok | ||
val k: jla.Annotation = a // ok | ||
val l: jla.Annotation = c // ok | ||
|
||
val m = new Ann_0 { val annotationType = classOf[Ann_0] } // ok | ||
val n = new Ann_1 { val annotationType = classOf[Ann_1] } // ok | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
class Deprecation extends Deprecated { | ||
final val annotationType = classOf[Deprecated] | ||
} | ||
|
||
class Suppression extends SuppressWarnings { | ||
final val annotationType = classOf[SuppressWarnings] | ||
|
||
def value = Array("unchecked") | ||
} | ||
|
||
class Retention(runtime: Boolean) extends java.lang.annotation.Retention { | ||
final val annotationType = classOf[Retention] | ||
|
||
def value = | ||
if (runtime) java.lang.annotation.RetentionPolicy.RUNTIME | ||
else java.lang.annotation.RetentionPolicy.SOURCE | ||
} | ||
|
||
object Test extends App { | ||
new Deprecation | ||
new Suppression | ||
new Retention(true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import java.lang.annotation._ | ||
|
||
@Deprecated @Retention(RetentionPolicy.RUNTIME) class Foo | ||
|
||
object Test extends App { | ||
classOf[Foo].getAnnotation(classOf[Deprecated]) | ||
|
||
assert(classOf[Foo].getAnnotation(classOf[Retention]).value() == RetentionPolicy.RUNTIME) | ||
|
||
import reflect.runtime.universe._ | ||
|
||
val List(d, r) = symbolOf[Foo].annotations | ||
|
||
d.tree match { | ||
case Apply(Select(New(tpt), _), Nil) => | ||
assert (tpt.tpe.typeSymbol == symbolOf[Deprecated], tpt.tpe.typeSymbol) | ||
} | ||
|
||
val RetentionPolicy_RUNTIME = symbolOf[RetentionPolicy].companion.info.decl(TermName("RUNTIME")) | ||
r.tree match { | ||
case Apply(Select(New(tpt), _), List(NamedArg(Ident(TermName("value")), Literal(Constant(RetentionPolicy_RUNTIME))))) => | ||
assert (tpt.tpe.typeSymbol == symbolOf[Retention], tpt.tpe.typeSymbol) | ||
} | ||
|
||
} |