Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7975

Use of static final field in an annotation element causes compile errors

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.7
    • Fix Version/s: 3.0.0-alpha-4, 2.5.4
    • Component/s: Compiler
    • Labels:
      None

      Description

      Using a class constant (static final field) in an annotation causes compile errors. This works in Java and the fix is pretty small.

      Ex:

      class C {
          public static final String VALUE = 'rawtypes'
          @SuppressWarnings(VALUE)
          def method() {
          }
      }
      

      This is a bit contrived to be concise. But we have examples in our code where Callable impls are tagged with a name, which is defined as a static constant on each class.

      The fix appears to be pretty minor. In ResolveVisitor.transformInlineConstants, a case for VariableExpression does the trick for me.

          } else if (exp instanceof VariableExpression) {
              VariableExpression ve = (VariableExpression) exp;
              if (ve.getAccessedVariable() instanceof FieldNode) {
                  FieldNode fn = (FieldNode) ve.getAccessedVariable();
                  if (!fn.isEnum() && fn.isStatic() && fn.isFinal() &&
                          fn.getInitialValueExpression() instanceof ConstantExpression) {
                      return fn.getInitialValueExpression();
                  }
              }
              ....
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                emilles Eric Milles
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: