Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X ef7c5d6b4 -> da6699b1a


GROOVY-8898: Annotation value cannot take inline constant from enum.


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/da6699b1
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/da6699b1
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/da6699b1

Branch: refs/heads/GROOVY_2_5_X
Commit: da6699b1ab1c6c0139f6d67c254a5686f48f0ca5
Parents: ef7c5d6
Author: Paul King <pa...@asert.com.au>
Authored: Wed Dec 5 20:51:46 2018 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Wed Dec 5 22:55:08 2018 +1000

----------------------------------------------------------------------
 .../groovy/ast/tools/ExpressionUtils.java       |  7 +++--
 src/test/gls/annotations/AnnotationTest.groovy  | 33 ++++++++++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/da6699b1/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java 
b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
index 8fab0db..e1399ec 100644
--- a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
@@ -293,7 +293,7 @@ public class ExpressionUtils {
     }
 
     /**
-     * The attribute values of annotations must be primitive or String 
constants.
+     * The attribute values of annotations must be primitive, String or Enum 
constants.
      * In various places, such constants can be seen during type resolution 
but won't be
      * readily accessible in later phases, e.g. they might be embedded into 
constructor code.
      * This method transforms constants that would appear in annotations early 
so they aren't lost.
@@ -309,8 +309,9 @@ public class ExpressionUtils {
             if (pe.getObjectExpression() instanceof ClassExpression) {
                 ClassExpression ce = (ClassExpression) 
pe.getObjectExpression();
                 ClassNode type = ce.getType();
-                if (type.isEnum()) return exp;
-                Expression constant = 
findConstant(ClassNodeUtils.getField(type, pe.getPropertyAsString()));
+                FieldNode field = ClassNodeUtils.getField(type, 
pe.getPropertyAsString());
+                if (type.isEnum() && field != null && field.isEnum()) return 
exp;
+                Expression constant = findConstant(field);
                 if (constant != null) return constant;
             }
         } else if (exp instanceof BinaryExpression) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/da6699b1/src/test/gls/annotations/AnnotationTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/gls/annotations/AnnotationTest.groovy 
b/src/test/gls/annotations/AnnotationTest.groovy
index 74282c3..64f294e 100644
--- a/src/test/gls/annotations/AnnotationTest.groovy
+++ b/src/test/gls/annotations/AnnotationTest.groovy
@@ -849,6 +849,39 @@ class AnnotationTest extends CompilableTestSupport {
         '''
     }
 
+    void testAnnotationAttributeConstantFromEnumConstantField() {
+        // GROOVY-8898
+        assertScript '''
+            import java.lang.annotation.*
+
+            @Retention(RetentionPolicy.RUNTIME)
+            @Target(ElementType.TYPE)
+            @interface MyAnnotation {
+                String[] groups() default []
+                MyEnum alt() default MyEnum.ALT1
+            }
+
+            class Base {
+                static final String CONST = 'bar'
+                def groups() { getClass().annotations[0].groups() }
+                def alt() { getClass().annotations[0].alt() }
+            }
+
+            enum MyEnum {
+                ALT1, ALT2;
+                public static final String CONST = 'baz'
+            }
+
+            @MyAnnotation(groups = ['foo', Base.CONST, MyEnum.CONST], alt = 
MyEnum.ALT2)
+            class Child extends Base {}
+
+            new Child().with {
+                assert groups() == ['foo', 'bar', 'baz']
+                assert alt() == MyEnum.ALT2
+            }
+        '''
+    }
+
     //Parametrized tests in Spock would allow to make it much more readable
     private static String codeWithMetaAnnotationWithTarget(String 
targetElementTypeName) {
         """

Reply via email to