01: package de.schlund.pfixcore.util;
02:
03: import java.util.ArrayList;
04: import java.util.Collection;
05: import java.util.List;
06: import java.util.Set;
07:
08: import com.sun.mirror.apt.AnnotationProcessor;
09: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
10: import com.sun.mirror.apt.AnnotationProcessorFactory;
11: import com.sun.mirror.apt.AnnotationProcessors;
12: import com.sun.mirror.declaration.AnnotationTypeDeclaration;
13:
14: import de.schlund.pfixcore.generator.iwrpgen.IWrapperAnnotationProcessorFactory;
15:
16: public class CommonAnnotationProcessorFactory implements
17: AnnotationProcessorFactory {
18:
19: List<AnnotationProcessorFactory> factories;
20:
21: public CommonAnnotationProcessorFactory() {
22: factories = new ArrayList<AnnotationProcessorFactory>();
23: factories.add(new IWrapperAnnotationProcessorFactory());
24: }
25:
26: public AnnotationProcessor getProcessorFor(
27: Set<AnnotationTypeDeclaration> decls,
28: AnnotationProcessorEnvironment env) {
29: AnnotationProcessor result = AnnotationProcessors.NO_OP;
30: StringBuilder sb = new StringBuilder();
31: for (AnnotationTypeDeclaration decl : decls)
32: sb.append(decl.getQualifiedName() + " ");
33: env.getMessager().printNotice(
34: "Requested annotations: " + sb.toString());
35: if (!decls.isEmpty()) {
36: for (AnnotationProcessorFactory factory : factories) {
37: Collection<String> supTypes = factory
38: .supportedAnnotationTypes();
39: boolean matches = true;
40: for (AnnotationTypeDeclaration decl : decls) {
41: if (!supTypes.contains(decl.getQualifiedName())) {
42: matches = false;
43: break;
44: }
45: }
46: if (matches) {
47: env.getMessager().printNotice(
48: "Matching factory: "
49: + factory.getClass().getName());
50: result = factory.getProcessorFor(decls, env);
51: break;
52: }
53: }
54: }
55: if (result == AnnotationProcessors.NO_OP)
56: env.getMessager().printNotice("No matching factory");
57: return result;
58: }
59:
60: public Collection<String> supportedOptions() {
61: List<String> list = new ArrayList<String>();
62: for (AnnotationProcessorFactory factory : factories) {
63: list.addAll(factory.supportedOptions());
64: }
65: return list;
66: }
67:
68: public Collection<String> supportedAnnotationTypes() {
69: List<String> list = new ArrayList<String>();
70: for (AnnotationProcessorFactory factory : factories) {
71: list.addAll(factory.supportedAnnotationTypes());
72: }
73: return list;
74: }
75:
76: }
|