James Daugherty created GROOVY-11793:
----------------------------------------

             Summary: Cannot Reference Previously Compiled Constants from 
Annotations
                 Key: GROOVY-11793
                 URL: https://issues.apache.org/jira/browse/GROOVY-11793
             Project: Groovy
          Issue Type: Task
          Components: Compiler
    Affects Versions: 4.0.28
         Environment: Mac OS (latest), Java 17, Groovy 4.0.28
            Reporter: James Daugherty


GROOVY-3278 added support in Groovy 3 to reference String constants defined on 
other classes in annotations.  In Groovy 4, this appears to be broken when 
split up across different compilation units. 

 

For example, the following will fail: 

 
{code:java}
import java.lang.annotation.Documented
import java.lang.annotation.ElementType
import java.lang.annotation.Inherited
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
import groovy.transform.CompileStatic

@CompileStatic
class OtherExample {
     static final String 
MY_VALUE=com.sun.org.apache.xml.internal.security.c14n.Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS
}

@CompileStatic
@Target([ElementType.METHOD, ElementType.TYPE])
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@interface MyAnnotation {
    String value() default ""
}

@CompileStatic
class TheProblem {
    String foo
    
    @MyAnnotation(OtherExample.MY_VALUE)
    def myTestMethod() {
       'yello'
    }
}

new TheProblem() {code}
 

 

with this error: 
{code:java}
Expected 'OtherExample.getMY_VALUE()' to be an inline constant of type 
java.lang.String in @MyAnnotation{code}
{code:java}
Attribute 'value' should have type 'java.lang.String'; but found type 
'java.lang.Object' in @MyAnnotation            {code}

But if the constant is defined on a class in the same script, it will succeed:


{code:java}
import java.lang.annotation.Documented
import java.lang.annotation.ElementType
import java.lang.annotation.Inherited
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
import groovy.transform.CompileStatic@CompileStatic
class AnExample {
    static final String CONST = 'foo'
}

@CompileStatic
class OtherExample {
     static final String MY_VALUE=AnExample.CONST
}

@CompileStatic
@Target([ElementType.METHOD, ElementType.TYPE])
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@interface MyAnnotation {
    String value() default ""
}

@CompileStatic
class TheProblem {
    String foo
    
    @MyAnnotation(OtherExample.MY_VALUE)
    def myTestMethod() {
       'yello'
    }
}

new TheProblem() {code}
Can the Groovy 3 behavior be restored so that constants can be reassigned & 
then referenced in annotations?

 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to