1 16 package org.apache.commons.attributes; 17 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Modifier ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import java.util.HashMap ; 31 32 class DefaultCachedRepository implements CachedRepository { 33 34 private final static Collection EMPTY_COLLECTION = new ArrayList (0); 35 36 private final Collection classAttributes; 37 private final Map fields = new HashMap (); 38 private final Map methods = new HashMap (); 39 private final Map constructors = new HashMap (); 40 41 private static class MethodAttributeBundle { 42 private Collection attributes = EMPTY_COLLECTION; 43 private List parameterAttributes = new ArrayList (); 44 private Collection returnAttributes = EMPTY_COLLECTION; 45 46 public MethodAttributeBundle () { 47 } 48 49 public Collection getAttributes () { 50 return attributes; 51 } 52 53 public Collection getReturnAttributes () { 54 return returnAttributes; 55 } 56 57 public Collection getParameterAttributes (int index) { 58 return (Collection ) parameterAttributes.get (index); 59 } 60 61 public void setAttributes (Collection attributes) { 62 this.attributes = Collections.unmodifiableCollection (attributes); 63 } 64 65 public void setReturnAttributes (Collection returnAttributes) { 66 this.returnAttributes = Collections.unmodifiableCollection (returnAttributes); 67 } 68 69 public void addParameterAttributes (Collection parameterAttributes) { 70 this.parameterAttributes.add (Collections.unmodifiableCollection (parameterAttributes)); 71 } 72 } 73 74 public DefaultCachedRepository (Class clazz, AttributeRepositoryClass repo) { 75 76 Set tempClassAttributes = new HashSet (); 78 79 tempClassAttributes.addAll (repo.getClassAttributes ()); 80 tempClassAttributes.addAll (getInheritableClassAttributes (clazz.getSuperclass ())); 81 Class [] ifs = clazz.getInterfaces (); 82 for (int i = 0; i < ifs.length; i++) { 83 tempClassAttributes.addAll (getInheritableClassAttributes (ifs[i])); 84 } 85 this.classAttributes = Collections.unmodifiableCollection (tempClassAttributes); 86 87 Method [] methods = clazz.getDeclaredMethods (); 89 for (int i = 0; i < methods.length; i++) { 90 MethodAttributeBundle bundle = new MethodAttributeBundle (); 91 92 Method m = methods[i]; 93 String key = Util.getSignature (m); 94 95 List attributeBundle = null; 96 if (repo.getMethodAttributes ().containsKey (key)) { 97 attributeBundle = (List ) repo.getMethodAttributes ().get (key); 98 } 99 100 Set attributes = new HashSet (); 101 if (attributeBundle != null) { 102 attributes.addAll ((Collection ) attributeBundle.get (0)); 103 } 104 attributes.addAll (getInheritableMethodAttributes (clazz.getSuperclass (), m.getName (), m.getParameterTypes ())); 105 for (int j = 0; j < ifs.length; j++) { 106 attributes.addAll (getInheritableMethodAttributes (ifs[j], m.getName (), m.getParameterTypes ())); 107 } 108 109 if (attributes.size () > 0) { 110 bundle.setAttributes (attributes); 111 } 112 113 attributes = new HashSet (); 115 if (attributeBundle != null) { 116 attributes.addAll ((Collection ) attributeBundle.get (1)); 117 } 118 attributes.addAll (getInheritableReturnAttributes (clazz.getSuperclass (), m.getName (), m.getParameterTypes ())); 119 for (int j = 0; j < ifs.length; j++) { 120 attributes.addAll (getInheritableReturnAttributes (ifs[j], m.getName (), m.getParameterTypes ())); 121 } 122 if (attributes.size () > 0) { 123 bundle.setReturnAttributes (attributes); 124 } 125 126 int numParameters = m.getParameterTypes().length; 128 for (int k = 0; k < numParameters; k++) { 129 attributes = new HashSet (); 130 if (attributeBundle != null) { 131 attributes.addAll ((Collection ) attributeBundle.get (k + 2)); 132 } 133 attributes.addAll (getInheritableMethodParameterAttributes (clazz.getSuperclass (), m.getName (), m.getParameterTypes (), k)); 134 for (int j = 0; j < ifs.length; j++) { 135 attributes.addAll (getInheritableMethodParameterAttributes (ifs[j], m.getName (), m.getParameterTypes (), k)); 136 } 137 138 bundle.addParameterAttributes (attributes); 139 } 140 141 this.methods.put (m, bundle); 142 } 143 144 Constructor [] constructors = clazz.getDeclaredConstructors (); 146 for (int i = 0; i < constructors.length; i++) { 147 Constructor ctor = constructors[i]; 148 String key = Util.getSignature (ctor); 149 150 if (repo.getConstructorAttributes ().containsKey (key)) { 151 List attributeBundle = null; 152 attributeBundle = (List ) repo.getConstructorAttributes ().get (key); 153 MethodAttributeBundle bundle = new MethodAttributeBundle (); 154 155 bundle.setAttributes ((Collection ) attributeBundle.get (0)); 156 int numParameters = ctor.getParameterTypes().length; 158 for (int k = 0; k < numParameters; k++) { 159 bundle.addParameterAttributes ((Collection ) attributeBundle.get (k + 1)); 160 } 161 this.constructors.put (ctor, bundle); 162 } 163 } 164 165 Field [] fields = clazz.getDeclaredFields (); 167 for (int i = 0; i < fields.length; i++) { 168 Field f = fields[i]; 169 String key = f.getName (); 170 if (repo.getFieldAttributes ().containsKey (key)) { 171 this.fields.put (f, Collections.unmodifiableCollection ((Collection ) repo.getFieldAttributes ().get (key))); 172 } 173 } 174 } 175 176 private static Collection getInheritableAttributes (Collection attrs) { 177 HashSet result = new HashSet (); 178 179 Iterator iter = attrs.iterator (); 180 while (iter.hasNext ()) { 181 Object attr = iter.next (); 182 if (Attributes.hasAttributeType (attr.getClass (), Inheritable.class)) { 183 result.add (attr); 184 } 185 } 186 return result; 187 } 188 189 private static Collection getInheritableClassAttributes (Class c) { 190 if (c == null) { 191 return new ArrayList (0); 192 } 193 194 HashSet result = new HashSet (); 195 result.addAll (getInheritableAttributes (Attributes.getAttributes (c))); 196 197 result.addAll (getInheritableClassAttributes (c.getSuperclass ())); 199 200 Class [] ifs = c.getInterfaces (); 202 for (int i = 0; i < ifs.length; i++) { 203 result.addAll (getInheritableClassAttributes (ifs[i])); 204 } 205 206 return result; 207 } 208 209 private static Collection getInheritableMethodAttributes (Class c, String methodName, Class [] methodParams) { 210 if (c == null) { 211 return new ArrayList (0); 212 } 213 214 HashSet result = new HashSet (); 215 216 try { 217 Method m = c.getDeclaredMethod (methodName, methodParams); 219 if ((m.getModifiers () & Modifier.PRIVATE) == 0) { 220 result.addAll (getInheritableAttributes (Attributes.getAttributes (m))); 221 } 222 } catch (NoSuchMethodException nsme) { 223 } 224 225 result.addAll (getInheritableMethodAttributes (c.getSuperclass (), methodName, methodParams)); 227 228 Class [] ifs = c.getInterfaces (); 230 for (int i = 0; i < ifs.length; i++) { 231 result.addAll (getInheritableMethodAttributes (ifs[i], methodName, methodParams)); 232 } 233 234 return result; 235 } 236 237 private static Collection getInheritableMethodParameterAttributes (Class c, String methodName, Class [] methodParams, int parameter) { 238 if (c == null) { 239 return new ArrayList (0); 240 } 241 242 HashSet result = new HashSet (); 243 244 try { 245 Method m = c.getDeclaredMethod (methodName, methodParams); 247 if ((m.getModifiers () & Modifier.PRIVATE) == 0) { 248 result.addAll (getInheritableAttributes (Attributes.getParameterAttributes (m, parameter))); 249 } 250 } catch (NoSuchMethodException nsme) { 251 } 252 253 result.addAll (getInheritableMethodParameterAttributes (c.getSuperclass (), methodName, methodParams, parameter)); 255 256 Class [] ifs = c.getInterfaces (); 258 for (int i = 0; i < ifs.length; i++) { 259 result.addAll (getInheritableMethodParameterAttributes (ifs[i], methodName, methodParams, parameter)); 260 } 261 262 return result; 263 } 264 265 private static Collection getInheritableReturnAttributes (Class c, String methodName, Class [] methodParams) { 266 if (c == null) { 267 return new ArrayList (0); 268 } 269 270 HashSet result = new HashSet (); 271 272 try { 273 Method m = c.getDeclaredMethod (methodName, methodParams); 275 if ((m.getModifiers () & Modifier.PRIVATE) == 0) { 276 result.addAll (getInheritableAttributes (Attributes.getReturnAttributes (m))); 277 } 278 } catch (NoSuchMethodException nsme) { 279 } 280 281 result.addAll (getInheritableReturnAttributes (c.getSuperclass (), methodName, methodParams)); 283 284 Class [] ifs = c.getInterfaces (); 286 for (int i = 0; i < ifs.length; i++) { 287 result.addAll (getInheritableReturnAttributes (ifs[i], methodName, methodParams)); 288 } 289 290 return result; 291 } 292 293 public Collection getAttributes () { 294 return classAttributes; 295 } 296 297 public Collection getAttributes (Field f) { 298 if (fields.containsKey (f)) { 299 return (Collection ) fields.get (f); 300 } else { 301 return EMPTY_COLLECTION; 302 } 303 304 } 305 306 public Collection getAttributes (Method m) { 307 if (methods.containsKey (m)) { 308 return ((MethodAttributeBundle) methods.get (m)).getAttributes (); 309 } else { 310 return EMPTY_COLLECTION; 311 } 312 } 313 314 public Collection getParameterAttributes (Constructor c, int parameter) { 315 if (constructors.containsKey (c)) { 316 return ((MethodAttributeBundle) constructors.get (c)).getParameterAttributes (parameter); 317 } else { 318 return EMPTY_COLLECTION; 319 } 320 } 321 322 public Collection getParameterAttributes (Method m, int parameter) { 323 if (methods.containsKey (m)) { 324 return ((MethodAttributeBundle) methods.get (m)).getParameterAttributes (parameter); 325 } else { 326 return EMPTY_COLLECTION; 327 } 328 } 329 330 public Collection getReturnAttributes (Method m) { 331 if (methods.containsKey (m)) { 332 return ((MethodAttributeBundle) methods.get (m)).getReturnAttributes (); 333 } else { 334 return EMPTY_COLLECTION; 335 } 336 } 337 338 public Collection getAttributes (Constructor c) { 339 if (constructors.containsKey (c)) { 340 return ((MethodAttributeBundle) constructors.get (c)).getAttributes (); 341 } else { 342 return EMPTY_COLLECTION; 343 } 344 } 345 } 346 | Popular Tags |