Search in sources :

Example 11 with AnnotationConstantExpression

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

the class AsmClassGenerator method determineCommonArrayType.

private static int determineCommonArrayType(final List<Expression> values) {
    Expression expr = values.get(0);
    int arrayElementType = -1;
    if (expr instanceof AnnotationConstantExpression) {
        arrayElementType = 1;
    } else if (expr instanceof ConstantExpression) {
        arrayElementType = 2;
    } else if (expr instanceof ClassExpression || expr instanceof ClosureExpression) {
        arrayElementType = 3;
    } else if (expr instanceof PropertyExpression) {
        arrayElementType = 4;
    }
    return arrayElementType;
}
Also used : 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) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 12 with AnnotationConstantExpression

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

the class AsmClassGenerator method visitAnnotationAttributes.

/**
 * Generates the annotation attributes.
 *
 * @param an the node with an annotation
 * @param av the visitor to use
 */
private void visitAnnotationAttributes(final AnnotationNode an, final AnnotationVisitor av) {
    Map<String, Object> constantAttrs = new HashMap<>();
    Map<String, PropertyExpression> enumAttrs = new HashMap<>();
    Map<String, Object> atAttrs = new HashMap<>();
    Map<String, ListExpression> arrayAttrs = new HashMap<>();
    for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
        String name = member.getKey();
        Expression expr = member.getValue();
        if (expr instanceof AnnotationConstantExpression) {
            atAttrs.put(name, ((AnnotationConstantExpression) expr).getValue());
        } else if (expr instanceof ConstantExpression) {
            constantAttrs.put(name, ((ConstantExpression) expr).getValue());
        } else if (expr instanceof ClassExpression) {
            constantAttrs.put(name, Type.getType(BytecodeHelper.getTypeDescription((expr.getType()))));
        } else if (expr instanceof PropertyExpression) {
            enumAttrs.put(name, (PropertyExpression) expr);
        } else if (expr instanceof ListExpression) {
            arrayAttrs.put(name, (ListExpression) expr);
        } else if (expr instanceof ClosureExpression) {
            ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) expr, ACC_PUBLIC);
            constantAttrs.put(name, Type.getType(BytecodeHelper.getTypeDescription(closureClass)));
        }
    }
    for (Map.Entry<String, Object> entry : constantAttrs.entrySet()) {
        av.visit(entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, PropertyExpression> entry : enumAttrs.entrySet()) {
        PropertyExpression propExp = entry.getValue();
        av.visitEnum(entry.getKey(), BytecodeHelper.getTypeDescription(propExp.getObjectExpression().getType()), String.valueOf(((ConstantExpression) propExp.getProperty()).getValue()));
    }
    for (Map.Entry<String, Object> entry : atAttrs.entrySet()) {
        AnnotationNode atNode = (AnnotationNode) entry.getValue();
        AnnotationVisitor av2 = av.visitAnnotation(entry.getKey(), BytecodeHelper.getTypeDescription(atNode.getClassNode()));
        visitAnnotationAttributes(atNode, av2);
        av2.visitEnd();
    }
    visitArrayAttributes(an, arrayAttrs, av);
}
Also used : InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) HashMap(java.util.HashMap) 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) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) 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) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with AnnotationConstantExpression

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

the class ExtendedVerifier method processDuplicateAnnotationContainers.

private void processDuplicateAnnotationContainers(AnnotatedNode node, Map<String, List<AnnotationNode>> nonSourceAnnotations) {
    for (Map.Entry<String, List<AnnotationNode>> next : nonSourceAnnotations.entrySet()) {
        if (next.getValue().size() > 1) {
            ClassNode repeatable = null;
            AnnotationNode repeatee = next.getValue().get(0);
            for (AnnotationNode anno : repeatee.getClassNode().getAnnotations()) {
                if (anno.getClassNode().getName().equals("java.lang.annotation.Repeatable")) {
                    Expression value = anno.getMember("value");
                    if (value instanceof ClassExpression && value.getType().isAnnotationDefinition()) {
                        repeatable = value.getType();
                        break;
                    }
                }
            }
            if (repeatable != null) {
                if (nonSourceAnnotations.containsKey(repeatable.getName())) {
                    addError("Cannot specify duplicate annotation on the same member. Explicit " + repeatable.getName() + " found when creating implicit container for " + next.getKey(), node);
                }
                AnnotationNode collector = new AnnotationNode(repeatable);
                if (repeatee.hasRuntimeRetention()) {
                    collector.setRuntimeRetention(true);
                } else if (repeatable.isResolved()) {
                    Class<?> repeatableType = repeatable.getTypeClass();
                    Retention retention = repeatableType.getAnnotation(Retention.class);
                    collector.setRuntimeRetention(retention != null && retention.value().equals(RetentionPolicy.RUNTIME));
                } else {
                    for (AnnotationNode annotation : repeatable.getAnnotations()) {
                        if (annotation.getClassNode().getName().equals("java.lang.annotation.Retention")) {
                            Expression value = annotation.getMember("value");
                            assert value != null;
                            Object retention = evaluateExpression(value, source.getConfiguration());
                            collector.setRuntimeRetention(retention != null && retention.toString().equals("RUNTIME"));
                            break;
                        }
                    }
                }
                collector.addMember("value", new ListExpression(next.getValue().stream().map(AnnotationConstantExpression::new).collect(Collectors.toList())));
                node.getAnnotations().removeAll(next.getValue());
                node.addAnnotation(collector);
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) StaticTypeCheckingSupport.evaluateExpression(org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.evaluateExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) Expression(org.codehaus.groovy.ast.expr.Expression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) List(java.util.List) ArrayList(java.util.ArrayList) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Retention(java.lang.annotation.Retention) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with AnnotationConstantExpression

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

the class GradleResolveVisitor method transformAnnotationConstantExpression.

@Override
protected Expression transformAnnotationConstantExpression(AnnotationConstantExpression ace) {
    AnnotationNode an = (AnnotationNode) ace.getValue();
    ClassNode type = an.getClassNode();
    resolveOrFail(type, ", unable to find class 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) 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