You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2018/12/05 11:20:19 UTC

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

Repository: groovy
Updated Branches:
  refs/heads/master ea0418c72 -> e28949291


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/e2894929
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e2894929
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e2894929

Branch: refs/heads/master
Commit: e2894929126d76e0d939c080bb9c942543d1f270
Parents: ea0418c
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 20:51:46 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/e2894929/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/e2894929/src/test/gls/annotations/AnnotationTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/gls/annotations/AnnotationTest.groovy b/src/test/gls/annotations/AnnotationTest.groovy
index c79b290..c881274 100644
--- a/src/test/gls/annotations/AnnotationTest.groovy
+++ b/src/test/gls/annotations/AnnotationTest.groovy
@@ -847,6 +847,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
+            }
+        '''
+    }
+
     void testAnnotationWithRepeatableSupportedPrecompiledJava() {
         assertScript '''
             import java.lang.annotation.*