Search in sources :

Example 16 with VariableScope

use of org.codehaus.groovy.ast.VariableScope in project groovy by apache.

the class ClosureWriter method getClosureSharedVariables.

protected Parameter[] getClosureSharedVariables(ClosureExpression ce) {
    VariableScope scope = ce.getVariableScope();
    Parameter[] ret = new Parameter[scope.getReferencedLocalVariablesCount()];
    int index = 0;
    for (Iterator iter = scope.getReferencedLocalVariablesIterator(); iter.hasNext(); ) {
        Variable element = (org.codehaus.groovy.ast.Variable) iter.next();
        Parameter p = new Parameter(element.getType(), element.getName());
        p.setOriginType(element.getOriginType());
        p.setClosureSharedVariable(element.isClosureSharedVariable());
        ret[index] = p;
        index++;
    }
    return ret;
}
Also used : Variable(org.codehaus.groovy.ast.Variable) Iterator(java.util.Iterator) Parameter(org.codehaus.groovy.ast.Parameter) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 17 with VariableScope

use of org.codehaus.groovy.ast.VariableScope in project groovy by apache.

the class AutoCloneASTTransformation method createCloneSerialization.

private void createCloneSerialization(ClassNode cNode) {
    final BlockStatement body = new BlockStatement();
    // def baos = new ByteArrayOutputStream()
    final Expression baos = varX("baos");
    body.addStatement(declS(baos, ctorX(BAOS_TYPE)));
    // baos.withObjectOutputStream{ it.writeObject(this) }
    MethodCallExpression writeObject = callX(castX(OOS_TYPE, varX("it")), "writeObject", varX("this"));
    writeObject.setImplicitThis(false);
    ClosureExpression writeClos = closureX(block(stmt(writeObject)));
    writeClos.setVariableScope(new VariableScope());
    body.addStatement(stmt(callX(baos, "withObjectOutputStream", args(writeClos))));
    // def bais = new ByteArrayInputStream(baos.toByteArray())
    final Expression bais = varX("bais");
    body.addStatement(declS(bais, ctorX(BAIS_TYPE, args(callX(baos, "toByteArray")))));
    // return bais.withObjectInputStream(getClass().classLoader){ (<type>) it.readObject() }
    MethodCallExpression readObject = callX(castX(OIS_TYPE, varX("it")), "readObject");
    readObject.setImplicitThis(false);
    ClosureExpression readClos = closureX(block(stmt(castX(GenericsUtils.nonGeneric(cNode), readObject))));
    readClos.setVariableScope(new VariableScope());
    Expression classLoader = callX(callThisX("getClass"), "getClassLoader");
    body.addStatement(returnS(callX(bais, "withObjectInputStream", args(classLoader, readClos))));
    new VariableScopeVisitor(sourceUnit, true).visitClass(cNode);
    ClassNode[] exceptions = { make(CloneNotSupportedException.class) };
    cNode.addMethod("clone", ACC_PUBLIC, GenericsUtils.nonGeneric(cNode), Parameter.EMPTY_ARRAY, exceptions, body);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 18 with VariableScope

use of org.codehaus.groovy.ast.VariableScope in project gradle by gradle.

the class GradleResolveVisitor method transformVariableExpression.

protected Expression transformVariableExpression(VariableExpression ve) {
    visitAnnotations(ve);
    Variable v = ve.getAccessedVariable();
    if (!(v instanceof DynamicVariable) && !checkingVariableTypeInDeclaration) {
        /*
         *  GROOVY-4009: when a normal variable is simply being used, there is no need to try to
         *  resolve its type. Variable type resolve should proceed only if the variable is being declared.
         */
        return ve;
    }
    if (v instanceof DynamicVariable) {
        String name = ve.getName();
        ClassNode t = ClassHelper.make(name);
        // asking isResolved here allows to check if a primitive
        // type name like "int" was used to make t. In such a case
        // we have nothing left to do.
        boolean isClass = t.isResolved();
        if (!isClass) {
            // compiler skip the resolving at several places in this class.
            if (Character.isLowerCase(name.charAt(0))) {
                t = new LowerCaseClass(name);
            }
            isClass = resolve(t);
            if (!isClass) {
                isClass = resolveToNestedOfCurrent(t);
            }
        }
        if (isClass) {
            // for each parentscope too
            for (VariableScope scope = currentScope; scope != null && !scope.isRoot(); scope = scope.getParent()) {
                if (scope.isRoot()) {
                    break;
                }
                if (scope.removeReferencedClassVariable(ve.getName()) == null) {
                    break;
                }
            }
            ClassExpression ce = new ClassExpression(t);
            ce.setSourcePosition(ve);
            return ce;
        }
    }
    resolveOrFail(ve.getType(), ve);
    ClassNode origin = ve.getOriginType();
    if (origin != ve.getType()) {
        resolveOrFail(origin, ve);
    }
    return ve;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 19 with VariableScope

use of org.codehaus.groovy.ast.VariableScope in project gradle by gradle.

the class GradleResolveVisitor method visitBlockStatement.

public void visitBlockStatement(BlockStatement block) {
    VariableScope oldScope = currentScope;
    currentScope = block.getVariableScope();
    super.visitBlockStatement(block);
    currentScope = oldScope;
}
Also used : VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 20 with VariableScope

use of org.codehaus.groovy.ast.VariableScope 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

VariableScope (org.codehaus.groovy.ast.VariableScope)20 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)11 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)11 ClassNode (org.codehaus.groovy.ast.ClassNode)10 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)10 Expression (org.codehaus.groovy.ast.expr.Expression)10 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)9 Parameter (org.codehaus.groovy.ast.Parameter)8 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)8 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)7 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)7 ArrayList (java.util.ArrayList)6 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)6 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)6 MethodNode (org.codehaus.groovy.ast.MethodNode)5 Variable (org.codehaus.groovy.ast.Variable)5 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)5 Iterator (java.util.Iterator)4 List (java.util.List)4 FieldNode (org.codehaus.groovy.ast.FieldNode)4