Search in sources :

Example 81 with Statement

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

the class GeneralUtils method createConstructorStatementDefault.

public static Statement createConstructorStatementDefault(FieldNode fNode) {
    final String name = fNode.getName();
    final ClassNode fType = fNode.getType();
    final Expression fieldExpr = propX(varX("this"), name);
    Expression initExpr = fNode.getInitialValueExpression();
    Statement assignInit;
    if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
        if (ClassHelper.isPrimitiveType(fType)) {
            assignInit = EmptyStatement.INSTANCE;
        } else {
            assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
        }
    } else {
        assignInit = assignS(fieldExpr, initExpr);
    }
    fNode.setInitialValueExpression(null);
    Expression value = findArg(name);
    return ifElseS(equalsNullX(value), assignInit, assignS(fieldExpr, castX(fType, value)));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 82 with Statement

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

the class AntlrParserPlugin method buildAST.

public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException {
    setClassLoader(classLoader);
    makeModule();
    try {
        convertGroovy(ast);
        if (output.getStatementBlock().isEmpty() && output.getMethods().isEmpty() && output.getClasses().isEmpty()) {
            output.addStatement(ReturnStatement.RETURN_NULL_OR_VOID);
        }
        // set the script source position
        ClassNode scriptClassNode = output.getScriptClassDummy();
        if (scriptClassNode != null) {
            List<Statement> statements = output.getStatementBlock().getStatements();
            if (!statements.isEmpty()) {
                Statement firstStatement = statements.get(0);
                Statement lastStatement = statements.get(statements.size() - 1);
                scriptClassNode.setSourcePosition(firstStatement);
                scriptClassNode.setLastColumnNumber(lastStatement.getLastColumnNumber());
                scriptClassNode.setLastLineNumber(lastStatement.getLastLineNumber());
            }
        }
    } catch (ASTRuntimeException e) {
        throw new ASTParserException(e.getMessage() + ". File: " + sourceUnit.getName(), e);
    }
    return output;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement)

Example 83 with Statement

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

the class ModuleNode method createStatementsClass.

protected ClassNode createStatementsClass() {
    ClassNode classNode = getScriptClassDummy();
    if (classNode.getName().endsWith("package-info")) {
        return classNode;
    }
    handleMainMethodIfPresent(methods);
    // return new Foo(new ShellContext(args)).run()
    classNode.addMethod(new MethodNode("main", ACC_PUBLIC | ACC_STATIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.STRING_TYPE.makeArray(), "args") }, ClassNode.EMPTY_ARRAY, new ExpressionStatement(new MethodCallExpression(new ClassExpression(ClassHelper.make(InvokerHelper.class)), "runScript", new ArgumentListExpression(new ClassExpression(classNode), new VariableExpression("args"))))));
    MethodNode methodNode = new MethodNode("run", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementBlock);
    methodNode.setIsScriptBody();
    classNode.addMethod(methodNode);
    classNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement());
    Statement stmt;
    // (like @BaseScript) that could change this.  But this is cautious and anticipates possible compiler changes.
    if (classNode.getSuperClass().getDeclaredConstructor(SCRIPT_CONTEXT_CTOR) != null) {
        stmt = new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(new VariableExpression("context"))));
    } else {
        // Fallback for non-standard base "script" classes with no context (Binding) constructor.
        stmt = new ExpressionStatement(new MethodCallExpression(new VariableExpression("super"), "setBinding", new ArgumentListExpression(new VariableExpression("context"))));
    }
    classNode.addConstructor(ACC_PUBLIC, new Parameter[] { new Parameter(ClassHelper.make(Binding.class), "context") }, ClassNode.EMPTY_ARRAY, stmt);
    for (MethodNode node : methods) {
        int modifiers = node.getModifiers();
        if ((modifiers & ACC_ABSTRACT) != 0) {
            throw new RuntimeException("Cannot use abstract methods in a script, they are only available inside classes. Method: " + node.getName());
        }
        // br: the old logic seems to add static to all def f().... in a script, which makes enclosing
        // inner classes (including closures) in a def function difficult. Comment it out.
        node.setModifiers(modifiers);
        classNode.addMethod(node);
    }
    return classNode;
}
Also used : InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) 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) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 84 with Statement

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

the class ClassCodeExpressionTransformer method visitProperty.

public void visitProperty(PropertyNode node) {
    visitAnnotations(node);
    Statement statement = node.getGetterBlock();
    visitClassCodeContainer(statement);
    statement = node.getSetterBlock();
    visitClassCodeContainer(statement);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement)

Example 85 with Statement

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

the class ClassNode method addStaticInitializerStatements.

public void addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) {
    MethodNode method = getOrAddStaticConstructorNode();
    BlockStatement block = null;
    Statement statement = method.getCode();
    if (statement == null) {
        block = new BlockStatement();
    } else if (statement instanceof BlockStatement) {
        block = (BlockStatement) statement;
    } else {
        block = new BlockStatement();
        block.addStatement(statement);
    }
    // before the other statements
    if (!fieldInit) {
        block.addStatements(staticStatements);
    } else {
        List<Statement> blockStatements = block.getStatements();
        staticStatements.addAll(blockStatements);
        blockStatements.clear();
        blockStatements.addAll(staticStatements);
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

Statement (org.codehaus.groovy.ast.stmt.Statement)205 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)167 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)121 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)57 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)55 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)54 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)53 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)53 ClassNode (org.codehaus.groovy.ast.ClassNode)50 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)50 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)40 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)39 WhileStatement (org.codehaus.groovy.ast.stmt.WhileStatement)39 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)38 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)38 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)38