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.util.Set ; 22 import java.util.Map ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 66 public class RuntimeAttributeRepository implements AttributeRepositoryClass { 67 68 72 private boolean sealed = false; 73 74 77 private final Set classAttributes = new HashSet (); 78 79 82 private final Map fieldAttributes = new HashMap (); 83 84 87 private final Map constructorAttributes = new HashMap (); 88 89 92 private final Map methodAttributes = new HashMap (); 93 94 97 private final Class clazz; 98 99 102 public RuntimeAttributeRepository (Class clazz) { 103 this.clazz = clazz; 104 } 105 106 109 public void addClassAttribute (Object attribute) { 110 classAttributes.add (attribute); 111 } 112 113 118 private void checkSealed () throws IllegalStateException { 119 if (sealed) { 120 throw new IllegalStateException ("RuntimeAttributeRepository has been sealed."); 121 } 122 } 123 124 128 private List getMethodOrConstructorAttributeBundle (Map map, String signature, int numSlots) { 129 List bundle = (List ) map.get (signature); 130 if (bundle == null) { 131 bundle = new ArrayList (); 132 map.put (signature, bundle); 133 134 for (int i = 0; i < numSlots; i++) { 135 bundle.add (new HashSet ()); 136 } 137 } 138 139 return bundle; 140 } 141 142 148 private List getMethodAttributeBundle (Method m) { 149 String signature = Util.getSignature (m); 150 if (m.getDeclaringClass () != clazz) { 151 throw new IllegalArgumentException ("There is no " + signature + " in " + clazz.getName () + ". It is defined in " + 152 m.getDeclaringClass ().getName ()); 153 } 154 155 return getMethodOrConstructorAttributeBundle (methodAttributes, signature, m.getParameterTypes ().length + 2); 156 } 157 158 164 private List getConstructorAttributeBundle (Constructor c) { 165 String signature = Util.getSignature (c); 166 if (c.getDeclaringClass () != clazz) { 167 throw new IllegalArgumentException ("There is no " + signature + " in " + clazz.getName () + ". It is defined in " + 168 c.getDeclaringClass ().getName ()); 169 } 170 171 return getMethodOrConstructorAttributeBundle (constructorAttributes, signature, c.getParameterTypes ().length + 1); 172 } 173 174 177 public void addFieldAttribute (String name, Object attribute) throws NoSuchFieldException , SecurityException { 178 addFieldAttribute (clazz.getDeclaredField (name), attribute); 179 } 180 181 184 public void addFieldAttribute (Field f, Object attribute) { 185 checkSealed (); 186 String signature = f.getName (); 187 if (f.getDeclaringClass () != clazz) { 188 throw new IllegalArgumentException ("There is no " + signature + " in " + clazz.getName () + ". It is defined in " + 189 f.getDeclaringClass ().getName ()); 190 } 191 192 Set attributeSet = (Set ) fieldAttributes.get (signature); 193 if (attributeSet == null) { 194 attributeSet = new HashSet (); 195 fieldAttributes.put (signature, attributeSet); 196 } 197 198 attributeSet.add (attribute); 199 } 200 201 205 public void addConstructorAttribute (Class [] parameters, Object attribute) throws NoSuchMethodException , SecurityException { 206 addConstructorAttribute (clazz.getDeclaredConstructor (parameters), attribute); 207 } 208 209 212 public void addConstructorAttribute (Constructor c, Object attribute) { 213 checkSealed (); 214 List bundle = getConstructorAttributeBundle (c); 215 Set ctorAttrs = (Set ) bundle.get (0); 216 ctorAttrs.add (attribute); 217 } 218 219 223 public void addMethodAttribute (String name, Class [] parameters, Object attribute) throws NoSuchMethodException , SecurityException { 224 addMethodAttribute (clazz.getDeclaredMethod (name, parameters), attribute); 225 } 226 227 public void addMethodAttribute (Method m, Object attribute) { 228 checkSealed (); 229 List bundle = getMethodAttributeBundle (m); 230 Set methodAttrs = (Set ) bundle.get (0); 231 methodAttrs.add (attribute); 232 } 233 234 238 public void addParameterAttribute (Class [] parameters, int parameterIndex, Object attribute) throws NoSuchMethodException , SecurityException { 239 addParameterAttribute (clazz.getDeclaredConstructor (parameters), parameterIndex, attribute); 240 } 241 242 245 public void addParameterAttribute (Constructor c, int parameterIndex, Object attribute) { 246 checkSealed (); 247 List bundle = getConstructorAttributeBundle (c); 248 249 Set parameterAttrs = (Set ) bundle.get (parameterIndex + 1); 250 parameterAttrs.add (attribute); 251 } 252 253 254 258 public void addParameterAttribute (String name, Class [] parameters, int parameterIndex, Object attribute) throws NoSuchMethodException , SecurityException { 259 addParameterAttribute (clazz.getDeclaredMethod (name, parameters), parameterIndex, attribute); 260 } 261 262 266 public void addParameterAttribute (Method m, int parameterIndex, Object attribute) { 267 checkSealed (); 268 List bundle = getMethodAttributeBundle (m); 269 270 Set parameterAttrs = (Set ) bundle.get (parameterIndex + 2); 271 parameterAttrs.add (attribute); 272 } 273 274 278 public void addReturnAttribute (String name, Class [] parameters, Object attribute) throws NoSuchMethodException , SecurityException { 279 addReturnAttribute (clazz.getDeclaredMethod (name, parameters), attribute); 280 } 281 282 286 public void addReturnAttribute (Method m, Object attribute) { 287 checkSealed (); 288 289 List bundle = getMethodAttributeBundle (m); 290 291 Set returnAttrs = (Set ) bundle.get (1); 292 returnAttrs.add (attribute); 293 } 294 295 298 public Class getDefinedClass () { 299 return clazz; 300 } 301 302 public Set getClassAttributes () { 303 return classAttributes; 304 } 305 306 public Map getFieldAttributes () { 307 return fieldAttributes; 308 } 309 310 public Map getMethodAttributes () { 311 return methodAttributes; 312 } 313 314 public Map getConstructorAttributes () { 315 return constructorAttributes; 316 } 317 318 321 public void seal () { 322 sealed = true; 323 } 324 } | Popular Tags |