forked from scala/scala3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix scala#10788: Parsing arguments for java annotations
* A port of fixes from scala/scala#8781 with improvements. * Some of improvements come from scala/scala#8982. * There are small changes to typer to allow for single elements of any type T be used where array of T is expected inside of java annotations arguments as it is correct in java and is used in 2 files in scala standard library.
- Loading branch information
Showing
8 changed files
with
258 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class annots.A | ||
class annots.A | ||
SOME STRING | ||
VALUE OF CONST | ||
false | ||
13.7 | ||
VALUE | ||
List(a, b, c) | ||
List() | ||
List(SINGLE) | ||
List(ABC) | ||
|
||
class annots.A | ||
class annots.A | ||
SOME STRING | ||
VALUE OF CONST | ||
false | ||
13.7 | ||
VALUE | ||
List(a, b, c) | ||
List() | ||
List(SINGLE) | ||
List(ABC) |
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,70 @@ | ||
package annots; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithClass { | ||
Class<?> arg(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithClassDefaultName { | ||
Class<?> value(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithString { | ||
String arg(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithReference { | ||
String arg(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithBoolean { | ||
boolean arg(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithFloat { | ||
float arg(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithNested { | ||
Nested arg(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface Nested { | ||
String value(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithArray { | ||
String[] value(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithEmptyArray { | ||
String[] value(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithSingleElement { | ||
String[] value(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface WithMultipleArgs { | ||
int[] ints(); | ||
float floatVal(); | ||
Nested[] annots(); | ||
Class<?> clazz(); | ||
String[] value(); | ||
} | ||
class A { | ||
static final String CONST = "VALUE OF CONST"; | ||
} |
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,5 @@ | ||
object Test: | ||
def main(args: Array[String]): Unit = | ||
annots.runTest(classOf[annots.Use_0]) | ||
println() | ||
annots.runTest(classOf[annots.Use_1]) |
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,20 @@ | ||
package annots; | ||
|
||
@WithClass(arg = A.class) | ||
@WithClassDefaultName(A.class) | ||
@WithString(arg = "SOME STRING") | ||
@WithReference(arg = A.CONST) | ||
@WithBoolean(arg = false) | ||
@WithFloat(arg = 13.7f) | ||
@WithNested(arg = @Nested("VALUE")) | ||
@WithArray({ "a", "b", "c" }) | ||
@WithEmptyArray({}) | ||
@WithSingleElement("SINGLE") | ||
@WithMultipleArgs( | ||
ints = {1, 2, 3, }, | ||
annots = { @Nested("Value"), @Nested(A.CONST) }, | ||
floatVal = 13.7f, | ||
value = "ABC", | ||
clazz = A.class | ||
) | ||
public class Use_0 {} |
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,21 @@ | ||
package annots; | ||
|
||
@WithClass(arg = A.class) | ||
@WithClassDefaultName(A.class) | ||
@WithString(arg = "SOME STRING") | ||
@WithReference(arg = A.CONST) | ||
@WithBoolean(arg = false) | ||
@WithFloat(arg = 13.7f) | ||
@WithNested(arg = @Nested("VALUE")) | ||
@WithArray({"a", "b", "c"}) | ||
@WithEmptyArray({}) | ||
@WithSingleElement("SINGLE") | ||
@WithMultipleArgs( | ||
ints = { 1, 2, 3, }, | ||
annots = { @Nested("Value"), | ||
@Nested(A.CONST) }, | ||
floatVal = 13.7f, | ||
value = "ABC", | ||
clazz = A.class | ||
) | ||
public class Use_1 {} |
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,17 @@ | ||
package annots | ||
|
||
def runTest(cls: Class[_]): Unit = | ||
val params = | ||
Option(cls.getAnnotation(classOf[WithClass])).map(_.arg) :: | ||
Option(cls.getAnnotation(classOf[WithClassDefaultName])).map(_.value) :: | ||
Option(cls.getAnnotation(classOf[WithString])).map(_.arg) :: | ||
Option(cls.getAnnotation(classOf[WithReference])).map(_.arg) :: | ||
Option(cls.getAnnotation(classOf[WithBoolean])).map(_.arg) :: | ||
Option(cls.getAnnotation(classOf[WithFloat])).map(_.arg) :: | ||
Option(cls.getAnnotation(classOf[WithNested])).map(_.arg.value) :: | ||
Option(cls.getAnnotation(classOf[WithArray])).map(_.value.toList) :: | ||
Option(cls.getAnnotation(classOf[WithEmptyArray])).map(_.value.toList) :: | ||
Option(cls.getAnnotation(classOf[WithSingleElement])).map(_.value.toList) :: | ||
Option(cls.getAnnotation(classOf[WithMultipleArgs])).map(_.value.toList) :: | ||
Nil | ||
params.flatten.foreach(println) |