KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > AnnotationRepository


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.aop.annotation;
23
24 import java.lang.reflect.Member JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.jboss.aop.annotation.factory.duplicate.AnnotationCreator;
34
35 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
36 import javassist.CtMember;
37
38 /**
39  * Repository for annotations that is used by the ClassAdvisor to override annotations.
40  *
41  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
42  * @version $Revision: 57381 $
43  */

44 public class AnnotationRepository
45 {
46    private static final String JavaDoc CLASS_ANNOTATION = "CLASS";
47    Map JavaDoc annotations = new ConcurrentReaderHashMap();
48    Map JavaDoc classAnnotations = new ConcurrentReaderHashMap();
49    Map JavaDoc disabledAnnotations = new ConcurrentReaderHashMap();
50    
51    public Map JavaDoc getAnnotations()
52    {
53        return annotations;
54    }
55    
56    public Map JavaDoc getClassAnnotations()
57    {
58        return classAnnotations;
59    }
60
61    public void addClassAnnotation(String JavaDoc annotation, String JavaDoc value)
62    {
63       classAnnotations.put(annotation, value);
64    }
65
66    public void addClassAnnotation(Class JavaDoc annotation, Object JavaDoc value)
67    {
68       classAnnotations.put(annotation.getName(), value);
69    }
70
71    public Object JavaDoc resolveClassAnnotation(Class JavaDoc annotation)
72    {
73       Object JavaDoc value = classAnnotations.get(annotation.getName());
74       boolean reinsert = value instanceof String JavaDoc;
75       value = extractAnnotation(value, annotation);
76       if (reinsert)
77       {
78          classAnnotations.put(annotation.getName(), value);
79       }
80       return value;
81    }
82
83    public Object JavaDoc resolveAnnotation(Member JavaDoc m, Class JavaDoc annotation)
84    {
85       Object JavaDoc value = resolveAnnotation(m, annotation.getName());
86       boolean reinsert = value instanceof String JavaDoc;
87       value = extractAnnotation(value, annotation);
88       if (reinsert)
89       {
90          addAnnotation(m, annotation, value);
91       }
92       return value;
93    }
94
95    protected Object JavaDoc extractAnnotation(Object JavaDoc value, Class JavaDoc annotation)
96    {
97       if (value == null) return null;
98       if (value instanceof String JavaDoc)
99       {
100          String JavaDoc expr = (String JavaDoc) value;
101          try
102          {
103             return AnnotationCreator.createAnnotation(expr, annotation);
104          }
105          catch (Exception JavaDoc e)
106          {
107             throw new RuntimeException JavaDoc(e);
108          }
109       }
110       return value;
111    }
112
113    protected Object JavaDoc resolveAnnotation(Member JavaDoc m, String JavaDoc annotation)
114    {
115       Map JavaDoc map = (Map JavaDoc) annotations.get(m);
116       if (map != null)
117       {
118          return map.get(annotation);
119       }
120       return null;
121    }
122    
123    public void disableAnnotation(Member JavaDoc m, String JavaDoc annotation)
124    {
125       List JavaDoc annotationList = (List JavaDoc)disabledAnnotations.get(m);
126       if (annotationList == null)
127       {
128          annotationList = new ArrayList JavaDoc();
129          disabledAnnotations.put(m,annotationList);
130       }
131       annotationList.add(annotation);
132    }
133    
134    public void disableAnnotation(String JavaDoc annotation)
135    {
136       List JavaDoc annotationList = (List JavaDoc)disabledAnnotations.get(CLASS_ANNOTATION);
137       if (annotationList == null)
138       {
139          annotationList = new ArrayList JavaDoc();
140          disabledAnnotations.put(CLASS_ANNOTATION,annotationList);
141       }
142       annotationList.add(annotation);
143    }
144    
145    public void enableAnnotation(String JavaDoc annotation)
146    {
147       List JavaDoc annotationList = (List JavaDoc)disabledAnnotations.get(CLASS_ANNOTATION);
148       if (annotationList != null)
149       {
150          annotationList.remove(annotation);
151       }
152       
153    }
154    
155    public boolean isDisabled(Member JavaDoc m, Class JavaDoc annotation)
156    {
157       return isDisabled(m,annotation.getName());
158    }
159    
160    public boolean isDisabled(Member JavaDoc m, String JavaDoc annotation)
161    {
162       List JavaDoc overrideList = (List JavaDoc)disabledAnnotations.get(m);
163       if (overrideList != null)
164       {
165          Iterator JavaDoc overrides = overrideList.iterator();
166          while (overrides.hasNext())
167          {
168             String JavaDoc override = (String JavaDoc)overrides.next();
169             if (override.equals(annotation))
170                return true;
171          }
172       }
173       return false;
174    }
175    
176    public boolean isDisabled(Class JavaDoc annotation)
177    {
178       return isDisabled(annotation.getName());
179    }
180    
181    public boolean isDisabled(String JavaDoc annotation)
182    {
183       List JavaDoc overrideList = (List JavaDoc)disabledAnnotations.get(CLASS_ANNOTATION);
184       if (overrideList != null)
185       {
186          Iterator JavaDoc overrides = overrideList.iterator();
187          while (overrides.hasNext())
188          {
189             String JavaDoc override = (String JavaDoc)overrides.next();
190             if (override.equals(annotation))
191                return true;
192          }
193       }
194       return false;
195    }
196
197    public void addAnnotation(Member JavaDoc m, Class JavaDoc annotation, Object JavaDoc value)
198    {
199       Map JavaDoc map = (Map JavaDoc) annotations.get(m);
200       if (map == null)
201       {
202          map = new HashMap JavaDoc();
203          annotations.put(m, map);
204       }
205       map.put(annotation.getName(), value);
206    }
207
208    public void addAnnotation(Member JavaDoc m, String JavaDoc annotation, Object JavaDoc value)
209    {
210       Map JavaDoc map = (Map JavaDoc) annotations.get(m);
211       if (map == null)
212       {
213          map = new HashMap JavaDoc();
214          annotations.put(m, map);
215       }
216       map.put(annotation, value);
217    }
218
219    public boolean hasClassAnnotation(String JavaDoc annotation)
220    {
221       return classAnnotations.containsKey(annotation);
222    }
223
224    public boolean hasClassAnnotation(Class JavaDoc annotation)
225    {
226       return classAnnotations.containsKey(annotation.getName());
227    }
228
229    public boolean hasAnnotation(Member JavaDoc m, Class JavaDoc annotation)
230    {
231       return resolveAnnotation(m, annotation.getName()) != null;
232    }
233
234    public boolean hasAnnotation(Member JavaDoc m, String JavaDoc annotation)
235    {
236       return resolveAnnotation(m, annotation) != null;
237    }
238
239    public boolean hasAnnotation(CtMember m, String JavaDoc annotation)
240    {
241       Set JavaDoc set = (Set JavaDoc) annotations.get(m);
242       if (set != null) return set.contains(annotation);
243       return false;
244    }
245
246    public void addAnnotation(CtMember m, String JavaDoc annotation)
247    {
248       Set JavaDoc set = (Set JavaDoc) annotations.get(m);
249       if (set == null)
250       {
251          set = new HashSet JavaDoc();
252          annotations.put(m, set);
253       }
254       set.add(annotation);
255    }
256 }
257
Popular Tags