Search in sources :

Example 16 with MapExpression

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

the class ImmutableASTTransformation method createNoArgConstructor.

private static void createNoArgConstructor(ClassNode cNode) {
    Statement body = stmt(ctorX(ClassNode.THIS, args(new MapExpression())));
    doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body));
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode)

Example 17 with MapExpression

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

the class GradleResolveVisitor method transformBinaryExpression.

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 18 with MapExpression

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

the class GrabAnnotationTransformation method callGrabAsStaticInitIfNeeded.

private void callGrabAsStaticInitIfNeeded(ClassNode classNode, ClassNode grapeClassNode, List<Map<String, Object>> grabMapsInit, List<Map<String, Object>> grabExcludeMaps) {
    List<Statement> grabInitializers = new ArrayList<Statement>();
    MapExpression basicArgs = new MapExpression();
    if (autoDownload != null) {
        basicArgs.addMapEntryExpression(new ConstantExpression(AUTO_DOWNLOAD_SETTING), new ConstantExpression(autoDownload));
    }
    if (disableChecksums != null) {
        basicArgs.addMapEntryExpression(new ConstantExpression(DISABLE_CHECKSUMS_SETTING), new ConstantExpression(disableChecksums));
    }
    if (!grabExcludeMaps.isEmpty()) {
        ListExpression list = new ListExpression();
        for (Map<String, Object> map : grabExcludeMaps) {
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            MapExpression inner = new MapExpression();
            for (Map.Entry<String, Object> entry : entries) {
                inner.addMapEntryExpression(new ConstantExpression(entry.getKey()), new ConstantExpression(entry.getValue()));
            }
            list.addExpression(inner);
        }
        basicArgs.addMapEntryExpression(new ConstantExpression("excludes"), list);
    }
    List<Expression> argList = new ArrayList<Expression>();
    argList.add(basicArgs);
    if (grabMapsInit.size() == 0)
        return;
    for (Map<String, Object> grabMap : grabMapsInit) {
        // add Grape.grab(excludeArgs, [group:group, module:module, version:version, classifier:classifier])
        // or Grape.grab([group:group, module:module, version:version, classifier:classifier])
        MapExpression dependencyArg = new MapExpression();
        for (String s : GRAB_REQUIRED) {
            dependencyArg.addMapEntryExpression(new ConstantExpression(s), new ConstantExpression(grabMap.get(s)));
        }
        for (String s : GRAB_OPTIONAL) {
            if (grabMap.containsKey(s))
                dependencyArg.addMapEntryExpression(new ConstantExpression(s), new ConstantExpression(grabMap.get(s)));
        }
        argList.add(dependencyArg);
    }
    ArgumentListExpression grabArgs = new ArgumentListExpression(argList);
    grabInitializers.add(new ExpressionStatement(new StaticMethodCallExpression(grapeClassNode, "grab", grabArgs)));
    // insert at beginning so we have the classloader set up before the class is called
    classNode.addStaticInitializerStatements(grabInitializers, true);
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 19 with MapExpression

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

the class GrabAnnotationTransformation method addGrabResolverAsStaticInitIfNeeded.

private void addGrabResolverAsStaticInitIfNeeded(ClassNode grapeClassNode, AnnotationNode node, List<Statement> grabResolverInitializers, Map<String, Object> grabResolverMap) {
    if ((node.getMember("initClass") == null) || (node.getMember("initClass") == ConstantExpression.TRUE)) {
        MapExpression resolverArgs = new MapExpression();
        for (Map.Entry<String, Object> next : grabResolverMap.entrySet()) {
            resolverArgs.addMapEntryExpression(new ConstantExpression(next.getKey()), new ConstantExpression(next.getValue()));
        }
        grabResolverInitializers.add(new ExpressionStatement(new StaticMethodCallExpression(grapeClassNode, "addResolver", new ArgumentListExpression(resolverArgs))));
    }
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 20 with MapExpression

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

the class ImmutableASTTransformation method createConstructorMap.

private void createConstructorMap(ClassNode cNode, List<PropertyNode> list, List<String> knownImmutableClasses, List<String> knownImmutables) {
    final BlockStatement body = new BlockStatement();
    body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression())));
    for (PropertyNode pNode : list) {
        body.addStatement(createConstructorStatement(cNode, pNode, knownImmutableClasses, knownImmutables));
    }
    // check for missing properties
    body.addStatement(stmt(callX(SELF_TYPE, "checkPropNames", args("this", "args"))));
    createConstructorMapCommon(cNode, body);
    if (list.size() > 0) {
        createNoArgConstructor(cNode);
    }
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

MapExpression (org.codehaus.groovy.ast.expr.MapExpression)21 Expression (org.codehaus.groovy.ast.expr.Expression)11 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)10 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)10 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)8 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)8 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)7 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)7 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)7 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)6 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)6 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)6 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)6 Statement (org.codehaus.groovy.ast.stmt.Statement)6 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)5 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)5 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)5 ClassNode (org.codehaus.groovy.ast.ClassNode)4 PropertyNode (org.codehaus.groovy.ast.PropertyNode)4 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)4