Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Duplicate
-
Affects Version/s: 1.5.6
-
Fix Version/s: None
-
Component/s: ast builder
-
Labels:None
-
Number of attachments :
Description
On annotation attributes, when assigning a constant with a "." in its name (as is the case when the constant is defined in a different class than the one being compiled), groovy generates org.codehaus.groovy.ast.expr.PropertyExpression.
See simple test case below.
— Tag.java
package pkg; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD,ElementType.FIELD}) public @interface Tag { String value() default ""; }
— TagType.java
package pkg; import org.apache.log4j.Logger; public class TagType { private final static Logger theLogger = Logger.getLogger(TagType.class); public static final String TAG_1 = "tag_1"; public static final String TAG_2 = "tag_2"; }
— GroovyClassWithAnnotationsAndConstants.groovy
package pkg; import pkg.Tag; import pkg.TagType class GroovyClassWithAnnotationsAndConstants { int myIntField; @Tag( value = TagType.TAG_1) // this will not compile and generate org.codehaus.groovy.ast.expr.PropertyExpression //@Tag( value = "tag_1") //the commented version, which uses a string literal does compile public int getIntField() { return myIntField; } public void setIntField(int value) { myIntField = value; } }
Issue Links
- is duplicated by
-
GROOVY-3278
Using referenced String constant as value of Annotation causes compile error
-
Activity
Guillaume Laforge
made changes -
| Field | Original Value | New Value |
|---|---|---|
| Description |
On annotation attributes, when assigning a constant with a "." in its name (as is the case when the constant is defined in a different class than the one being compiled), groovy generates org.codehaus.groovy.ast.expr.PropertyExpression. See simple test case below. --- Tag.java package pkg; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD,ElementType.FIELD}) public @interface Tag { String value() default ""; } --- TagType.java package pkg; import org.apache.log4j.Logger; public class TagType { private final static Logger theLogger = Logger.getLogger(TagType.class); public static final String TAG_1 = "tag_1"; public static final String TAG_2 = "tag_2"; } --- GroovyClassWithAnnotationsAndConstants.groovy package pkg; import pkg.Tag; import pkg.TagType class GroovyClassWithAnnotationsAndConstants { int myIntField; @Tag( value = TagType.TAG_1) // this will not compile and generate org.codehaus.groovy.ast.expr.PropertyExpression //@Tag( value = "tag_1") //the commented version, which uses a string literal does compile public int getIntField() { return myIntField; } public void setIntField(int value) { myIntField = value; } } |
On annotation attributes, when assigning a constant with a "." in its name (as is the case when the constant is defined in a different class than the one being compiled), groovy generates org.codehaus.groovy.ast.expr.PropertyExpression. See simple test case below. --- Tag.java {code} package pkg; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD,ElementType.FIELD}) public @interface Tag { String value() default ""; } {code} --- TagType.java {code} package pkg; import org.apache.log4j.Logger; public class TagType { private final static Logger theLogger = Logger.getLogger(TagType.class); public static final String TAG_1 = "tag_1"; public static final String TAG_2 = "tag_2"; } {code} --- GroovyClassWithAnnotationsAndConstants.groovy {code} package pkg; import pkg.Tag; import pkg.TagType class GroovyClassWithAnnotationsAndConstants { int myIntField; @Tag( value = TagType.TAG_1) // this will not compile and generate org.codehaus.groovy.ast.expr.PropertyExpression //@Tag( value = "tag_1") //the commented version, which uses a string literal does compile public int getIntField() { return myIntField; } public void setIntField(int value) { myIntField = value; } } {code} |
Paul King
made changes -
| Link | This issue is duplicated by GROOVY-3278 [ GROOVY-3278 ] |
Paul King
made changes -
| Status | Open [ 1 ] | Resolved [ 5 ] |
| Resolution | Duplicate [ 3 ] |
Paul King
made changes -
| Status | Resolved [ 5 ] | Closed [ 6 ] |
Additional test we could add to AnnotationTest and improve:
void testUsingClassStaticPublicFieldsAsAnnotationParameterValue() { assertScript """ import java.lang.annotation.* @Retention(RetentionPolicy.RUNTIME) @Target([ElementType.METHOD, ElementType.FIELD]) @interface Tag { String value() default "" String[] array() default [] } class TagType { public static final String TAG_ONE = "1" public static final String[] TAG_TWO = ["2a", "2b"] } class GroovyClassWithAnnotationsAndConstants { @Tag(TagType.TAG_ONE) private String annotatedField @Tag(value = TagType.TAG_ONE) void annotatedMethod() { } @Tag(array = TagType.TAG_TWO) String annotatedProperty @Tag(array = TagType.TAG_TWO) int otherAnnotatedMethod(int i) { i } @Tag(array = TagType.TAG_ONE) String helloAnnotatedMethod() { "hello" } @Tag(array = [TagType.TAG_ONE]) String hiAnnotatedMethod() { "hi" } @Tag(array = [TagType.TAG_ONE, TagType.TAG_ONE]) String byeAnnotatedMethod() { "bye" } } assertEquals TagType.TAG_ONE, GroovyClassWithAnnotationsAndConstants.class.getDeclaredField('annotatedField').annotations[0].value() // more asserts here """ }