Search in sources :

Example 71 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement 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 72 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.

the class AbstractGrailsArtefactTransformer method addApiLookupFieldAndSetter.

protected void addApiLookupFieldAndSetter(ClassNode classNode, ClassNode implementationNode, String apiProperty, Expression initialValueExpression) {
    FieldNode fieldNode = classNode.getField(apiProperty);
    if (fieldNode == null || !fieldNode.getDeclaringClass().equals(classNode)) {
        fieldNode = new FieldNode(apiProperty, Modifier.PRIVATE | Modifier.STATIC, implementationNode, classNode, initialValueExpression);
        classNode.addField(fieldNode);
        String setterName = "set" + MetaClassHelper.capitalize(apiProperty);
        Parameter setterParameter = new Parameter(implementationNode, apiProperty);
        BlockStatement setterBody = new BlockStatement();
        setterBody.addStatement(new ExpressionStatement(new BinaryExpression(new AttributeExpression(new ClassExpression(classNode), new ConstantExpression(apiProperty)), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression(setterParameter))));
        GrailsASTUtils.addCompileStaticAnnotation(classNode.addMethod(setterName, Modifier.PUBLIC | Modifier.STATIC, ClassHelper.VOID_TYPE, new Parameter[] { setterParameter }, null, setterBody));
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression)

Example 73 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.

the class ASTValidationErrorsHelper method addSetErrorsMethod.

protected void addSetErrorsMethod(final ClassNode paramTypeClassNode) {
    final String errorsArgumentName = "$errorsArg";
    MethodNode setErrorsMethod = paramTypeClassNode.getMethod(SET_ERRORS_METHOD_NAME, new Parameter[] { new Parameter(ERRORS_CLASS_NODE, errorsArgumentName) });
    if (setErrorsMethod == null) {
        final Expression assignErrorsExpression = new BinaryExpression(ERRORS_EXPRESSION, EQUALS_SYMBOL, new VariableExpression(errorsArgumentName));
        setErrorsMethod = new MethodNode(SET_ERRORS_METHOD_NAME, Modifier.PUBLIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ERRORS_CLASS_NODE, errorsArgumentName) }, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, new ExpressionStatement(assignErrorsExpression));
        paramTypeClassNode.addMethod(setErrorsMethod);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 74 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project gradle by gradle.

the class RuleVisitor method visitRuleClosure.

public void visitRuleClosure(ClosureExpression expression, Expression invocation, String invocationDisplayName) {
    InputReferences parentInputs = inputs;
    VariableExpression parentInputsVariable = inputsVariable;
    try {
        inputs = new InputReferences();
        inputsVariable = new VariableExpression("__rule_inputs_var_" + (counter++), POTENTIAL_INPUTS);
        inputsVariable.setClosureSharedVariable(true);
        super.visitClosureExpression(expression);
        BlockStatement code = (BlockStatement) expression.getCode();
        code.setNodeMetaData(AST_NODE_METADATA_LOCATION_KEY, new SourceLocation(location, scriptSourceDescription, invocationDisplayName, invocation.getLineNumber(), invocation.getColumnNumber()));
        code.setNodeMetaData(AST_NODE_METADATA_INPUTS_KEY, inputs);
        if (parentInputsVariable != null) {
            expression.getVariableScope().putReferencedLocalVariable(parentInputsVariable);
        }
        code.getVariableScope().putDeclaredVariable(inputsVariable);
        if (parentInputsVariable == null) {
            // <inputs-lvar> = <inputs-field>
            DeclarationExpression variableDeclaration = new DeclarationExpression(inputsVariable, ASSIGN, new VariableExpression(INPUTS_FIELD_NAME));
            code.getStatements().add(0, new ExpressionStatement(variableDeclaration));
        } else {
            // <inputs-lvar> = <inputs-field> ?: <parent-inputs-lvar>
            DeclarationExpression variableDeclaration = new DeclarationExpression(inputsVariable, ASSIGN, new ElvisOperatorExpression(new VariableExpression(INPUTS_FIELD_NAME), parentInputsVariable));
            code.getStatements().add(0, new ExpressionStatement(variableDeclaration));
        }
        // Move default values into body of closure, so they can use <inputs-lvar>
        for (Parameter parameter : expression.getParameters()) {
            if (parameter.hasInitialExpression()) {
                code.getStatements().add(1, new ExpressionStatement(new BinaryExpression(new VariableExpression(parameter.getName()), ASSIGN, parameter.getInitialExpression())));
                parameter.setInitialExpression(ConstantExpression.NULL);
            }
        }
    } finally {
        if (parentInputs != null) {
            parentInputs.addNestedReferences(inputs);
        }
        inputs = parentInputs;
        inputsVariable = parentInputsVariable;
    }
}
Also used : ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 75 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.

the class GroovyPageOptimizerVisitor method introduceThisObjectVariable.

// TODO: Research why http://jira.grails.org/browse/GRAILS-8679 happens with this enabled. See ElvisAndClosureGroovyPageTests
//    @Override
//    public void visitClosureExpression(ClosureExpression expression) {
//        innerClosures.push(expression);
//        introduceThisObjectVariable(expression);
//        super.visitClosureExpression(expression);
//        innerClosures.pop();
//    }
@SuppressWarnings("unused")
private void introduceThisObjectVariable(ClosureExpression closureExpression) {
    if (closureExpression.getCode() instanceof BlockStatement) {
        List<Statement> oldBlock = ((BlockStatement) closureExpression.getCode()).getStatements();
        BlockStatement newBlock = new BlockStatement();
        newBlock.addStatement(new ExpressionStatement(thisObjectDeclaration));
        newBlock.addStatements(oldBlock);
        closureExpression.setCode(newBlock);
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)122 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)78 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)65 Statement (org.codehaus.groovy.ast.stmt.Statement)58 Expression (org.codehaus.groovy.ast.expr.Expression)54 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)52 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)51 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)49 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)44 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)39 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)36 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)35 Parameter (org.codehaus.groovy.ast.Parameter)30 MethodNode (org.codehaus.groovy.ast.MethodNode)29 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)29 ClassNode (org.codehaus.groovy.ast.ClassNode)27 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)26 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)26 ArrayList (java.util.ArrayList)25 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)24