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