Search in sources :

Example 11 with EmptyStatement

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

the class FinalVariableAnalyzer method visitIfElse.

@Override
public void visitIfElse(final IfStatement ifElse) {
    visitStatement(ifElse);
    ifElse.getBooleanExpression().visit(this);
    Map<Variable, VariableState> ifState = pushState();
    ifElse.getIfBlock().visit(this);
    popState();
    Statement elseBlock = ifElse.getElseBlock();
    Map<Variable, VariableState> elseState = pushState();
    if (elseBlock instanceof EmptyStatement) {
        // dispatching to EmptyStatement will not call back visitor,
        // must call our visitEmptyStatement explicitly
        visitEmptyStatement((EmptyStatement) elseBlock);
    } else {
        elseBlock.visit(this);
    }
    popState();
    // merge if/else branches
    Map<Variable, VariableState> curState = getState();
    Set<Variable> allVars = new HashSet<Variable>();
    allVars.addAll(curState.keySet());
    allVars.addAll(ifState.keySet());
    allVars.addAll(elseState.keySet());
    for (Variable var : allVars) {
        VariableState beforeValue = curState.get(var);
        VariableState ifValue = ifState.get(var);
        VariableState elseValue = elseState.get(var);
        // merge if and else values
        VariableState mergedIfElse;
        mergedIfElse = ifValue != null && elseValue != null && ifValue.isFinal && elseValue.isFinal ? VariableState.is_final : VariableState.is_var;
        if (beforeValue == null) {
            curState.put(var, mergedIfElse);
        } else {
            if (beforeValue == VariableState.is_uninitialized) {
                curState.put(var, mergedIfElse);
            } else if (ifValue != null || elseValue != null) {
                curState.put(var, VariableState.is_var);
            }
        }
    }
}
Also used : Variable(org.codehaus.groovy.ast.Variable) 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) HashSet(java.util.HashSet)

Example 12 with EmptyStatement

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

the class TupleConstructorASTTransformation 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;
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        boolean includeProperties = !memberHasValue(anno, "includeProperties", false);
        boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true);
        boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
        boolean callSuper = memberHasValue(anno, "callSuper", true);
        boolean force = memberHasValue(anno, "force", true);
        boolean defaults = !memberHasValue(anno, "defaults", false);
        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, includeSuperFields))
            return;
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, false, includeSuperFields))
            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(this, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, callSuper, force, excludes, includes, useSetters, defaults, allNames, sourceUnit, (ClosureExpression) pre, (ClosureExpression) post);
        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) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) 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 13 with EmptyStatement

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

the class RulesVisitor method visitMethodCallExpression.

@Override
public void visitMethodCallExpression(MethodCallExpression call) {
    ClosureExpression closureExpression = AstUtils.getSingleClosureArg(call);
    if (closureExpression != null) {
        // path { ... }
        rewriteAction(call, extractModelPathFromMethodTarget(call), closureExpression, RuleVisitor.displayName(call));
        return;
    }
    Pair<ClassExpression, ClosureExpression> args = AstUtils.getClassAndClosureArgs(call);
    if (args != null) {
        // path(Type) { ... }
        rewriteCreator(call, extractModelPathFromMethodTarget(call), args.getRight(), args.getLeft(), RuleVisitor.displayName(call));
        return;
    }
    ClassExpression classArg = AstUtils.getClassArg(call);
    if (classArg != null) {
        // path(Type)
        String displayName = RuleVisitor.displayName(call);
        List<Statement> statements = Lists.newLinkedList();
        statements.add(new EmptyStatement());
        BlockStatement block = new BlockStatement(statements, new VariableScope());
        closureExpression = new ClosureExpression(Parameter.EMPTY_ARRAY, block);
        closureExpression.setVariableScope(block.getVariableScope());
        String modelPath = extractModelPathFromMethodTarget(call);
        rewriteCreator(call, modelPath, closureExpression, classArg, displayName);
        return;
    }
    restrict(call, INVALID_RULE_SIGNATURE);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) VariableScope(org.codehaus.groovy.ast.VariableScope)

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