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