Search in sources :

Example 6 with AnnotationConstantExpression

use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.

the class ResolveVisitor method transformAnnotationConstantExpression.

protected Expression transformAnnotationConstantExpression(final AnnotationConstantExpression ace) {
    AnnotationNode an = (AnnotationNode) ace.getValue();
    ClassNode type = an.getClassNode();
    resolveOrFail(type, " for annotation", an);
    for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
        member.setValue(transform(member.getValue()));
    }
    return ace;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) 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) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with AnnotationConstantExpression

use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.

the class StaticImportVisitor method transform.

@Override
public Expression transform(Expression exp) {
    if (exp == null)
        return null;
    Class<? extends Expression> clazz = exp.getClass();
    if (clazz == VariableExpression.class) {
        return transformVariableExpression((VariableExpression) exp);
    }
    if (clazz == BinaryExpression.class) {
        return transformBinaryExpression((BinaryExpression) exp);
    }
    if (clazz == PropertyExpression.class) {
        return transformPropertyExpression((PropertyExpression) exp);
    }
    if (clazz == MethodCallExpression.class) {
        return transformMethodCallExpression((MethodCallExpression) exp);
    }
    if (exp instanceof ClosureExpression) {
        return transformClosureExpression((ClosureExpression) exp);
    }
    if (clazz == ConstructorCallExpression.class) {
        return transformConstructorCallExpression((ConstructorCallExpression) exp);
    }
    if (clazz == ArgumentListExpression.class) {
        Expression result = exp.transformExpression(this);
        if (foundArgs == null && inPropertyExpression) {
            foundArgs = result;
        }
        return result;
    }
    if (exp instanceof ConstantExpression) {
        Expression result = exp.transformExpression(this);
        if (foundConstant == null && inPropertyExpression) {
            foundConstant = result;
        }
        if (inAnnotation && exp instanceof AnnotationConstantExpression) {
            ConstantExpression ce = (ConstantExpression) result;
            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();
                Map<String, Expression> attributes = an.getMembers();
                for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
                    Expression attrExpr = transform(entry.getValue());
                    entry.setValue(attrExpr);
                }
            }
        }
        return result;
    }
    return exp.transformExpression(this);
}
Also used : PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ExpressionUtils.isSuperExpression(org.apache.groovy.ast.tools.ExpressionUtils.isSuperExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) 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) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) Map(java.util.Map)

Example 8 with AnnotationConstantExpression

use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.

the class AsmClassGenerator method visitAnnotationDefaultExpression.

private void visitAnnotationDefaultExpression(final AnnotationVisitor av, final ClassNode type, final Expression exp) {
    if (exp instanceof ClosureExpression) {
        ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) exp, ACC_PUBLIC);
        Type t = Type.getType(BytecodeHelper.getTypeDescription(closureClass));
        av.visit(null, t);
    } else if (type.isArray()) {
        AnnotationVisitor avl = av.visitArray(null);
        ClassNode componentType = type.getComponentType();
        if (exp instanceof ListExpression) {
            ListExpression list = (ListExpression) exp;
            for (Expression lExp : list.getExpressions()) {
                visitAnnotationDefaultExpression(avl, componentType, lExp);
            }
        } else {
            visitAnnotationDefaultExpression(avl, componentType, exp);
        }
    } else if (isPrimitiveType(type) || isStringType(type)) {
        ConstantExpression constExp = (ConstantExpression) exp;
        av.visit(null, constExp.getValue());
    } else if (isClassType(type)) {
        ClassNode clazz = exp.getType();
        Type t = Type.getType(BytecodeHelper.getTypeDescription(clazz));
        av.visit(null, t);
    } else if (type.isDerivedFrom(ClassHelper.Enum_Type)) {
        PropertyExpression pExp = (PropertyExpression) exp;
        ClassExpression cExp = (ClassExpression) pExp.getObjectExpression();
        String desc = BytecodeHelper.getTypeDescription(cExp.getType());
        String name = pExp.getPropertyAsString();
        av.visitEnum(null, desc, name);
    } else if (type.implementsInterface(ClassHelper.Annotation_TYPE)) {
        AnnotationConstantExpression avExp = (AnnotationConstantExpression) exp;
        AnnotationNode value = (AnnotationNode) avExp.getValue();
        AnnotationVisitor avc = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(avExp.getType()));
        visitAnnotationAttributes(value, avc);
    } else {
        throw new GroovyBugError("unexpected annotation type " + type.getName());
    }
    av.visitEnd();
}
Also used : InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ClassHelper.isStringType(org.codehaus.groovy.ast.ClassHelper.isStringType) GenericsType(org.codehaus.groovy.ast.GenericsType) ClassHelper.isObjectType(org.codehaus.groovy.ast.ClassHelper.isObjectType) ClassHelper.isClassType(org.codehaus.groovy.ast.ClassHelper.isClassType) ClassHelper.isPrimitiveType(org.codehaus.groovy.ast.ClassHelper.isPrimitiveType) Type(org.objectweb.asm.Type) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodReferenceExpression(org.codehaus.groovy.ast.expr.MethodReferenceExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) Expression(org.codehaus.groovy.ast.expr.Expression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodPointerExpression(org.codehaus.groovy.ast.expr.MethodPointerExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 9 with AnnotationConstantExpression

use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.

the class AnnotationVisitor method visitExpression.

protected void visitExpression(String attrName, Expression attrExp, ClassNode attrType) {
    if (attrType.isArray()) {
        // check needed as @Test(attr = {"elem"}) passes through the parser
        if (attrExp instanceof ListExpression) {
            ListExpression le = (ListExpression) attrExp;
            visitListExpression(attrName, le, attrType.getComponentType());
        } else if (attrExp instanceof ClosureExpression) {
            addError("Annotation list attributes must use Groovy notation [el1, el2]", attrExp);
        } else {
            // treat like a singleton list as per Java
            ListExpression listExp = new ListExpression();
            listExp.addExpression(attrExp);
            if (annotation != null) {
                annotation.setMember(attrName, listExp);
            }
            visitExpression(attrName, listExp, attrType);
        }
    } else if (ClassHelper.isPrimitiveType(attrType)) {
        visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), ClassHelper.getWrapper(attrType));
    } else if (isStringType(attrType)) {
        visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), ClassHelper.STRING_TYPE);
    } else if (isClassType(attrType)) {
        if (!(attrExp instanceof ClassExpression || attrExp instanceof ClosureExpression)) {
            addError("Only classes and closures can be used for attribute '" + attrName + "'", attrExp);
        }
    } else if (attrType.isDerivedFrom(ClassHelper.Enum_Type)) {
        if (attrExp instanceof PropertyExpression) {
            visitEnumExpression(attrName, (PropertyExpression) attrExp, attrType);
        } else if (attrExp instanceof ConstantExpression) {
            visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), attrType);
        } else {
            addError("Expected enum value for attribute " + attrName, attrExp);
        }
    } else if (isValidAnnotationClass(attrType)) {
        if (attrExp instanceof AnnotationConstantExpression) {
            visitAnnotationExpression(attrName, (AnnotationConstantExpression) attrExp, attrType);
        } else {
            addError("Expected annotation of type '" + attrType.getName() + "' for attribute " + attrName, attrExp);
        }
    } else {
        addError("Unexpected type " + attrType.getName(), attrExp);
    }
}
Also used : AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 10 with AnnotationConstantExpression

use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression in project groovy by apache.

the class Annotations method annotationValueToExpression.

private static Expression annotationValueToExpression(Object value, AsmReferenceResolver resolver) {
    if (value instanceof TypeWrapper) {
        ClassNode type = resolver.resolveClassNullable(Type.getType(((TypeWrapper) value).desc).getClassName());
        return type != null ? new ClassExpression(type) : null;
    }
    if (value instanceof EnumConstantWrapper) {
        EnumConstantWrapper wrapper = (EnumConstantWrapper) value;
        return new PropertyExpression(new ClassExpression(resolver.resolveType(Type.getType(wrapper.enumDesc))), wrapper.constant);
    }
    if (value instanceof AnnotationStub) {
        AnnotationNode annotationNode = createAnnotationNode((AnnotationStub) value, resolver);
        return annotationNode != null ? new AnnotationConstantExpression(annotationNode) : nullX();
    }
    if (value != null && value.getClass().isArray()) {
        ListExpression elementExprs = new ListExpression();
        int len = Array.getLength(value);
        for (int i = 0; i != len; ++i) {
            elementExprs.addExpression(annotationValueToExpression(Array.get(value, i), resolver));
        }
        return elementExprs;
    }
    if (value instanceof List) {
        ListExpression elementExprs = new ListExpression();
        for (Object o : (List) value) {
            elementExprs.addExpression(annotationValueToExpression(o, resolver));
        }
        return elementExprs;
    }
    return new ConstantExpression(value);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) List(java.util.List)

Aggregations

AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)14 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)12 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)12 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)12 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)11 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)11 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)10 ClassNode (org.codehaus.groovy.ast.ClassNode)9 Expression (org.codehaus.groovy.ast.expr.Expression)9 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)9 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)8 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)8 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)8 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)8 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)8 Map (java.util.Map)7 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)7 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)7 HashMap (java.util.HashMap)6 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)6