1 /*
2 * Copyright 2007 Werner Guttmann
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.castor.core.annotationprocessing;
17
18 import java.lang.annotation.Annotation;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.castor.core.nature.BaseNature;
26
27 /**
28 * An AnnotationProcessingService handles multiple {@link AnnotationProcessor}s
29 * and uses them to process one or more {@link Annotation}s. This is a standard
30 * implementation that should be sufficient for most purposes.
31 *
32 * @see AnnotationProcessingService
33 * @author Alexander Eibner, Peter Schmidt
34 * @since 1.3
35 */
36
37 public class BaseAnnotationProcessingService implements
38 AnnotationProcessingService {
39
40 /**
41 * List of annotation processors.
42 */
43 private Map<Class<? extends Annotation>, AnnotationProcessor> _annotationProcessorMap = new HashMap<Class<? extends Annotation>, AnnotationProcessor>();
44
45 /**
46 * {@inheritDoc}
47 *
48 * @see org.castor.annotationprocessing.AnnotationProcessingService#
49 * addAnnotationProcessor(AnnotationProcessor)
50 */
51 public void addAnnotationProcessor(
52 final AnnotationProcessor annotationProcessor) {
53 if (annotationProcessor != null) {
54 _annotationProcessorMap.put(annotationProcessor
55 .forAnnotationClass(), annotationProcessor);
56 }
57 }
58
59 /**
60 * {@inheritDoc}
61 *
62 * @see org.castor.annotationprocessing.AnnotationProcessingService#
63 * getAnnotationProcessors()
64 */
65 public Set<AnnotationProcessor> getAnnotationProcessors() {
66 return new HashSet<AnnotationProcessor>(_annotationProcessorMap
67 .values());
68 }
69
70 /**
71 * {@inheritDoc}
72 *
73 * @see org.castor.annotationprocessing.AnnotationProcessingService#
74 * processAnnotations(BaseNature, Annotation[])
75 */
76 public <I extends BaseNature> Annotation[] processAnnotations(I info,
77 final Annotation[] annotations) {
78 ArrayList<Annotation> unprocessed = new ArrayList<Annotation>();
79
80 for (int i = 0; i < annotations.length; i++) {
81 if (processAnnotation(info, annotations[i]) == false) {
82 unprocessed.add(annotations[i]);
83 }
84 }
85
86 Annotation[] arrReturn = new Annotation[unprocessed.size()];
87
88 return unprocessed.toArray(arrReturn);
89 }
90
91 /**
92 * {@inheritDoc}
93 *
94 * @see org.castor.annotationprocessing.AnnotationProcessingService#processAnnotation
95 * (BaseNature, Annotation)
96 */
97 public <I extends BaseNature, A extends Annotation> boolean processAnnotation(
98 I info, final A annotation) {
99 boolean processed = false;
100 AnnotationProcessor annotationProcessor = _annotationProcessorMap
101 .get(annotation.annotationType());
102 if (annotationProcessor != null) {
103 processed = annotationProcessor.processAnnotation(info, annotation);
104 }
105 return processed;
106 }
107 }