Search in sources :

Example 1 with AnnotationCollectorMode

use of groovy.transform.AnnotationCollectorMode in project groovy by apache.

the class ASTTransformationCollectorCodeVisitor method visitAnnotations.

/**
     * If the annotation is annotated with {@link GroovyASTTransformation}
     * the annotation is added to <code>stageVisitors</code> at the appropriate processor visitor.
     *
     * @param node the node to process
     */
public void visitAnnotations(AnnotatedNode node) {
    super.visitAnnotations(node);
    Map<Integer, List<AnnotationNode>> existing = new TreeMap<Integer, List<AnnotationNode>>();
    Map<Integer, List<AnnotationNode>> replacements = new LinkedHashMap<Integer, List<AnnotationNode>>();
    Map<Integer, AnnotationCollectorMode> modes = new LinkedHashMap<Integer, AnnotationCollectorMode>();
    int index = 0;
    for (AnnotationNode annotation : node.getAnnotations()) {
        findCollectedAnnotations(annotation, node, index, modes, existing, replacements);
        index++;
    }
    for (Integer replacementIndex : replacements.keySet()) {
        mergeCollectedAnnotations(modes.get(replacementIndex), existing, replacements.get(replacementIndex));
        existing.put(replacementIndex, replacements.get(replacementIndex));
    }
    List<AnnotationNode> mergedList = new ArrayList<AnnotationNode>();
    for (List<AnnotationNode> next : existing.values()) {
        mergedList.addAll(next);
    }
    node.getAnnotations().clear();
    node.getAnnotations().addAll(mergedList);
    for (AnnotationNode annotation : node.getAnnotations()) {
        Annotation transformClassAnnotation = getTransformClassAnnotation(annotation.getClassNode());
        if (transformClassAnnotation == null) {
            // skip if there is no such annotation
            continue;
        }
        addTransformsToClassNode(annotation, transformClassAnnotation);
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) AnnotationCollectorMode(groovy.transform.AnnotationCollectorMode) Annotation(java.lang.annotation.Annotation) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with AnnotationCollectorMode

use of groovy.transform.AnnotationCollectorMode in project groovy by apache.

the class ASTTransformationCollectorCodeVisitor method findCollectedAnnotations.

private void findCollectedAnnotations(AnnotationNode aliasNode, AnnotatedNode origin, Integer index, Map<Integer, AnnotationCollectorMode> modes, Map<Integer, List<AnnotationNode>> existing, Map<Integer, List<AnnotationNode>> replacements) {
    ClassNode classNode = aliasNode.getClassNode();
    for (AnnotationNode annotation : classNode.getAnnotations()) {
        if (annotation.getClassNode().getName().equals(AnnotationCollector.class.getName())) {
            AnnotationCollectorMode mode = getMode(annotation);
            if (mode == null) {
                mode = AnnotationCollectorMode.DUPLICATE;
            }
            modes.put(index, mode);
            Expression processorExp = annotation.getMember("processor");
            AnnotationCollectorTransform act = null;
            assertStringConstant(processorExp);
            if (processorExp != null) {
                String className = (String) ((ConstantExpression) processorExp).getValue();
                Class klass = loadTransformClass(className, aliasNode);
                if (klass != null) {
                    try {
                        act = (AnnotationCollectorTransform) klass.newInstance();
                    } catch (InstantiationException e) {
                        source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
                    } catch (IllegalAccessException e) {
                        source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
                    }
                }
            } else {
                act = new AnnotationCollectorTransform();
            }
            if (act != null) {
                replacements.put(index, act.visit(annotation, aliasNode, origin, source));
                return;
            }
        }
    }
    if (!replacements.containsKey(index)) {
        existing.put(index, Collections.singletonList(aliasNode));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) AnnotationCollector(groovy.transform.AnnotationCollector) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotationCollectorMode(groovy.transform.AnnotationCollectorMode)

Aggregations

AnnotationCollectorMode (groovy.transform.AnnotationCollectorMode)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 AnnotationCollector (groovy.transform.AnnotationCollector)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 ClassNode (org.codehaus.groovy.ast.ClassNode)1 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)1 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)1 Expression (org.codehaus.groovy.ast.expr.Expression)1 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)1 ExceptionMessage (org.codehaus.groovy.control.messages.ExceptionMessage)1