groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-7975) Use of static final field in an annotation element causes compile errors
Date Sun, 04 Nov 2018 13:43:00 GMT

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

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

Github user danielsun1106 commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/819#discussion_r230589376
  
    --- 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 --
    
    looks like some template code, which is duplicated now. It's better to refactor it IMO


> 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)

Mime
View raw message