KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > javassist > JavassistInheritableAnnotationHolder


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.javassist;
23
24 import java.lang.annotation.Inherited JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javassist.CtClass;
29
30 import org.jboss.reflect.plugins.AnnotationHelper;
31 import org.jboss.reflect.spi.AnnotationValue;
32
33 /**
34  *
35  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
36  * @version $Revision: 46363 $
37  */

38 public abstract class JavassistInheritableAnnotationHolder extends JavassistAnnotatedInfo
39 {
40    /** The classname of the <code>@Inherited</code> annotation, this needs retroing to work on JDK 1.4 */
41    private static final String JavaDoc INHERITED_NAME = Inherited JavaDoc.class.getName();//This
42

43    /** All annotations Map<String, AnnotationValue> */
44    protected Map JavaDoc<String JavaDoc, AnnotationValue> allAnnotations;
45
46    /** All annotations */
47    protected AnnotationValue[] allAnnotationsArray = NOT_CONFIGURED;
48
49    protected CtClass ctClass;
50
51    public JavassistInheritableAnnotationHolder(CtClass ctClass, AnnotationHelper annotationHelper)
52    {
53       super(annotationHelper);
54       this.ctClass = ctClass;
55    }
56
57    public AnnotationValue[] getAnnotations()
58    {
59       if (allAnnotationsArray == NOT_CONFIGURED)
60          setupAnnotations(annotationHelper.getAnnotations(ctClass));
61       return allAnnotationsArray;
62    }
63
64    protected AnnotationValue[] getAnnotations(Object JavaDoc obj)
65    {
66       synchronized (this)
67       {
68          if (allAnnotationsArray == NOT_CONFIGURED)
69          {
70             allAnnotationsArray = annotationHelper.getAnnotations(obj);
71             setupAnnotations(allAnnotationsArray);
72
73          }
74       }
75       return allAnnotationsArray;
76    }
77
78    public AnnotationValue getAnnotation(String JavaDoc name)
79    {
80       getAnnotations();
81       return allAnnotations.get(name);
82    }
83
84    public boolean isAnnotationPresent(String JavaDoc name)
85    {
86       getAnnotations();
87       return allAnnotations.containsKey(name);
88    }
89
90
91    /**
92     * Set up the annotations
93     *
94     * @param annotations the annotations
95     */

96    public void setupAnnotations(AnnotationValue[] annotations)
97    {
98       JavassistInheritableAnnotationHolder superHolder = getSuperHolder();
99       AnnotationValue[] superAllAnnotations = (superHolder != null) ? superHolder.getAnnotations() : null;
100
101       if (annotations != null && annotations.length > 0)
102       {
103          annotationMap = new HashMap JavaDoc<String JavaDoc, AnnotationValue>();
104          annotationsArray = annotations;
105          for (int i = 0; i < annotations.length; i++)
106          {
107             annotationMap.put(annotations[i].getAnnotationType().getName(), annotations[i]);
108          }
109          allAnnotations = new HashMap JavaDoc<String JavaDoc, AnnotationValue>();
110
111          if (superHolder != null && superAllAnnotations != null && superAllAnnotations.length != 0)
112          {
113             for (int i = 0; i < superAllAnnotations.length; i++)
114             {
115                AnnotationValue av = superAllAnnotations[i];
116                if (av.getAnnotationType().isAnnotationPresent(INHERITED_NAME))
117                {
118                   allAnnotations.put(av.getAnnotationType().getName(), av);
119                }
120             }
121          }
122          else
123             allAnnotationsArray = annotationsArray;
124
125          for (int i = 0; i < annotations.length; i++)
126             allAnnotations.put(annotations[i].getAnnotationType().getName(), annotations[i]);
127
128          allAnnotationsArray = allAnnotations.values().toArray(new AnnotationValue[allAnnotations.size()]);
129       }
130       else
131       {
132          if (superHolder != null)
133          {
134             allAnnotations = superHolder.getAllAnnotations();
135             allAnnotationsArray = superAllAnnotations;
136          }
137          else
138          {
139             allAnnotations = new HashMap JavaDoc<String JavaDoc, AnnotationValue>();
140          }
141       }
142    }
143
144    /**
145     * Get all the annotations as a map
146     *
147     * @return the map
148     */

149    protected Map JavaDoc<String JavaDoc, AnnotationValue> getAllAnnotations()
150    {
151       if (allAnnotations == null)
152          setupAnnotations(annotationHelper.getAnnotations(ctClass));
153       return allAnnotations;
154    }
155
156
157    public abstract JavassistInheritableAnnotationHolder getSuperHolder();
158 }
159
Popular Tags