View Javadoc
1   /*
2    * Copyright 2009 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.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   * Base implementation class to be used for implementing {@link AnnotationProcessingService}s.
28   * 
29   * @see AnnotationProcessingService
30   * @author Alexander Eibner, Peter Schmidt
31   * @since 1.3.1
32   */
33  public class BaseTargetAwareAnnotationProcessingService extends BaseAnnotationProcessingService
34      implements TargetAwareAnnotationProcessingService {
35  
36    /**
37     * List of target aware annotation processors.
38     */
39    private Map<Class<? extends Annotation>, TargetAwareAnnotationProcessor> _taAnnotationProcessorMap =
40        new HashMap<Class<? extends Annotation>, TargetAwareAnnotationProcessor>();
41  
42    /**
43     * {@inheritDoc}
44     * 
45     * @see org.castor.annotationprocessing.AnnotationProcessingService#
46     *      addAnnotationProcessor(AnnotationProcessor)
47     */
48    public void addAnnotationProcessor(final TargetAwareAnnotationProcessor taAnnotationProcessor) {
49      if (taAnnotationProcessor != null) {
50        _taAnnotationProcessorMap.put(taAnnotationProcessor.forAnnotationClass(),
51            taAnnotationProcessor);
52      }
53    }
54  
55    /**
56     * {@inheritDoc}
57     * 
58     * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessingService#getAllAnnotationProcessors()
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     * {@inheritDoc}
69     * 
70     * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessingService#getTargetAwareAnnotationProcessors()
71     */
72    public Set<TargetAwareAnnotationProcessor> getTargetAwareAnnotationProcessors() {
73      Set<TargetAwareAnnotationProcessor> result =
74          new HashSet<TargetAwareAnnotationProcessor>(this._taAnnotationProcessorMap.values());
75      return result;
76    }
77  
78    /**
79     * {@inheritDoc}
80     * 
81     * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessingService#processAnnotation(org.castor.core.nature.BaseNature,
82     *      java.lang.annotation.Annotation, java.lang.reflect.AnnotatedElement)
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     * {@inheritDoc}
98     * 
99     * @see org.castor.core.annotationprocessing.TargetAwareAnnotationProcessingService#processAnnotations(org.castor.core.nature.BaseNature,
100    *      java.lang.annotation.Annotation[], java.lang.reflect.AnnotatedElement)
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    * This method acts like it's super method, but also tries to process the annotation with the
119    * {@link TargetAwareAnnotationProcessor}s. {@inheritDoc}
120    * 
121    * @see org.castor.core.annotationprocessing.BaseAnnotationProcessingService#processAnnotation(org.castor.core.nature.BaseNature,
122    *      java.lang.annotation.Annotation)
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    * This method acts like it's super method, but also tries to process the annotations with the
142    * {@link TargetAwareAnnotationProcessor}s. {@inheritDoc}
143    * 
144    * @see org.castor.core.annotationprocessing.BaseAnnotationProcessingService#processAnnotations(org.castor.core.nature.BaseNature,
145    *      java.lang.annotation.Annotation)
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 }