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);
}
}
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));
}
}
Aggregations