Search in sources :

Example 31 with Statement

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

the class StaticTypeCheckingVisitor method visitClosureExpression.

@Override
public void visitClosureExpression(final ClosureExpression expression) {
    boolean oldStaticContext = typeCheckingContext.isInStaticContext;
    typeCheckingContext.isInStaticContext = false;
    // collect every variable expression used in the loop body
    final Map<VariableExpression, ClassNode> varOrigType = new HashMap<VariableExpression, ClassNode>();
    Statement code = expression.getCode();
    code.visit(new VariableExpressionTypeMemoizer(varOrigType));
    Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
    // first, collect closure shared variables and reinitialize types
    SharedVariableCollector collector = new SharedVariableCollector(getSourceUnit());
    collector.visitClosureExpression(expression);
    Set<VariableExpression> closureSharedExpressions = collector.getClosureSharedExpressions();
    Map<VariableExpression, ListHashMap> typesBeforeVisit = null;
    if (!closureSharedExpressions.isEmpty()) {
        typesBeforeVisit = new HashMap<VariableExpression, ListHashMap>();
        saveVariableExpressionMetadata(closureSharedExpressions, typesBeforeVisit);
    }
    // perform visit
    typeCheckingContext.pushEnclosingClosureExpression(expression);
    DelegationMetadata dmd = getDelegationMetadata(expression);
    if (dmd == null) {
        typeCheckingContext.delegationMetadata = new DelegationMetadata(typeCheckingContext.getEnclosingClassNode(), Closure.OWNER_FIRST, typeCheckingContext.delegationMetadata);
    } else {
        typeCheckingContext.delegationMetadata = new DelegationMetadata(dmd.getType(), dmd.getStrategy(), typeCheckingContext.delegationMetadata);
    }
    super.visitClosureExpression(expression);
    typeCheckingContext.delegationMetadata = typeCheckingContext.delegationMetadata.getParent();
    MethodNode node = new MethodNode("dummy", 0, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code);
    returnAdder.visitMethod(node);
    TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
    if (!enclosingClosure.getReturnTypes().isEmpty()) {
        ClassNode returnType = lowestUpperBound(enclosingClosure.getReturnTypes());
        storeInferredReturnType(expression, returnType);
        ClassNode inferredType = wrapClosureType(returnType);
        storeType(enclosingClosure.getClosureExpression(), inferredType);
    }
    typeCheckingContext.popEnclosingClosure();
    boolean typeChanged = isSecondPassNeededForControlStructure(varOrigType, oldTracker);
    if (typeChanged)
        visitClosureExpression(expression);
    // restore original metadata
    restoreVariableExpressionMetadata(typesBeforeVisit);
    typeCheckingContext.isInStaticContext = oldStaticContext;
    Parameter[] parameters = expression.getParameters();
    if (parameters != null) {
        for (Parameter parameter : parameters) {
            typeCheckingContext.controlStructureVariables.remove(parameter);
        }
    }
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) LinkedHashMap(java.util.LinkedHashMap) ListHashMap(org.codehaus.groovy.util.ListHashMap) HashMap(java.util.HashMap) 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) ListHashMap(org.codehaus.groovy.util.ListHashMap) MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 32 with Statement

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

the class SynchronizedASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    String value = getMemberStringValue(node, "value");
    if (parent instanceof MethodNode) {
        MethodNode mNode = (MethodNode) parent;
        if (mNode.isAbstract()) {
            addError("Error during " + MY_TYPE_NAME + " processing: annotation not allowed on abstract method '" + mNode.getName() + "'", mNode);
            return;
        }
        ClassNode cNode = mNode.getDeclaringClass();
        String lockExpr = determineLock(value, cNode, mNode);
        if (lockExpr == null)
            return;
        Statement origCode = mNode.getCode();
        Statement newCode = new SynchronizedStatement(varX(lockExpr), origCode);
        mNode.setCode(newCode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Statement(org.codehaus.groovy.ast.stmt.Statement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Example 33 with Statement

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

the class ToStringASTTransformation method appendValue.

private static void appendValue(BlockStatement body, Expression result, VariableExpression first, Expression value, String name, boolean includeNames, boolean ignoreNulls) {
    final BlockStatement thenBlock = new BlockStatement();
    final Statement appendValue = ignoreNulls ? ifS(notNullX(value), thenBlock) : thenBlock;
    appendCommaIfNotFirst(thenBlock, result, first);
    appendPrefix(thenBlock, result, name, includeNames);
    thenBlock.addStatement(ifElseS(sameX(value, new VariableExpression("this")), appendS(result, constX("(this)")), appendS(result, callX(INVOKER_TYPE, "toString", value))));
    body.addStatement(appendValue);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 34 with Statement

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

the class TupleConstructorASTTransformation method processArgsBlock.

private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) {
    BlockStatement block = new BlockStatement();
    for (PropertyNode pNode : cNode.getProperties()) {
        if (pNode.isStatic())
            continue;
        // if namedArgs.containsKey(propertyName) setProperty(propertyName, namedArgs.get(propertyName));
        Statement ifStatement = ifS(callX(namedArgs, "containsKey", constX(pNode.getName())), assignS(varX(pNode), propX(namedArgs, pNode.getName())));
        block.addStatement(ifStatement);
    }
    block.addStatement(stmt(callX(CHECK_METHOD_TYPE, "checkPropNames", args(varX("this"), namedArgs))));
    return block;
}
Also used : PropertyNode(org.codehaus.groovy.ast.PropertyNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 35 with Statement

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

the class GeneralUtils method block.

public static BlockStatement block(VariableScope varScope, Statement... stmts) {
    BlockStatement block = new BlockStatement();
    block.setVariableScope(varScope);
    for (Statement stmt : stmts) block.addStatement(stmt);
    return block;
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) 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