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.Collection ; 22 import java.util.Collections ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.WeakHashMap ; 30 31 90 public class Attributes { 91 92 106 private final static Map classRepositories = new WeakHashMap (); 107 108 112 private static List initList = new ArrayList (); 113 114 private synchronized static CachedRepository getCachedRepository (Class clazz) throws RepositoryError, CircularDependencyError { 115 if (classRepositories.containsKey (clazz)) { 116 CachedRepository cr = (CachedRepository) classRepositories.get (clazz); 117 if (cr == null) { 118 List dependencyList = new ArrayList (); 120 dependencyList.addAll (initList); 121 throw new CircularDependencyError (clazz.getName (), dependencyList); 122 } else { 123 return cr; 124 } 125 } else { 126 CachedRepository cached = null; 128 129 initList.add (clazz.getName ()); 130 try { 131 classRepositories.put (clazz, null); 132 133 Class attributeRepo = null; 134 AttributeRepositoryClass repo = EmptyAttributeRepositoryClass.INSTANCE; 135 try { 136 attributeRepo = Class.forName (clazz.getName () + "$__attributeRepository", true, clazz.getClassLoader ()); 137 repo = (AttributeRepositoryClass) attributeRepo.newInstance (); 138 } catch (ClassNotFoundException cnfe) { 139 repo = EmptyAttributeRepositoryClass.INSTANCE; 141 } catch (InstantiationException ie) { 142 throw new RepositoryError (ie); 143 } catch (IllegalAccessException iae) { 144 throw new RepositoryError (iae); 145 } 146 cached = new DefaultCachedRepository (clazz, repo); 147 148 classRepositories.put (clazz, cached); 149 150 if (repo != null) { 151 Util.validateRepository (clazz, repo); 152 } 153 } finally { 154 initList.remove (initList.size () - 1); 155 } 156 157 return cached; 158 } 159 } 160 161 168 private static Object getAttribute (Collection attrs, Class attributeClass) throws MultipleAttributesError { 169 Object candidate = null; 170 Iterator iter = attrs.iterator (); 171 while (iter.hasNext ()) { 172 Object attr = iter.next (); 173 if (attr.getClass () == attributeClass) { 174 if (candidate == null) { 175 candidate = attr; 176 } else { 177 throw new MultipleAttributesError (attributeClass.getName ()); 178 } 179 } 180 } 181 182 return candidate; 183 } 184 185 192 public static Object getAttribute (Class clazz, Class attributeClass) throws RepositoryError, MultipleAttributesError { 193 return getAttribute (getAttributes (clazz), attributeClass); 194 } 195 196 203 public static Object getAttribute (Field field, Class attributeClass) throws RepositoryError, MultipleAttributesError { 204 return getAttribute (getAttributes (field), attributeClass); 205 } 206 207 214 public static Object getAttribute (Constructor ctor, Class attributeClass) throws RepositoryError, MultipleAttributesError { 215 return getAttribute (getAttributes (ctor), attributeClass); 216 } 217 218 225 public static Object getAttribute (Method method, Class attributeClass) throws RepositoryError, MultipleAttributesError { 226 return getAttribute (getAttributes (method), attributeClass); 227 } 228 229 236 public static Object getParameterAttribute (Method method, int parameter, Class attributeClass) throws RepositoryError, MultipleAttributesError { 237 return getAttribute (getParameterAttributes (method, parameter), attributeClass); 238 } 239 240 247 public static Object getParameterAttribute (Constructor constructor, int parameter, Class attributeClass) throws RepositoryError, MultipleAttributesError { 248 return getAttribute (getParameterAttributes (constructor, parameter), attributeClass); 249 } 250 251 258 public static Object getReturnAttribute (Method method, Class attributeClass) throws RepositoryError, MultipleAttributesError { 259 return getAttribute (getReturnAttributes (method), attributeClass); 260 } 261 262 265 public static Collection getAttributes (Class clazz) throws RepositoryError { 266 if (clazz == null) { 267 throw new NullPointerException ("clazz"); 268 } 269 270 return getCachedRepository (clazz).getAttributes (); 271 } 272 273 276 public static Collection getAttributes (Method method) throws RepositoryError { 277 if (method == null) { 278 throw new NullPointerException ("method"); 279 } 280 281 return getCachedRepository (method.getDeclaringClass()).getAttributes (method); 282 } 283 284 287 public static Collection getParameterAttributes (Method method, int parameter) throws RepositoryError { 288 return getCachedRepository (method.getDeclaringClass()).getParameterAttributes (method, parameter); 289 } 290 291 294 public static Collection getParameterAttributes (Constructor constructor, int parameter) throws RepositoryError { 295 if (constructor == null) { 296 throw new NullPointerException ("constructor"); 297 } 298 return getCachedRepository (constructor.getDeclaringClass()).getParameterAttributes (constructor, parameter); 299 } 300 301 304 public static Collection getReturnAttributes (Method method) throws RepositoryError { 305 if (method == null) { 306 throw new NullPointerException ("method"); 307 } 308 return getCachedRepository (method.getDeclaringClass()).getReturnAttributes (method); 309 } 310 311 314 public static Collection getAttributes (Field field) throws RepositoryError { 315 if (field == null) { 316 throw new NullPointerException ("field"); 317 } 318 return getCachedRepository (field.getDeclaringClass()).getAttributes (field); 319 } 320 321 324 public static Collection getAttributes (Constructor cons) throws RepositoryError { 325 if (cons == null) { 326 throw new NullPointerException ("cons"); 327 } 328 return getCachedRepository (cons.getDeclaringClass()).getAttributes (cons); 329 } 330 331 334 private static Collection getAttributes (Collection attrs, Class attributeClass) { 335 HashSet result = new HashSet (); 336 Iterator iter = attrs.iterator (); 337 while (iter.hasNext ()) { 338 Object attr = iter.next (); 339 if (attr.getClass () == attributeClass) { 340 result.add (attr); 341 } 342 } 343 344 return Collections.unmodifiableCollection (result); 345 } 346 347 351 public static Collection getAttributes (Class clazz, Class attributeClass) throws RepositoryError { 352 return getAttributes (getAttributes (clazz), attributeClass); 353 } 354 355 359 public static Collection getAttributes (Field field, Class attributeClass) throws RepositoryError { 360 return getAttributes (getAttributes (field), attributeClass); 361 } 362 363 367 public static Collection getAttributes (Constructor ctor, Class attributeClass) throws RepositoryError { 368 return getAttributes (getAttributes (ctor), attributeClass); 369 } 370 371 375 public static Collection getAttributes (Method method, Class attributeClass) throws RepositoryError { 376 return getAttributes (getAttributes (method), attributeClass); 377 } 378 379 383 public static Collection getParameterAttributes (Method method, int parameter, Class attributeClass) throws RepositoryError { 384 return getAttributes (getParameterAttributes (method, parameter), attributeClass); 385 } 386 387 391 public static Collection getParameterAttributes (Constructor constructor, int parameter, Class attributeClass) throws RepositoryError { 392 return getAttributes (getParameterAttributes (constructor, parameter), attributeClass); 393 } 394 395 399 public static Collection getReturnAttributes (Method method, Class attributeClass) throws RepositoryError { 400 return getAttributes (getReturnAttributes (method), attributeClass); 401 } 402 403 407 private static boolean hasAttributeType (Collection attrs, Class attributeClass) { 408 Iterator iter = attrs.iterator (); 409 while (iter.hasNext ()) { 410 Object attr = iter.next (); 411 if (attr.getClass () == attributeClass) { 412 return true; 413 } 414 } 415 416 return false; 417 } 418 419 423 public static boolean hasAttributeType (Class clazz, Class attributeClass) throws RepositoryError { 424 return hasAttributeType (getAttributes (clazz), attributeClass); 425 } 426 427 431 public static boolean hasAttributeType (Field field, Class attributeClass) throws RepositoryError { 432 return hasAttributeType (getAttributes (field), attributeClass); 433 } 434 435 439 public static boolean hasAttributeType (Constructor ctor, Class attributeClass) throws RepositoryError { 440 return hasAttributeType (getAttributes (ctor), attributeClass); 441 } 442 443 447 public static boolean hasAttributeType (Method method, Class attributeClass) throws RepositoryError { 448 return hasAttributeType (getAttributes (method), attributeClass); 449 } 450 451 455 public static boolean hasParameterAttributeType (Method method, int parameter, Class attributeClass) throws RepositoryError { 456 return hasAttributeType (getParameterAttributes (method, parameter), attributeClass); 457 } 458 459 463 public static boolean hasParameterAttributeType (Constructor constructor, int parameter, Class attributeClass) throws RepositoryError { 464 return hasAttributeType (getParameterAttributes (constructor, parameter), attributeClass); 465 } 466 467 471 public static boolean hasReturnAttributeType (Method method, Class attributeClass) throws RepositoryError { 472 return hasAttributeType (getReturnAttributes (method), attributeClass); 473 } 474 475 479 private static boolean hasAttribute (Collection attrs, Object attribute) throws RepositoryError { 480 return attrs.contains (attribute); 481 } 482 483 487 public static boolean hasAttribute (Class clazz, Object attribute) throws RepositoryError { 488 return hasAttribute (getAttributes (clazz), attribute); 489 } 490 491 495 public static boolean hasAttribute (Field field, Object attribute) throws RepositoryError { 496 return hasAttribute (getAttributes (field), attribute); 497 } 498 499 503 public static boolean hasAttribute (Constructor ctor, Object attribute) throws RepositoryError { 504 return hasAttribute (getAttributes (ctor), attribute); 505 } 506 507 511 public static boolean hasAttribute (Method method, Object attribute) throws RepositoryError { 512 return hasAttribute (getAttributes (method), attribute); 513 } 514 515 519 public static boolean hasParameterAttribute (Method method, int parameter, Object attribute) throws RepositoryError { 520 return hasAttribute (getParameterAttributes (method, parameter), attribute); 521 } 522 523 527 public static boolean hasParameterAttribute (Constructor constructor, int parameter, Object attribute) throws RepositoryError { 528 return hasAttribute (getParameterAttributes (constructor, parameter), attribute); 529 } 530 531 535 public static boolean hasReturnAttribute (Method method, Object attribute) throws RepositoryError { 536 return hasAttribute (getReturnAttributes (method), attribute); 537 } 538 539 551 public static synchronized void setAttributes (RuntimeAttributeRepository repo) throws IllegalStateException { 552 repo.seal (); 553 554 Class clazz = repo.getDefinedClass (); 555 if (classRepositories.get (clazz) != null) { 556 throw new IllegalStateException (clazz.getName ()); 557 } 558 Util.validateRepository (clazz, repo); 559 560 DefaultCachedRepository cached = new DefaultCachedRepository (clazz, repo); 561 classRepositories.put (clazz, cached); 562 } 563 } | Popular Tags |