Search in sources :

Example 96 with MethodCallExpression

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

the class PluginUseScriptBlockMetadataExtractor method extract.

public void extract(SourceUnit sourceUnit, ScriptBlock scriptBlock) {
    ClosureExpression closureArg = scriptBlock.getClosureExpression();
    closureArg.getCode().visit(new RestrictiveCodeVisitor(sourceUnit, formatErrorMessage(BASE_MESSAGE)) {

        @Override
        public void visitBlockStatement(BlockStatement block) {
            for (Statement statement : block.getStatements()) {
                statement.visit(this);
            }
        }

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            if (!call.isImplicitThis()) {
                Expression target = call.getObjectExpression();
                if (!(target instanceof MethodCallExpression)) {
                    restrict(target, formatErrorMessage(BASE_MESSAGE));
                    return;
                }
                visitMethodCallExpression((MethodCallExpression) target);
            }
            if (call.getMethod() instanceof ConstantExpression) {
                ConstantExpression methodName = (ConstantExpression) call.getMethod();
                if (isOfType(methodName, String.class)) {
                    String methodNameText = methodName.getText();
                    if (methodNameText.equals("id") || methodNameText.equals("version")) {
                        ConstantExpression argumentExpression = hasSingleConstantArgOfType(call, String.class);
                        if (argumentExpression == null) {
                            restrict(call, formatErrorMessage(NEED_SINGLE_STRING));
                            return;
                        }
                        String argStringValue = argumentExpression.getValue().toString();
                        if (argStringValue.length() == 0) {
                            restrict(argumentExpression, formatErrorMessage(NEED_SINGLE_STRING));
                            return;
                        }
                        if (methodName.getText().equals("id")) {
                            if (call.isImplicitThis()) {
                                try {
                                    DefaultPluginId.validate(argStringValue);
                                    call.setNodeMetaData(PluginDependencySpec.class, pluginRequestCollector.createSpec(call.getLineNumber()).id(argStringValue));
                                } catch (InvalidPluginIdException e) {
                                    restrict(argumentExpression, formatErrorMessage(e.getReason()));
                                }
                            } else {
                                restrict(call, formatErrorMessage(BASE_MESSAGE));
                            }
                        }
                        if (methodName.getText().equals("version")) {
                            PluginDependencySpec spec = getSpecFor(call);
                            if (spec == null) {
                                return;
                            }
                            spec.version(argStringValue);
                            call.setNodeMetaData(PluginDependencySpec.class, spec);
                        }
                    } else if (methodNameText.equals("apply")) {
                        ConstantExpression arguments = hasSingleConstantArgOfType(call, boolean.class);
                        if (arguments == null) {
                            restrict(call, formatErrorMessage(NEED_SINGLE_BOOLEAN));
                            return;
                        }
                        PluginDependencySpec spec = getSpecFor(call);
                        if (spec == null) {
                            return;
                        }
                        spec.apply((Boolean) arguments.getValue());
                    } else {
                        if (!call.isImplicitThis()) {
                            restrict(methodName, formatErrorMessage(EXTENDED_MESSAGE));
                        } else {
                            restrict(methodName, formatErrorMessage(BASE_MESSAGE));
                        }
                    }
                } else {
                    restrict(methodName, formatErrorMessage(NOT_LITERAL_ID_METHOD_NAME));
                }
            } else {
                restrict(call);
            }
        }

        private PluginDependencySpec getSpecFor(MethodCallExpression call) {
            Expression objectExpression = call.getObjectExpression();
            if (objectExpression instanceof MethodCallExpression) {
                return objectExpression.getNodeMetaData(PluginDependencySpec.class);
            } else {
                restrict(call, formatErrorMessage(BASE_MESSAGE));
                return null;
            }
        }

        @Override
        public void visitExpressionStatement(ExpressionStatement statement) {
            statement.getExpression().visit(this);
        }
    });
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) RestrictiveCodeVisitor(org.gradle.groovy.scripts.internal.RestrictiveCodeVisitor) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) InvalidPluginIdException(org.gradle.plugin.internal.InvalidPluginIdException) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) PluginDependencySpec(org.gradle.plugin.use.PluginDependencySpec) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 97 with MethodCallExpression

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

the class MarkupBuilderCodeTransformer method transform.

@Override
public Expression transform(final Expression exp) {
    if (exp instanceof BinaryExpression) {
        return transformBinaryExpression((BinaryExpression) exp);
    }
    if (exp instanceof MethodCallExpression) {
        return transformMethodCall((MethodCallExpression) exp);
    }
    if (exp instanceof ClosureExpression) {
        ClosureExpression cl = (ClosureExpression) exp;
        cl.getCode().visit(this);
        return cl;
    }
    if (exp instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) exp;
        if (var.getAccessedVariable() instanceof DynamicVariable) {
            MethodCallExpression callGetModel = new MethodCallExpression(new VariableExpression("this"), "getModel", ArgumentListExpression.EMPTY_ARGUMENTS);
            callGetModel.setImplicitThis(true);
            callGetModel.setSourcePosition(exp);
            String varName = var.getName();
            if ("model".equals(varName) || "unescaped".equals(varName)) {
                return callGetModel;
            }
            MethodCallExpression mce = new MethodCallExpression(callGetModel, "get", new ArgumentListExpression(new ConstantExpression(varName)));
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            MethodCallExpression yield = new MethodCallExpression(new VariableExpression("this"), "tryEscape", new ArgumentListExpression(mce));
            yield.setImplicitThis(true);
            yield.setSourcePosition(exp);
            yield.putNodeMetaData(TARGET_VARIABLE, varName);
            return autoEscape ? yield : mce;
        }
    }
    return super.transform(exp);
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 98 with MethodCallExpression

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

the class AutoNewLineTransformer method visitMethodCallExpression.

@Override
public void visitMethodCallExpression(final MethodCallExpression call) {
    boolean old = inBuilderMethod;
    inBuilderMethod = false;
    if (call.isImplicitThis() && call.getArguments() instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
        if (!expressions.isEmpty()) {
            Expression lastArg = expressions.get(expressions.size() - 1);
            if (lastArg instanceof ClosureExpression) {
                call.getObjectExpression().visit(this);
                call.getMethod().visit(this);
                for (Expression expression : expressions) {
                    inBuilderMethod = (expression == lastArg);
                    expression.visit(this);
                }
            }
        }
    } else {
        super.visitMethodCallExpression(call);
    }
    inBuilderMethod = old;
}
Also used : VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 99 with MethodCallExpression

use of org.codehaus.groovy.ast.expr.MethodCallExpression 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 100 with MethodCallExpression

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

the class MacroInvocationTrap method getClosureArgument.

protected ClosureExpression getClosureArgument(MethodCallExpression call) {
    TupleExpression tupleArguments = getMacroArguments(call);
    if (tupleArguments.getExpressions().size() < 1) {
        addError("Call arguments should have at least one argument", tupleArguments);
        return null;
    }
    Expression result = tupleArguments.getExpression(tupleArguments.getExpressions().size() - 1);
    if (!(result instanceof ClosureExpression)) {
        addError("Last call argument should be a closure", result);
        return null;
    }
    return (ClosureExpression) result;
}
Also used : 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) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Aggregations

MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)131 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)89 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)81 Expression (org.codehaus.groovy.ast.expr.Expression)77 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)73 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)59 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)58 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)51 ClassNode (org.codehaus.groovy.ast.ClassNode)43 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)42 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)40 MethodNode (org.codehaus.groovy.ast.MethodNode)36 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)36 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)36 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)35 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)35 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)34 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)31 FieldNode (org.codehaus.groovy.ast.FieldNode)26 Parameter (org.codehaus.groovy.ast.Parameter)23