This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 019e788 GROOVY-9817: Enhance Closure annotation value (as Closure
class) to Arrays (closes #1420)
019e788 is described below
commit 019e788fd2faf62f238242c453aa4f560abf7ee0
Author: Paul King <[email protected]>
AuthorDate: Tue Nov 17 17:57:16 2020 +1000
GROOVY-9817: Enhance Closure annotation value (as Closure class) to Arrays
(closes #1420)
---
.../org/codehaus/groovy/classgen/AsmClassGenerator.java | 8 ++++++--
.../annotations/closures/AnnotationClosureTest.groovy | 16 ++++++++++++++++
2 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
index 0fd7c0c..cd2f69d 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -2111,7 +2111,7 @@ public class AsmClassGenerator extends ClassGenerator {
arrayElementType = 1;
} else if (expr instanceof ConstantExpression) {
arrayElementType = 2;
- } else if (expr instanceof ClassExpression) {
+ } else if (expr instanceof ClassExpression || expr instanceof
ClosureExpression) {
arrayElementType = 3;
} else if (expr instanceof PropertyExpression) {
arrayElementType = 4;
@@ -2131,7 +2131,11 @@ public class AsmClassGenerator extends ClassGenerator {
av.visit(null, ((ConstantExpression) expr).getValue());
break;
case 3:
- av.visit(null,
Type.getType(BytecodeHelper.getTypeDescription(expr.getType())));
+ ClassNode type = expr.getType();
+ if (expr instanceof ClosureExpression) {
+ type =
controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) expr,
ACC_PUBLIC);
+ }
+ av.visit(null,
Type.getType(BytecodeHelper.getTypeDescription(type)));
break;
case 4:
PropertyExpression propExpr = (PropertyExpression) expr;
diff --git a/src/test/gls/annotations/closures/AnnotationClosureTest.groovy
b/src/test/gls/annotations/closures/AnnotationClosureTest.groovy
index 3b8d5d1..d54befd 100644
--- a/src/test/gls/annotations/closures/AnnotationClosureTest.groovy
+++ b/src/test/gls/annotations/closures/AnnotationClosureTest.groovy
@@ -112,6 +112,13 @@ class Foo {}
assert closure.call() == 3
}
+ void testWorksOnAnnotationWithArray() {
+ def closureClasses =
ClassWithAnnArrayClosure.getAnnotation(AnnWithClassArrayElement).elem()
+ assert closureClasses?.size() == 2
+ assert closureClasses[0].newInstance(null, null)() == 3
+ assert closureClasses[1].newInstance(null, null)() == 5
+ }
+
void testMayContainGString() {
def closureClass =
ClosureWithGString.getAnnotation(AnnWithClassElement).elem()
def closure = closureClass.newInstance(null, null)
@@ -170,10 +177,19 @@ class Foo {}
}
@Retention(RetentionPolicy.RUNTIME)
+@interface AnnWithClassArrayElement {
+ Class[] elem()
+}
+
+@Retention(RetentionPolicy.RUNTIME)
@interface AnnWithStringElement {
String elem()
}
+@AnnWithClassArrayElement(elem = [{ 1 + 2 }, { 2 + 3 }])
+class ClassWithAnnArrayClosure {
+}
+
@AnnWithClassElement(elem = { 1 + 2 })
class ClassWithAnnClosure {
@AnnWithClassElement(elem = { 1 + 2 })