View Javadoc
1   /*
2    * Copyright 2009 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.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   * Base implementation class to be used for implementing {@link AnnotationProcessingService}s. 
30   * 
31   * @see AnnotationProcessingService
32   * @author Alexander Eibner, Peter Schmidt
33   * @since 1.3.1
34   */
35  public class BaseTargetAwareAnnotationProcessingService extends
36          BaseAnnotationProcessingService implements
37          TargetAwareAnnotationProcessingService {
38  
39      /**
40       * List of target aware annotation processors.
41       */
42      private Map<Class<? extends Annotation>, TargetAwareAnnotationProcessor> _taAnnotationProcessorMap = new HashMap<Class<? extends Annotation>, TargetAwareAnnotationProcessor>();
43  
44      /**
45       * {@inheritDoc}
46       * 
47       * @see org.castor.annotationprocessing.AnnotationProcessingService#
48       *      addAnnotationProcessor(AnnotationProcessor)
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       * {@inheritDoc}
60       * 
61       * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessingService#getAllAnnotationProcessors()
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       * {@inheritDoc}
72       * 
73       * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessingService#getTargetAwareAnnotationProcessors()
74       */
75      public Set<TargetAwareAnnotationProcessor> getTargetAwareAnnotationProcessors() {
76          Set<TargetAwareAnnotationProcessor> result = new HashSet<TargetAwareAnnotationProcessor>(
77                  this._taAnnotationProcessorMap.values());
78          return result;
79      }
80  
81      /**
82       * {@inheritDoc}
83       * 
84       * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessingService#processAnnotation(org.castor.core.nature.BaseNature,
85       *      java.lang.annotation.Annotation, java.lang.reflect.AnnotatedElement)
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      * {@inheritDoc}
102      * 
103      * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessingService#processAnnotations(org.castor.core.nature.BaseNature,
104      *      java.lang.annotation.Annotation[],
105      *      java.lang.reflect.AnnotatedElement)
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      * This method acts like it's super method, but also tries to process the
124      * annotation with the {@link TargetAwareAnnotationProcessor}s.
125      * {@inheritDoc}
126      * 
127      * @see org.castor.core.annotationprocessing.BaseAnnotationProcessingService#processAnnotation(org.castor.core.nature.BaseNature,
128      *      java.lang.annotation.Annotation)
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      * This method acts like it's super method, but also tries to process the
149      * annotations with the {@link TargetAwareAnnotationProcessor}s.
150      * {@inheritDoc}
151      * 
152      * @see org.castor.core.annotationprocessing.BaseAnnotationProcessingService#processAnnotations(org.castor.core.nature.BaseNature,
153      *      java.lang.annotation.Annotation)
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 }