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.


---

Reply via email to