Search in sources :

Example 11 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 12 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 13 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 14 with MapExpression

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

the class GrabAnnotationTransformation method addGrabResolverAsStaticInitIfNeeded.

private static 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(constX(next.getKey()), constX(next.getValue()));
        }
        grabResolverInitializers.add(stmt(callX(grapeClassNode, "addResolver", args(resolverArgs))));
    }
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) Map(java.util.Map) HashMap(java.util.HashMap)

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

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