Search in sources :

Example 6 with EmptyStatement

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

the class FinalVariableAnalyzer method visitTryCatchFinally.

@Override
public void visitTryCatchFinally(final TryCatchStatement statement) {
    visitStatement(statement);
    Map<Variable, VariableState> beforeTryCatch = new HashMap<Variable, VariableState>(getState());
    statement.getTryStatement().visit(this);
    for (CatchStatement catchStatement : statement.getCatchStatements()) {
        catchStatement.visit(this);
    }
    Statement finallyStatement = statement.getFinallyStatement();
    // we need to recall which final variables are unassigned so cloning the current state
    Map<Variable, VariableState> afterTryCatchState = new HashMap<Variable, VariableState>(getState());
    if (finallyStatement instanceof EmptyStatement) {
        // dispatching to EmptyStatement will not call back visitor,
        // must call our visitEmptyStatement explicitly
        visitEmptyStatement((EmptyStatement) finallyStatement);
    } else {
        finallyStatement.visit(this);
    }
    // and now we must reset to uninitialized state variables which were only initialized during try/catch
    Map<Variable, VariableState> afterFinally = new HashMap<Variable, VariableState>(getState());
    for (Map.Entry<Variable, VariableState> entry : afterFinally.entrySet()) {
        Variable var = entry.getKey();
        VariableState afterFinallyState = entry.getValue();
        VariableState beforeTryCatchState = beforeTryCatch.get(var);
        if (afterFinallyState == VariableState.is_final && beforeTryCatchState != VariableState.is_final && afterTryCatchState.get(var) != beforeTryCatchState) {
            getState().put(var, beforeTryCatchState == null ? VariableState.is_uninitialized : beforeTryCatchState);
        }
    }
}
Also used : Variable(org.codehaus.groovy.ast.Variable) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) HashMap(java.util.HashMap) Statement(org.codehaus.groovy.ast.stmt.Statement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with EmptyStatement

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

the class AutoImplementASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        ClassNode exception = getMemberClassValue(anno, "exception");
        if (exception != null && Undefined.isUndefinedException(exception)) {
            exception = null;
        }
        String message = getMemberStringValue(anno, "message");
        Expression code = anno.getMember("code");
        if (code != null && !(code instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'code'. Found " + code, cNode);
            return;
        }
        createMethods(cNode, exception, message, (ClosureExpression) code);
        if (code != null) {
            anno.setMember("code", new ClosureExpression(new Parameter[0], new EmptyStatement()));
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 8 with EmptyStatement

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

the class AntlrParserPlugin method tryStatement.

protected Statement tryStatement(AST tryStatementNode) {
    AST tryNode = tryStatementNode.getFirstChild();
    Statement tryStatement = statement(tryNode);
    Statement finallyStatement = EmptyStatement.INSTANCE;
    AST node = tryNode.getNextSibling();
    // let's do the catch nodes
    List<CatchStatement> catches = new ArrayList<CatchStatement>();
    for (; node != null && isType(LITERAL_catch, node); node = node.getNextSibling()) {
        final List<CatchStatement> catchStatements = catchStatement(node);
        catches.addAll(catchStatements);
    }
    if (isType(LITERAL_finally, node)) {
        finallyStatement = statement(node);
        node = node.getNextSibling();
    }
    if (finallyStatement instanceof EmptyStatement && catches.isEmpty()) {
        throw new ASTRuntimeException(tryStatementNode, "A try statement must have at least one catch or finally block.");
    }
    TryCatchStatement tryCatchStatement = new TryCatchStatement(tryStatement, finallyStatement);
    configureAST(tryCatchStatement, tryStatementNode);
    for (CatchStatement statement : catches) {
        tryCatchStatement.addCatch(statement);
    }
    return tryCatchStatement;
}
Also used : AST(antlr.collections.AST) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) 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) ArrayList(java.util.ArrayList) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement)

Example 9 with EmptyStatement

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

the class MapConstructorASTTransformation method visit.

//    private static final ClassNode CHECK_METHOD_TYPE = make(ImmutableASTTransformation.class);
public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        boolean includeProperties = !memberHasValue(anno, "includeProperties", false);
        boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
        boolean useSetters = memberHasValue(anno, "useSetters", true);
        List<String> excludes = getMemberStringList(anno, "excludes");
        List<String> includes = getMemberStringList(anno, "includes");
        boolean allNames = memberHasValue(anno, "allNames", true);
        if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME))
            return;
        if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, false))
            return;
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, false))
            return;
        // if @Immutable is found, let it pick up options and do work so we'll skip
        if (hasAnnotation(cNode, ImmutableASTTransformation.MY_TYPE))
            return;
        Expression pre = anno.getMember("pre");
        if (pre != null && !(pre instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'pre'. Found " + pre, cNode);
            return;
        }
        Expression post = anno.getMember("post");
        if (post != null && !(post instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'post'. Found " + post, cNode);
            return;
        }
        createConstructor(cNode, includeFields, includeProperties, includeSuperProperties, useSetters, excludes, includes, (ClosureExpression) pre, (ClosureExpression) post, source, allNames);
        if (pre != null) {
            anno.setMember("pre", new ClosureExpression(new Parameter[0], new EmptyStatement()));
        }
        if (post != null) {
            anno.setMember("post", new ClosureExpression(new Parameter[0], new EmptyStatement()));
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 10 with EmptyStatement

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

the class StaticTypeCheckingVisitor method visitIfElse.

@Override
public void visitIfElse(final IfStatement ifElse) {
    Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
    try {
        // create a new temporary element in the if-then-else type info
        typeCheckingContext.pushTemporaryTypeInfo();
        visitStatement(ifElse);
        ifElse.getBooleanExpression().visit(this);
        ifElse.getIfBlock().visit(this);
        // pop if-then-else temporary type info
        typeCheckingContext.popTemporaryTypeInfo();
        // GROOVY-6099: restore assignment info as before the if branch
        restoreTypeBeforeConditional();
        Statement elseBlock = ifElse.getElseBlock();
        if (elseBlock instanceof EmptyStatement) {
            // dispatching to EmptyStatement will not call back visitor,
            // must call our visitEmptyStatement explicitly
            visitEmptyStatement((EmptyStatement) elseBlock);
        } else {
            elseBlock.visit(this);
        }
    } finally {
        popAssignmentTracking(oldTracker);
    }
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)13 Statement (org.codehaus.groovy.ast.stmt.Statement)10 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)9 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)9 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)9 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)8 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)6 Expression (org.codehaus.groovy.ast.expr.Expression)5 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)5 Variable (org.codehaus.groovy.ast.Variable)4 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)4 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)4 ArrayList (java.util.ArrayList)3 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)3 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)3 ClassNode (org.codehaus.groovy.ast.ClassNode)3 Parameter (org.codehaus.groovy.ast.Parameter)3 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)3 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)3 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)3