Search in sources :

Example 16 with DynamicVariable

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

Aggregations

DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)16 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)12 Expression (org.codehaus.groovy.ast.expr.Expression)10 ClassNode (org.codehaus.groovy.ast.ClassNode)9 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)9 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)9 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)8 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)8 Variable (org.codehaus.groovy.ast.Variable)7 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)7 FieldNode (org.codehaus.groovy.ast.FieldNode)5 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)5 PropertyNode (org.codehaus.groovy.ast.PropertyNode)4 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)4 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)4 ClassCodeExpressionTransformer (org.codehaus.groovy.ast.ClassCodeExpressionTransformer)3 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)3 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)3 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)2