This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 015b3bc  GROOVY-9817: Enhance Closure annotation value (as Closure 
class) to Arrays (closes #1420)
015b3bc is described below

commit 015b3bc60fc956221af9ee868f44561a64ecb76b
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 b78d1ac..d642b2c 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -2059,7 +2059,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;
@@ -2079,7 +2079,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 })

Reply via email to