KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > InheritableAnnotationHolder


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.reflect.plugins;
23
24 import java.io.Serializable JavaDoc;
25 import java.lang.annotation.Inherited JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.jboss.reflect.spi.AnnotatedInfo;
31 import org.jboss.reflect.spi.AnnotationValue;
32 import org.jboss.util.JBossObject;
33
34 /**
35  * An annotation holder
36  *
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
39  */

40 public abstract class InheritableAnnotationHolder extends JBossObject implements AnnotatedInfo, Serializable JavaDoc
41 {
42    /** serialVersionUID */
43    private static final long serialVersionUID = 3257290210164289843L;
44    
45    /** The classname of the <code>@Inherited</code> annotation, this needs retroing to work on JDK 1.4 */
46    private static final String JavaDoc INHERITED_NAME = Inherited JavaDoc.class.getName();//This
47

48    /** Unknown annotations map */
49    static final Map JavaDoc<String JavaDoc, AnnotationValue> UNKNOWN_ANNOTATIONS_MAP = Collections.unmodifiableMap(new HashMap JavaDoc<String JavaDoc, AnnotationValue>());
50    
51    /** Unknown annotations */
52    static final AnnotationValue[] UNKNOWN_ANNOTATIONS = new AnnotationValue[0];
53    
54    /** Declared annotations Map<String, AnnotationValue> */
55    protected Map JavaDoc<String JavaDoc, AnnotationValue> declaredAnnotations = UNKNOWN_ANNOTATIONS_MAP;
56    
57    /** All annotations Map<String, AnnotationValue> */
58    protected Map JavaDoc<String JavaDoc, AnnotationValue> allAnnotations = UNKNOWN_ANNOTATIONS_MAP;
59    
60    /** All annotations */
61    protected AnnotationValue[] allAnnotationsArray = UNKNOWN_ANNOTATIONS;
62    
63    /** Declared annotations */
64    protected AnnotationValue[] declaredAnnotationsArray = UNKNOWN_ANNOTATIONS;
65
66    /** The annotated element */
67    protected Object JavaDoc annotatedElement;
68    
69    /** The annotation helper */
70    protected AnnotationHelper annotationHelper;
71    
72    /**
73     * Create a new InheritableAnnotationHolder.
74     */

75    public InheritableAnnotationHolder()
76    {
77    }
78
79    /**
80     * Set the annotation helper
81     *
82     * @param helper the helper
83     */

84    public void setAnnotationHelper(AnnotationHelper helper)
85    {
86       this.annotationHelper = helper;
87    }
88
89    public void setAnnotatedElement(Object JavaDoc annotatedElement)
90    {
91       this.annotatedElement = annotatedElement;
92    }
93    
94    /**
95     * Get the declared annotations
96     *
97     * @return the declared annotations
98     */

99    public AnnotationValue[] getDeclaredAnnotations()
100    {
101       if (declaredAnnotationsArray == UNKNOWN_ANNOTATIONS)
102          setupAnnotations(annotationHelper.getAnnotations(annotatedElement));
103       return declaredAnnotationsArray;
104    }
105
106    public AnnotationValue[] getAnnotations()
107    {
108       if (allAnnotationsArray == UNKNOWN_ANNOTATIONS)
109          setupAnnotations(annotationHelper.getAnnotations(annotatedElement));
110       return allAnnotationsArray;
111    }
112
113    public AnnotationValue getAnnotation(String JavaDoc name)
114    {
115       if (allAnnotations == UNKNOWN_ANNOTATIONS_MAP)
116          setupAnnotations(annotationHelper.getAnnotations(annotatedElement));
117       return allAnnotations.get(name);
118    }
119
120    public boolean isAnnotationPresent(String JavaDoc name)
121    {
122       if (allAnnotations == UNKNOWN_ANNOTATIONS_MAP)
123          setupAnnotations(annotationHelper.getAnnotations(annotatedElement));
124       return allAnnotations.containsKey(name);
125    }
126
127    /**
128     * Set up the annotations
129     *
130     * @param annotations the annotations
131     */

132    public void setupAnnotations(AnnotationValue[] annotations)
133    {
134       InheritableAnnotationHolder superHolder = getSuperHolder();
135       AnnotationValue[] superAllAnnotations = (superHolder != null) ? superHolder.getAnnotations() : null;
136       
137       if (annotations != null && annotations.length > 0)
138       {
139          declaredAnnotations = new HashMap JavaDoc<String JavaDoc, AnnotationValue>();
140          declaredAnnotationsArray = annotations;
141          for (int i = 0; i < annotations.length; i++)
142             declaredAnnotations.put(annotations[i].getAnnotationType().getName(), annotations[i]);
143          allAnnotations = new HashMap JavaDoc<String JavaDoc, AnnotationValue>();
144          
145          if (superHolder != null && superAllAnnotations != null && superAllAnnotations.length != 0)
146          {
147             for (int i = 0; i < superAllAnnotations.length; i++)
148             {
149                AnnotationValue av = superAllAnnotations[i];
150                if (av.getAnnotationType().isAnnotationPresent(INHERITED_NAME))
151                {
152                   allAnnotations.put(av.getAnnotationType().getName(), av);
153                }
154             }
155          }
156          else
157             allAnnotationsArray = declaredAnnotationsArray;
158
159          for (int i = 0; i < annotations.length; i++)
160             allAnnotations.put(annotations[i].getAnnotationType().getName(), annotations[i]);
161
162          allAnnotationsArray = allAnnotations.values().toArray(new AnnotationValue[allAnnotations.size()]);
163       }
164       else
165       {
166          if (superHolder != null)
167          {
168             allAnnotations = superHolder.getAllAnnotations();
169             allAnnotationsArray = superAllAnnotations;
170          }
171       }
172    }
173    
174    /**
175     * Get all the annotations as a map
176     *
177     * @return the map
178     */

179    protected Map JavaDoc<String JavaDoc, AnnotationValue> getAllAnnotations()
180    {
181       if (allAnnotations == UNKNOWN_ANNOTATIONS_MAP)
182          setupAnnotations(annotationHelper.getAnnotations(annotatedElement));
183       return allAnnotations;
184    }
185    
186    /**
187     * Get the super holder of annoations
188     *
189     * @return the super holder
190     */

191    protected abstract InheritableAnnotationHolder getSuperHolder();
192 }
193
Popular Tags