Search in sources :

Example 81 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.

the class AstBuilder method createEnumConstantInitExpression.

private Expression createEnumConstantInitExpression(final ArgumentsContext ctx, final InnerClassNode anonymousInnerClassNode) {
    if (!asBoolean(ctx) && !asBoolean(anonymousInnerClassNode)) {
        return null;
    }
    TupleExpression argumentListExpression = (TupleExpression) this.visitArguments(ctx);
    List<Expression> expressions = argumentListExpression.getExpressions();
    if (expressions.size() == 1) {
        Expression expression = expressions.get(0);
        if (expression instanceof NamedArgumentListExpression) {
            // e.g. SOME_ENUM_CONSTANT(a: "1", b: "2")
            List<MapEntryExpression> mapEntryExpressionList = ((NamedArgumentListExpression) expression).getMapEntryExpressions();
            ListExpression listExpression = new ListExpression(mapEntryExpressionList.stream().map(e -> (Expression) e).collect(Collectors.toList()));
            if (asBoolean(anonymousInnerClassNode)) {
                listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
            }
            if (mapEntryExpressionList.size() > 1) {
                listExpression.setWrapped(true);
            }
            return configureAST(listExpression, ctx);
        }
        if (!asBoolean(anonymousInnerClassNode)) {
            if (expression instanceof ListExpression) {
                ListExpression listExpression = new ListExpression();
                listExpression.addExpression(expression);
                return configureAST(listExpression, ctx);
            }
            return expression;
        }
        ListExpression listExpression = new ListExpression();
        if (expression instanceof ListExpression) {
            ((ListExpression) expression).getExpressions().forEach(listExpression::addExpression);
        } else {
            listExpression.addExpression(expression);
        }
        listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
        return configureAST(listExpression, ctx);
    }
    ListExpression listExpression = new ListExpression(expressions);
    if (asBoolean(anonymousInnerClassNode)) {
        listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
    }
    if (asBoolean(ctx)) {
        listExpression.setWrapped(true);
    }
    return asBoolean(ctx) ? configureAST(listExpression, ctx) : configureAST(listExpression, anonymousInnerClassNode);
}
Also used : MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodReferenceExpression(org.codehaus.groovy.ast.expr.MethodReferenceExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) Expression(org.codehaus.groovy.ast.expr.Expression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodPointerExpression(org.codehaus.groovy.ast.expr.MethodPointerExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression)

Example 82 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.

the class ImmutablePropertyUtils method getKnownImmutables.

public static List<String> getKnownImmutables(final AbstractASTTransformation xform, final ClassNode cNode) {
    List<AnnotationNode> annotations = cNode.getAnnotations(ImmutablePropertyUtils.IMMUTABLE_OPTIONS_TYPE);
    AnnotationNode anno = annotations.isEmpty() ? null : annotations.get(0);
    final List<String> immutables = new ArrayList<String>();
    if (anno == null)
        return immutables;
    final Expression expression = anno.getMember(MEMBER_KNOWN_IMMUTABLES);
    if (expression == null)
        return immutables;
    if (!(expression instanceof ListExpression)) {
        xform.addError("Use the Groovy list notation [el1, el2] to specify known immutable property names via \"" + MEMBER_KNOWN_IMMUTABLES + "\"", anno);
        return immutables;
    }
    final ListExpression listExpression = (ListExpression) expression;
    for (Expression listItemExpression : listExpression.getExpressions()) {
        if (listItemExpression instanceof ConstantExpression) {
            immutables.add((String) ((ConstantExpression) listItemExpression).getValue());
        }
    }
    if (!xform.checkPropertyList(cNode, immutables, "knownImmutables", anno, "immutable class", false))
        return immutables;
    return immutables;
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayList(java.util.ArrayList)

Example 83 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.

the class ExpressionUtils method transformListOfConstants.

/**
 * Given a list of constants, transform each item in the list.
 *
 * @param origList the list to transform
 * @param attrType the target type
 * @return the transformed list or the original if nothing was changed
 */
public static Expression transformListOfConstants(final ListExpression origList, final ClassNode attrType) {
    ListExpression newList = new ListExpression();
    boolean changed = false;
    for (Expression e : origList.getExpressions()) {
        try {
            Expression transformed = transformInlineConstants(e, attrType);
            newList.addExpression(transformed);
            if (transformed != e)
                changed = true;
        } catch (Exception ignored) {
            newList.addExpression(e);
        }
    }
    if (changed) {
        newList.setSourcePosition(origList);
        return newList;
    }
    return origList;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression)

Example 84 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project gradle by gradle.

the class GradleResolveVisitor method transformBinaryExpression.

@Override
protected Expression transformBinaryExpression(BinaryExpression be) {
    Expression left = transform(be.getLeftExpression());
    int type = be.getOperation().getType();
    if ((type == Types.ASSIGNMENT_OPERATOR || type == Types.EQUAL) && left instanceof ClassExpression) {
        ClassExpression ce = (ClassExpression) left;
        String error = "you tried to assign a value to the class '" + ce.getType().getName() + "'";
        if (ce.getType().isScript()) {
            error += ". Do you have a script with this name?";
        }
        addError(error, be.getLeftExpression());
        return be;
    }
    if (left instanceof ClassExpression && isLeftSquareBracket(type)) {
        if (be.getRightExpression() instanceof ListExpression) {
            ListExpression list = (ListExpression) be.getRightExpression();
            if (list.getExpressions().isEmpty()) {
                // we have C[] if the list is empty -> should be an array then!
                final ClassExpression ce = new ClassExpression(left.getType().makeArray());
                ce.setSourcePosition(be);
                return ce;
            } else {
                // may be we have C[k1:v1, k2:v2] -> should become (C)([k1:v1, k2:v2])
                boolean map = true;
                for (Expression expression : list.getExpressions()) {
                    if (!(expression instanceof MapEntryExpression)) {
                        map = false;
                        break;
                    }
                }
                if (map) {
                    final MapExpression me = new MapExpression();
                    for (Expression expression : list.getExpressions()) {
                        me.addMapEntryExpression((MapEntryExpression) transform(expression));
                    }
                    me.setSourcePosition(list);
                    final CastExpression ce = new CastExpression(left.getType(), me);
                    ce.setSourcePosition(be);
                    return ce;
                }
            }
        } else if (be.getRightExpression() instanceof SpreadMapExpression) {
            // we have C[*:map] -> should become (C) map
            SpreadMapExpression mapExpression = (SpreadMapExpression) be.getRightExpression();
            Expression right = transform(mapExpression.getExpression());
            Expression ce = new CastExpression(left.getType(), right);
            ce.setSourcePosition(be);
            return ce;
        }
        if (be.getRightExpression() instanceof MapEntryExpression) {
            // may be we have C[k1:v1] -> should become (C)([k1:v1])
            final MapExpression me = new MapExpression();
            me.addMapEntryExpression((MapEntryExpression) transform(be.getRightExpression()));
            me.setSourcePosition(be.getRightExpression());
            final CastExpression ce = new CastExpression(left.getType(), me);
            ce.setSourcePosition(be);
            return ce;
        }
    }
    Expression right = transform(be.getRightExpression());
    be.setLeftExpression(left);
    be.setRightExpression(right);
    return be;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression)

Example 85 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy-core by groovy.

the class ImmutableASTTransformation method getKnownImmutableClasses.

private List<String> getKnownImmutableClasses(AnnotationNode node) {
    final ArrayList<String> immutableClasses = new ArrayList<String>();
    final Expression expression = node.getMember(MEMBER_KNOWN_IMMUTABLE_CLASSES);
    if (expression == null)
        return immutableClasses;
    if (!(expression instanceof ListExpression)) {
        addError("Use the Groovy list notation [el1, el2] to specify known immutable classes via \"" + MEMBER_KNOWN_IMMUTABLE_CLASSES + "\"", node);
        return immutableClasses;
    }
    final ListExpression listExpression = (ListExpression) expression;
    for (Expression listItemExpression : listExpression.getExpressions()) {
        if (listItemExpression instanceof ClassExpression) {
            immutableClasses.add(listItemExpression.getType().getName());
        }
    }
    return immutableClasses;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayList(java.util.ArrayList) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ToStringASTTransformation.createToString(org.codehaus.groovy.transform.ToStringASTTransformation.createToString)

Aggregations

ListExpression (org.codehaus.groovy.ast.expr.ListExpression)103 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)77 Expression (org.codehaus.groovy.ast.expr.Expression)77 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)70 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)53 ClassNode (org.codehaus.groovy.ast.ClassNode)49 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)43 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)42 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)38 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)38 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)37 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)32 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)32 ArrayList (java.util.ArrayList)31 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)30 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)28 AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)25 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)25 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)23 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)22