[ 
https://issues.apache.org/jira/browse/GROOVY-7975?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16674627#comment-16674627
 ] 

ASF GitHub Bot commented on GROOVY-7975:
----------------------------------------

Github user paulk-asert commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/819#discussion_r230627859
  
    --- Diff: src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java ---
    @@ -18,20 +18,205 @@
      */
     package org.apache.groovy.ast.tools;
     
    +import org.codehaus.groovy.ast.ClassHelper;
     import org.codehaus.groovy.ast.ClassNode;
     import org.codehaus.groovy.ast.FieldNode;
    +import org.codehaus.groovy.ast.expr.BinaryExpression;
     import org.codehaus.groovy.ast.expr.ClassExpression;
     import org.codehaus.groovy.ast.expr.ConstantExpression;
     import org.codehaus.groovy.ast.expr.Expression;
     import org.codehaus.groovy.ast.expr.ListExpression;
     import org.codehaus.groovy.ast.expr.PropertyExpression;
    +import org.codehaus.groovy.ast.expr.VariableExpression;
    +
    +import java.lang.reflect.Field;
    +import java.lang.reflect.Modifier;
    +import java.util.ArrayList;
    +
    +import static org.codehaus.groovy.syntax.Types.DIVIDE;
    +import static org.codehaus.groovy.syntax.Types.MINUS;
    +import static org.codehaus.groovy.syntax.Types.MULTIPLY;
    +import static org.codehaus.groovy.syntax.Types.PLUS;
     
     public class ExpressionUtils {
    +    private static ArrayList<Integer> handledTypes = new 
ArrayList<Integer>();
    +
         private ExpressionUtils() {
     
         }
     
    -    // resolve constant-looking expressions statically (do here as gets 
transformed away later)
    +    static {
    +        handledTypes.add(PLUS);
    +        handledTypes.add(MINUS);
    +        handledTypes.add(MULTIPLY);
    +        handledTypes.add(DIVIDE);
    +    }
    +
    +    public static ConstantExpression 
transformBinaryConstantExpression(BinaryExpression be, ClassNode targetType) {
    +        if (isTypeOrArrayOfType(targetType, ClassHelper.STRING_TYPE, 
false)) {
    +            if (be.getOperation().getType() == PLUS) {
    +                Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
    +                Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
    +                if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
    +                    ConstantExpression newExp = new 
ConstantExpression((String) ((ConstantExpression) left).getValue() +
    +                            ((ConstantExpression) right).getValue());
    +                    newExp.setSourcePosition(be);
    +                    return newExp;
    +                }
    +            }
    +        } else if (isTypeOrArrayOfType(targetType, 
ClassHelper.Integer_TYPE, false) || isTypeOrArrayOfType(targetType, 
ClassHelper.int_TYPE, false)) {
    +            int type = be.getOperation().getType();
    +            if (handledTypes.contains(type)) {
    +                Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
    +                Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
    +                if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
    +                    Integer newVal = null;
    +                    switch(type) {
    +                        case PLUS:
    +                            newVal = (Integer) ((ConstantExpression) 
left).getValue() +
    +                                    (Integer) ((ConstantExpression) 
right).getValue();
    +                            break;
    +                        case MINUS:
    +                            newVal = (Integer) ((ConstantExpression) 
left).getValue() -
    +                                    (Integer) ((ConstantExpression) 
right).getValue();
    +                            break;
    +                        case MULTIPLY:
    +                            newVal = (Integer) ((ConstantExpression) 
left).getValue() *
    +                                    (Integer) ((ConstantExpression) 
right).getValue();
    +                            break;
    +                        case DIVIDE:
    +                            newVal = (Integer) ((ConstantExpression) 
left).getValue() /
    +                                    (Integer) ((ConstantExpression) 
right).getValue();
    +                            break;
    +                    }
    +                    if (newVal != null) {
    +                        ConstantExpression newExp = new 
ConstantExpression(newVal, true);
    +                        newExp.setSourcePosition(be);
    +                        return newExp;
    +                    }
    +                }
    +            }
    +        } else if (isTypeOrArrayOfType(targetType, 
ClassHelper.Double_TYPE, false) || isTypeOrArrayOfType(targetType, 
ClassHelper.double_TYPE, false)) {
    --- End diff --
    
    Already refactored - thanks for the suggestion.


> Use of static final field in an annotation element causes compile errors
> ------------------------------------------------------------------------
>
>                 Key: GROOVY-7975
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7975
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.7
>            Reporter: Eric Milles
>            Priority: Major
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> Using a class constant (static final field) in an annotation causes compile 
> errors.  This works in Java and the fix is pretty small.
> Ex:
> {code}
> class C {
>     public static final String VALUE = 'rawtypes'
>     @SuppressWarnings(VALUE)
>     def method() {
>     }
> }
> {code}
> 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.
> {code}
>     } 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();
>             }
>         }
>         ....
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to