1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.castor.core.annotationprocessing;
15
16 import java.lang.annotation.Annotation;
17 import java.lang.reflect.AnnotatedElement;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.castor.core.nature.BaseNature;
25
26
27
28
29
30
31
32
33 public class BaseTargetAwareAnnotationProcessingService extends BaseAnnotationProcessingService
34 implements TargetAwareAnnotationProcessingService {
35
36
37
38
39 private Map<Class<? extends Annotation>, TargetAwareAnnotationProcessor> _taAnnotationProcessorMap =
40 new HashMap<Class<? extends Annotation>, TargetAwareAnnotationProcessor>();
41
42
43
44
45
46
47
48 public void addAnnotationProcessor(final TargetAwareAnnotationProcessor taAnnotationProcessor) {
49 if (taAnnotationProcessor != null) {
50 _taAnnotationProcessorMap.put(taAnnotationProcessor.forAnnotationClass(),
51 taAnnotationProcessor);
52 }
53 }
54
55
56
57
58
59
60 public Set<AnnotationProcessor> getAllAnnotationProcessors() {
61 Set<AnnotationProcessor> result =
62 new HashSet<AnnotationProcessor>(super.getAnnotationProcessors());
63 result.addAll(this._taAnnotationProcessorMap.values());
64 return result;
65 }
66
67
68
69
70
71
72 public Set<TargetAwareAnnotationProcessor> getTargetAwareAnnotationProcessors() {
73 Set<TargetAwareAnnotationProcessor> result =
74 new HashSet<TargetAwareAnnotationProcessor>(this._taAnnotationProcessorMap.values());
75 return result;
76 }
77
78
79
80
81
82
83
84 public <I extends BaseNature, A extends Annotation> boolean processAnnotation(I info,
85 A annotation, AnnotatedElement target) throws AnnotationTargetException {
86 boolean processed = false;
87 TargetAwareAnnotationProcessor annotationProcessor =
88 _taAnnotationProcessorMap.get(annotation.annotationType());
89 if (annotationProcessor != null) {
90 processed = annotationProcessor.processAnnotation(info, annotation, target);
91 }
92 return processed;
93
94 }
95
96
97
98
99
100
101
102 public <I extends BaseNature> Annotation[] processAnnotations(I info, Annotation[] annotations,
103 AnnotatedElement target) throws AnnotationTargetException {
104 ArrayList<Annotation> unprocessed = new ArrayList<Annotation>();
105
106 for (int i = 0; i < annotations.length; i++) {
107 if (processAnnotation(info, annotations[i], target) == false) {
108 unprocessed.add(annotations[i]);
109 }
110 }
111
112 Annotation[] arrReturn = new Annotation[unprocessed.size()];
113
114 return unprocessed.toArray(arrReturn);
115 }
116
117
118
119
120
121
122
123
124 @Override
125 public <I extends BaseNature, A extends Annotation> boolean processAnnotation(I info,
126 A annotation) {
127 boolean superReturn = super.processAnnotation(info, annotation);
128 if (!superReturn) {
129 boolean processed = false;
130 AnnotationProcessor annotationProcessor =
131 _taAnnotationProcessorMap.get(annotation.annotationType());
132 if (annotationProcessor != null) {
133 processed = annotationProcessor.processAnnotation(info, annotation);
134 }
135 return processed;
136 }
137 return superReturn;
138 }
139
140
141
142
143
144
145
146
147 @Override
148 public <I extends BaseNature> Annotation[] processAnnotations(I info, Annotation[] annotations) {
149 Annotation[] superUnprocessed = super.processAnnotations(info, annotations);
150
151 ArrayList<Annotation> unprocessed = new ArrayList<Annotation>();
152
153 for (int i = 0; i < superUnprocessed.length; i++) {
154 if (processAnnotation(info, superUnprocessed[i]) == false) {
155 unprocessed.add(superUnprocessed[i]);
156 }
157 }
158
159 Annotation[] arrReturn = new Annotation[unprocessed.size()];
160
161 return unprocessed.toArray(arrReturn);
162 }
163
164 }