Search in sources :

Example 21 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class InnerClassCompletionVisitor method addThisReference.

private void addThisReference(ConstructorNode node) {
    if (!shouldHandleImplicitThisForInnerClass(classNode))
        return;
    Statement code = node.getCode();
    // add "this$0" field init
    //add this parameter to node
    Parameter[] params = node.getParameters();
    Parameter[] newParams = new Parameter[params.length + 1];
    System.arraycopy(params, 0, newParams, 1, params.length);
    String name = getUniqueName(params, node);
    Parameter thisPara = new Parameter(classNode.getOuterClass().getPlainNodeReference(), name);
    newParams[0] = thisPara;
    node.setParameters(newParams);
    BlockStatement block = null;
    if (code == null) {
        block = new BlockStatement();
    } else if (!(code instanceof BlockStatement)) {
        block = new BlockStatement();
        block.addStatement(code);
    } else {
        block = (BlockStatement) code;
    }
    BlockStatement newCode = new BlockStatement();
    addFieldInit(thisPara, thisField, newCode);
    ConstructorCallExpression cce = getFirstIfSpecialConstructorCall(block);
    if (cce == null) {
        cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression());
        block.getStatements().add(0, new ExpressionStatement(cce));
    }
    if (shouldImplicitlyPassThisPara(cce)) {
        // add thisPara to this(...)
        TupleExpression args = (TupleExpression) cce.getArguments();
        List<Expression> expressions = args.getExpressions();
        VariableExpression ve = new VariableExpression(thisPara.getName());
        ve.setAccessedVariable(thisPara);
        expressions.add(0, ve);
    }
    if (cce.isSuperCall()) {
        // we have a call to super here, so we need to add
        // our code after that
        block.getStatements().add(1, newCode);
    }
    node.setCode(block);
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) 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) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 22 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class InnerClassCompletionVisitor method getFirstIfSpecialConstructorCall.

private static ConstructorCallExpression getFirstIfSpecialConstructorCall(BlockStatement code) {
    if (code == null)
        return null;
    final List<Statement> statementList = code.getStatements();
    if (statementList.isEmpty())
        return null;
    final Statement statement = statementList.get(0);
    if (!(statement instanceof ExpressionStatement))
        return null;
    Expression expression = ((ExpressionStatement) statement).getExpression();
    if (!(expression instanceof ConstructorCallExpression))
        return null;
    ConstructorCallExpression cce = (ConstructorCallExpression) expression;
    if (cce.isSpecialCall())
        return cce;
    return null;
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) 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) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 23 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class VariableScopeVisitor method visitConstructorCallExpression.

public void visitConstructorCallExpression(ConstructorCallExpression call) {
    isSpecialConstructorCall = call.isSpecialCall();
    super.visitConstructorCallExpression(call);
    isSpecialConstructorCall = false;
    if (!call.isUsingAnonymousInnerClass())
        return;
    pushState();
    InnerClassNode innerClass = (InnerClassNode) call.getType();
    innerClass.setVariableScope(currentScope);
    for (MethodNode method : innerClass.getMethods()) {
        Parameter[] parameters = method.getParameters();
        // null means no implicit "it"
        if (parameters.length == 0)
            parameters = null;
        ClosureExpression cl = new ClosureExpression(parameters, method.getCode());
        visitClosureExpression(cl);
    }
    for (FieldNode field : innerClass.getFields()) {
        final Expression expression = field.getInitialExpression();
        pushState(field.isStatic());
        if (expression != null) {
            if (expression instanceof VariableExpression) {
                VariableExpression vexp = (VariableExpression) expression;
                if (vexp.getAccessedVariable() instanceof Parameter) {
                    // workaround for GROOVY-6834: accessing a parameter which is not yet seen in scope
                    popState();
                    continue;
                }
            }
            expression.visit(this);
        }
        popState();
    }
    for (Statement statement : innerClass.getObjectInitializerStatements()) {
        statement.visit(this);
    }
    markClosureSharedVariables();
    popState();
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement)

Example 24 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class Verifier method visitMethod.

public void visitMethod(MethodNode node) {
    //GROOVY-3712 - if it's an MOP method, it's an error as they aren't supposed to exist before ACG is invoked
    if (MopWriter.isMopMethod(node.getName())) {
        throw new RuntimeParserException("Found unexpected MOP methods in the class node for " + classNode.getName() + "(" + node.getName() + ")", classNode);
    }
    this.methodNode = node;
    adjustTypesIfStaticMainMethod(node);
    addReturnIfNeeded(node);
    Statement statement;
    statement = node.getCode();
    if (statement != null)
        statement.visit(new VerifierCodeVisitor(this));
}
Also used : BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException)

Example 25 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class Verifier method addDefaultParameterConstructors.

protected void addDefaultParameterConstructors(final ClassNode node) {
    List methods = new ArrayList(node.getDeclaredConstructors());
    addDefaultParameters(methods, new DefaultArgsAction() {

        public void call(ArgumentListExpression arguments, Parameter[] newParams, MethodNode method) {
            ConstructorNode ctor = (ConstructorNode) method;
            ConstructorCallExpression expression = new ConstructorCallExpression(ClassNode.THIS, arguments);
            Statement code = new ExpressionStatement(expression);
            addConstructor(newParams, ctor, code, node);
        }
    });
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ArrayList(java.util.ArrayList) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Statement (org.codehaus.groovy.ast.stmt.Statement)206 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)168 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)122 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)90 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)79 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)71 Expression (org.codehaus.groovy.ast.expr.Expression)63 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)56 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)55 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)53 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)53 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)53 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)50 ClassNode (org.codehaus.groovy.ast.ClassNode)49 WhileStatement (org.codehaus.groovy.ast.stmt.WhileStatement)39 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)38 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)38 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)38 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)38 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)38