Search in sources :

Example 41 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.

the class GradleResolveVisitor method transformInlineConstants.

// resolve constant-looking expressions statically (do here as gets transformed away later)
private Expression transformInlineConstants(Expression exp) {
    if (exp instanceof PropertyExpression) {
        PropertyExpression pe = (PropertyExpression) exp;
        if (pe.getObjectExpression() instanceof ClassExpression) {
            ClassExpression ce = (ClassExpression) pe.getObjectExpression();
            ClassNode type = ce.getType();
            if (type.isEnum()) {
                return exp;
            }
            FieldNode fn = type.getField(pe.getPropertyAsString());
            if (fn != null && !fn.isEnum() && fn.isStatic() && fn.isFinal()) {
                if (fn.getInitialValueExpression() instanceof ConstantExpression) {
                    return fn.getInitialValueExpression();
                }
            }
        }
    } else if (exp instanceof ListExpression) {
        ListExpression le = (ListExpression) exp;
        ListExpression result = new ListExpression();
        for (Expression e : le.getExpressions()) {
            result.addExpression(transformInlineConstants(e));
        }
        return result;
    } else if (exp instanceof AnnotationConstantExpression) {
        ConstantExpression ce = (ConstantExpression) exp;
        if (ce.getValue() instanceof AnnotationNode) {
            // replicate a little bit of AnnotationVisitor here
            // because we can't wait until later to do this
            AnnotationNode an = (AnnotationNode) ce.getValue();
            for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
                member.setValue(transformInlineConstants(member.getValue()));
            }
        }
    }
    return exp;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 42 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.

the class GradleResolveVisitor method checkThisAndSuperAsPropertyAccess.

private void checkThisAndSuperAsPropertyAccess(PropertyExpression expression) {
    if (expression.isImplicitThis()) {
        return;
    }
    String prop = expression.getPropertyAsString();
    if (prop == null) {
        return;
    }
    if (!prop.equals("this") && !prop.equals("super")) {
        return;
    }
    ClassNode type = expression.getObjectExpression().getType();
    if (expression.getObjectExpression() instanceof ClassExpression) {
        if (!(currentClass instanceof InnerClassNode) && !Traits.isTrait(type)) {
            addError("The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.", expression);
            return;
        }
        if (currentScope != null && !currentScope.isInStaticContext() && Traits.isTrait(type) && "super".equals(prop) && directlyImplementsTrait(type)) {
            return;
        }
        ClassNode iterType = currentClass;
        while (iterType != null) {
            if (iterType.equals(type)) {
                break;
            }
            iterType = iterType.getOuterClass();
        }
        if (iterType == null) {
            addError("The class '" + type.getName() + "' needs to be an outer class of '" + currentClass.getName() + "' when using '.this' or '.super'.", expression);
        }
        if ((currentClass.getModifiers() & Opcodes.ACC_STATIC) == 0) {
            return;
        }
        if (currentScope != null && !currentScope.isInStaticContext()) {
            return;
        }
        addError("The usage of 'Class.this' and 'Class.super' within static nested class '" + currentClass.getName() + "' is not allowed in a static context.", expression);
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 43 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.

the class GradleResolveVisitor method transformPropertyExpression.

protected Expression transformPropertyExpression(PropertyExpression pe) {
    boolean itlp = isTopLevelProperty;
    boolean ipe = inPropertyExpression;
    Expression objectExpression = pe.getObjectExpression();
    inPropertyExpression = true;
    isTopLevelProperty = objectExpression.getClass() != PropertyExpression.class;
    objectExpression = transform(objectExpression);
    // we handle the property part as if it were not part of the property
    inPropertyExpression = false;
    Expression property = transform(pe.getProperty());
    isTopLevelProperty = itlp;
    inPropertyExpression = ipe;
    boolean spreadSafe = pe.isSpreadSafe();
    PropertyExpression old = pe;
    pe = new PropertyExpression(objectExpression, property, pe.isSafe());
    pe.setSpreadSafe(spreadSafe);
    pe.setSourcePosition(old);
    String className = lookupClassName(pe);
    if (className != null) {
        ClassNode type = ClassHelper.make(className);
        if (resolve(type)) {
            Expression ret = new ClassExpression(type);
            ret.setSourcePosition(pe);
            return ret;
        }
    }
    if (objectExpression instanceof ClassExpression && pe.getPropertyAsString() != null) {
        // possibly an inner class
        ClassExpression ce = (ClassExpression) objectExpression;
        ClassNode type = new ConstructedNestedClass(ce.getType(), pe.getPropertyAsString());
        if (resolve(type, false, false, false)) {
            Expression ret = new ClassExpression(type);
            ret.setSourcePosition(ce);
            return ret;
        }
    }
    Expression ret = pe;
    checkThisAndSuperAsPropertyAccess(pe);
    if (isTopLevelProperty) {
        ret = correctClassClassChain(pe);
    }
    return ret;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 44 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy-core by groovy.

the class BinaryExpressionHelper method evaluateInstanceof.

private void evaluateInstanceof(BinaryExpression expression) {
    OperandStack operandStack = controller.getOperandStack();
    expression.getLeftExpression().visit(controller.getAcg());
    operandStack.box();
    Expression rightExp = expression.getRightExpression();
    ClassNode classType;
    if (rightExp instanceof ClassExpression) {
        ClassExpression classExp = (ClassExpression) rightExp;
        classType = classExp.getType();
    } else {
        throw new RuntimeException("Right hand side of the instanceof keyword must be a class name, not: " + rightExp);
    }
    String classInternalName = BytecodeHelper.getClassInternalName(classType);
    controller.getMethodVisitor().visitTypeInsn(INSTANCEOF, classInternalName);
    operandStack.replace(ClassHelper.boolean_TYPE);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 45 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy-core by groovy.

the class InvokeDynamicWriter method coerce.

@Override
public void coerce(ClassNode from, ClassNode target) {
    ClassNode wrapper = ClassHelper.getWrapper(target);
    makeIndyCall(invokeMethod, EmptyExpression.INSTANCE, false, false, "asType", new ClassExpression(wrapper));
    if (ClassHelper.boolean_TYPE.equals(target) || ClassHelper.Boolean_TYPE.equals(target)) {
        writeIndyCast(ClassHelper.OBJECT_TYPE, target);
    } else {
        BytecodeHelper.doCast(controller.getMethodVisitor(), wrapper);
        controller.getOperandStack().replace(wrapper);
        controller.getOperandStack().doGroovyCast(target);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Aggregations

ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)97 Expression (org.codehaus.groovy.ast.expr.Expression)59 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)57 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)55 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)52 ClassNode (org.codehaus.groovy.ast.ClassNode)48 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)41 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)40 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)37 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)33 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)31 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)27 MethodNode (org.codehaus.groovy.ast.MethodNode)18 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)17 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)17 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)17 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)16 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)16 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)16 FieldNode (org.codehaus.groovy.ast.FieldNode)15