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