Search in sources :

Example 1 with AnnotationConstantExpression

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

the class AsmClassGenerator method visitAnnotationArrayElement.

private void visitAnnotationArrayElement(final Expression expr, final int arrayElementType, final AnnotationVisitor av) {
    switch(arrayElementType) {
        case 1:
            AnnotationNode atAttr = (AnnotationNode) ((AnnotationConstantExpression) expr).getValue();
            AnnotationVisitor av2 = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(atAttr.getClassNode()));
            visitAnnotationAttributes(atAttr, av2);
            av2.visitEnd();
            break;
        case 2:
            av.visit(null, ((ConstantExpression) expr).getValue());
            break;
        case 3:
            ClassNode type = expr.getType();
            if (expr instanceof ClosureExpression) {
                type = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) expr, ACC_PUBLIC);
            }
            av.visit(null, Type.getType(BytecodeHelper.getTypeDescription(type)));
            break;
        case 4:
            PropertyExpression propExpr = (PropertyExpression) expr;
            av.visitEnum(null, BytecodeHelper.getTypeDescription(propExpr.getObjectExpression().getType()), String.valueOf(((ConstantExpression) propExpr.getProperty()).getValue()));
            break;
    }
}
Also used : InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 2 with AnnotationConstantExpression

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

the class AnnotationVisitor method checkCircularReference.

public void checkCircularReference(ClassNode searchClass, ClassNode attrType, Expression startExp) {
    if (!isValidAnnotationClass(attrType))
        return;
    if (!(startExp instanceof AnnotationConstantExpression)) {
        addError("Found '" + startExp.getText() + "' when expecting an Annotation Constant", startExp);
        return;
    }
    AnnotationConstantExpression ace = (AnnotationConstantExpression) startExp;
    AnnotationNode annotationNode = (AnnotationNode) ace.getValue();
    if (annotationNode.getClassNode().equals(searchClass)) {
        addError("Circular reference discovered in " + searchClass.getName(), startExp);
        return;
    }
    ClassNode cn = annotationNode.getClassNode();
    for (MethodNode method : cn.getMethods()) {
        if (method.getReturnType().equals(searchClass)) {
            addError("Circular reference discovered in " + cn.getName(), startExp);
        }
        ReturnStatement code = (ReturnStatement) method.getCode();
        if (code == null)
            continue;
        checkCircularReference(searchClass, method.getReturnType(), code.getExpression());
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 3 with AnnotationConstantExpression

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

the class AnnotationCollectorTransform method makeExpression.

private static Expression makeExpression(Object o) {
    if (o instanceof Class) {
        return new ClassExpression(ClassHelper.make((Class<?>) o));
    }
    // TODO: value as Annotation here!
    if (o instanceof Object[][]) {
        List<AnnotationNode> annotations = makeListOfAnnotations((Object[][]) o);
        ListExpression le = new ListExpression();
        for (AnnotationNode an : annotations) {
            le.addExpression(new AnnotationConstantExpression(an));
        }
        return le;
    } else if (o instanceof Object[]) {
        ListExpression le = new ListExpression();
        Object[] values = (Object[]) o;
        for (Object val : values) {
            le.addExpression(makeExpression(val));
        }
        return le;
    }
    return new ConstantExpression(o, true);
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) 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)

Example 4 with AnnotationConstantExpression

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

the class StaticTypeCheckingVisitor method checkNamedParamsAnnotation.

private void checkNamedParamsAnnotation(final Parameter param, final MapExpression args) {
    if (!isOrImplements(param.getType(), MAP_TYPE))
        return;
    List<MapEntryExpression> entryExpressions = args.getMapEntryExpressions();
    Map<Object, Expression> entries = new LinkedHashMap<>();
    for (MapEntryExpression entry : entryExpressions) {
        Object key = entry.getKeyExpression();
        if (key instanceof ConstantExpression) {
            key = ((ConstantExpression) key).getValue();
        }
        entries.put(key, entry.getValueExpression());
    }
    List<String> collectedNames = new ArrayList<>();
    List<AnnotationNode> annotations = param.getAnnotations(NAMED_PARAMS_CLASSNODE);
    if (annotations != null && !annotations.isEmpty()) {
        AnnotationNode an = null;
        for (AnnotationNode next : annotations) {
            if (next.getClassNode().getName().equals(NamedParams.class.getName())) {
                an = next;
            }
        }
        if (an != null) {
            Expression value = an.getMember("value");
            if (value instanceof AnnotationConstantExpression) {
                processNamedParam((AnnotationConstantExpression) value, entries, args, collectedNames);
            } else if (value instanceof ListExpression) {
                ListExpression le = (ListExpression) value;
                for (Expression next : le.getExpressions()) {
                    if (next instanceof AnnotationConstantExpression) {
                        processNamedParam((AnnotationConstantExpression) next, entries, args, collectedNames);
                    }
                }
            }
        }
    }
    annotations = param.getAnnotations(NAMED_PARAM_CLASSNODE);
    if (annotations != null && !annotations.isEmpty()) {
        for (AnnotationNode next : annotations) {
            if (next.getClassNode().getName().equals(NamedParam.class.getName())) {
                processNamedParam(next, entries, args, collectedNames);
            }
        }
    }
    if (!collectedNames.isEmpty()) {
        for (Map.Entry<Object, Expression> entry : entries.entrySet()) {
            if (!collectedNames.contains(entry.getKey())) {
                addStaticTypeError("unexpected named arg: " + entry.getKey(), args);
            }
        }
    }
}
Also used : NamedParams(groovy.transform.NamedParams) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayList(java.util.ArrayList) NamedParam(groovy.transform.NamedParam) StaticTypeCheckingSupport.toMethodParametersString(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.toMethodParametersString) StaticTypeCheckingSupport.isParameterizedWithString(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isParameterizedWithString) StaticTypeCheckingSupport.isParameterizedWithGStringOrGStringString(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isParameterizedWithGStringOrGStringString) LinkedHashMap(java.util.LinkedHashMap) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) 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) StaticTypeCheckingSupport.evaluateExpression(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.evaluateExpression) 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) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) 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) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Collectors.toMap(java.util.stream.Collectors.toMap) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap)

Example 5 with AnnotationConstantExpression

use of org.codehaus.groovy.ast.expr.AnnotationConstantExpression 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) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

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