Search in sources :

Example 26 with MapExpression

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

the class TestMixinTransformation method addTestRuntimeAwareMixinFieldIfNonExistent.

protected FieldNode addTestRuntimeAwareMixinFieldIfNonExistent(ClassNode classNode, ClassNode fieldType, String fieldName) {
    if (classNode == null || classNode.getField(fieldName) != null) {
        return null;
    }
    MapExpression constructorArguments = new MapExpression();
    constructorArguments.addMapEntryExpression(new MapEntryExpression(new ConstantExpression("testClass"), new ClassExpression(classNode)));
    FieldNode mixinInstanceFieldNode = classNode.addField(fieldName, Modifier.STATIC, fieldType, new ConstructorCallExpression(fieldType, constructorArguments));
    mixinInstanceFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(MixinInstance.class)));
    addJunitRuleFields(classNode);
    return mixinInstanceFieldNode;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 27 with MapExpression

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

the class MacroInvocationTrap method handleTargetMethodCallExpression.

@Override
protected boolean handleTargetMethodCallExpression(MethodCallExpression macroCall) {
    final ClosureExpression closureExpression = getClosureArgument(macroCall);
    if (closureExpression == null) {
        return true;
    }
    if (closureExpression.getParameters() != null && closureExpression.getParameters().length > 0) {
        addError("Macro closure arguments are not allowed", closureExpression);
        return true;
    }
    final MapExpression mapExpression = buildSubstitutionMap(closureExpression);
    String source = convertClosureToSource(closureExpression);
    BlockStatement closureBlock = (BlockStatement) closureExpression.getCode();
    Boolean asIs = false;
    TupleExpression macroArguments = getMacroArguments(macroCall);
    if (macroArguments == null) {
        return true;
    }
    List<Expression> macroArgumentsExpressions = macroArguments.getExpressions();
    if (macroArgumentsExpressions.size() == 2 || macroArgumentsExpressions.size() == 3) {
        Expression asIsArgumentExpression = macroArgumentsExpressions.get(macroArgumentsExpressions.size() - 2);
        if ((asIsArgumentExpression instanceof ConstantExpression)) {
            ConstantExpression asIsConstantExpression = (ConstantExpression) asIsArgumentExpression;
            if (!(asIsConstantExpression.getValue() instanceof Boolean)) {
                addError("AsIs argument value should be boolean", asIsConstantExpression);
                return true;
            }
            asIs = (Boolean) asIsConstantExpression.getValue();
        }
    }
    macroArgumentsExpressions.remove(macroArgumentsExpressions.size() - 1);
    macroArgumentsExpressions.add(new ConstantExpression(source));
    macroArgumentsExpressions.add(mapExpression);
    macroArgumentsExpressions.add(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.getMacroValue(closureBlock, asIs).getClass(), false)));
    macroCall.setObjectExpression(new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"));
    macroCall.setSpreadSafe(false);
    macroCall.setSafe(false);
    macroCall.setImplicitThis(false);
    return true;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MacroBuilder(org.codehaus.groovy.macro.runtime.MacroBuilder) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 28 with MapExpression

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

the class MacroInvocationTrap method buildSubstitutionMap.

private MapExpression buildSubstitutionMap(final ASTNode expr) {
    final Map<MacroSubstitutionKey, ClosureExpression> map = new HashMap<MacroSubstitutionKey, ClosureExpression>();
    final MapExpression mapExpression = new MapExpression();
    ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {

        @Override
        protected SourceUnit getSourceUnit() {
            return null;
        }

        @Override
        public void visitClass(final ClassNode node) {
            super.visitClass(node);
            Iterator<InnerClassNode> it = node.getInnerClasses();
            while (it.hasNext()) {
                InnerClassNode next = it.next();
                visitClass(next);
            }
        }

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            super.visitMethodCallExpression(call);
            if (isBuildInvocation(call, MacroTransformation.DOLLAR_VALUE)) {
                ClosureExpression substitutionClosureExpression = getClosureArgument(call);
                if (substitutionClosureExpression == null) {
                    return;
                }
                Statement code = substitutionClosureExpression.getCode();
                if (code instanceof BlockStatement) {
                    ((BlockStatement) code).setVariableScope(null);
                }
                MacroSubstitutionKey key = new MacroSubstitutionKey(call, expr.getLineNumber(), expr.getColumnNumber());
                map.put(key, substitutionClosureExpression);
            }
        }
    };
    if (expr instanceof ClassNode) {
        visitor.visitClass((ClassNode) expr);
    } else {
        expr.visit(visitor);
    }
    for (Map.Entry<MacroSubstitutionKey, ClosureExpression> entry : map.entrySet()) {
        mapExpression.addMapEntryExpression(entry.getKey().toConstructorCallExpression(), entry.getValue());
    }
    return mapExpression;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) HashMap(java.util.HashMap) Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MacroSubstitutionKey(org.codehaus.groovy.macro.runtime.MacroSubstitutionKey) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with MapExpression

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

the class ClassNodeUtils method hasPossibleStaticMethod.

/**
     * Returns true if the given method has a possibly matching static method with the given name and arguments.
     *
     * @param cNode     the ClassNode of interest
     * @param name      the name of the method of interest
     * @param arguments the arguments to match against
     * @param trySpread whether to try to account for SpreadExpressions within the arguments
     * @return true if a matching method was found
     */
public static boolean hasPossibleStaticMethod(ClassNode cNode, String name, Expression arguments, boolean trySpread) {
    int count = 0;
    boolean foundSpread = false;
    if (arguments instanceof TupleExpression) {
        TupleExpression tuple = (TupleExpression) arguments;
        for (Expression arg : tuple.getExpressions()) {
            if (arg instanceof SpreadExpression) {
                foundSpread = true;
            } else {
                count++;
            }
        }
    } else if (arguments instanceof MapExpression) {
        count = 1;
    }
    for (MethodNode method : cNode.getMethods(name)) {
        if (method.isStatic()) {
            Parameter[] parameters = method.getParameters();
            // do fuzzy match for spread case: count will be number of non-spread args
            if (trySpread && foundSpread && parameters.length >= count)
                return true;
            if (parameters.length == count)
                return true;
            // handle varargs case
            if (parameters.length > 0 && parameters[parameters.length - 1].getType().isArray()) {
                if (count >= parameters.length - 1)
                    return true;
                // fuzzy match any spread to a varargs
                if (trySpread && foundSpread)
                    return true;
            }
            // handle parameters with default values
            int nonDefaultParameters = 0;
            for (Parameter parameter : parameters) {
                if (!parameter.hasInitialExpression()) {
                    nonDefaultParameters++;
                }
            }
            if (count < parameters.length && nonDefaultParameters <= count) {
                return true;
            }
        // TODO handle spread with nonDefaultParams?
        }
    }
    return false;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Parameter(org.codehaus.groovy.ast.Parameter) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression)

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

Aggregations

MapExpression (org.codehaus.groovy.ast.expr.MapExpression)43 Expression (org.codehaus.groovy.ast.expr.Expression)27 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)23 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)22 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)22 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)21 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)19 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)19 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)19 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)18 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)18 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)18 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)18 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)16 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)16 ClassNode (org.codehaus.groovy.ast.ClassNode)15 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)15 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)13 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)13 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)13