-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11117 from Kordyjan/java-annotations
Support for parsing annotation arguments from java
- Loading branch information
Showing
8 changed files
with
287 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,82 @@ | ||
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(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface ShouldNotCrash { | ||
String value(); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface ShouldAlsoNotCrash { | ||
String value(); | ||
int[] ints(); | ||
} | ||
|
||
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,22 @@ | ||
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 | ||
) | ||
@ShouldNotCrash(false ? "A" + A.CONST : "B") | ||
@ShouldAlsoNotCrash(value = "C", ints = { 1, 2, 3, 5 - 1 }) | ||
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) |