| 1 | /** |
| 2 | * Class <code>InspectorSupport</code>. |
| 3 | */ |
| 4 | package org.jsesoft.ri; |
| 5 | |
| 6 | import java.lang.annotation.Annotation; |
| 7 | import java.lang.reflect.Constructor; |
| 8 | import java.lang.reflect.Field; |
| 9 | import java.lang.reflect.Method; |
| 10 | |
| 11 | /** |
| 12 | * Supports the implementation of inspectors and inspect strategies. |
| 13 | * <p> |
| 14 | * The <code>org.jsesoft.ri.InspectorSupport</code> provides |
| 15 | * a default implementation for all the methods |
| 16 | * the <code>ReflectionInspector</code> calls during traversal of the |
| 17 | * inspectee's reflection tree. This default |
| 18 | * implementation simply calls the respective function of the attached |
| 19 | * strategy. If no strategy is attached, |
| 20 | * the default implementation returns immediately returning |
| 21 | * <code>false</code> where a return value is required (meaning |
| 22 | * that default processing should take place). |
| 23 | |
| 24 | * <p> |
| 25 | * This class supports the implementation of inspector related classes |
| 26 | * in three ways: |
| 27 | * <ul> |
| 28 | * <li> |
| 29 | * As a superclass of <code>ReflectionInspector</code> it provides default |
| 30 | * implementations of all methods not overridden by |
| 31 | * <code>ReflectionInspector</code>. This default implementation calls |
| 32 | * the respective strategy function if a stragegy is attached. If no strategy is |
| 33 | * attached, it returns <code>false</code> indicating inspection is not yet |
| 34 | * complete. Subclasses of |
| 35 | * <code>ReflectionInspector</code> amy use this default in own overrides |
| 36 | * or need not even override. |
| 37 | * </li> |
| 38 | * <li> |
| 39 | * Strategies may subclass <code>InspectorSupport</code> as well, with the |
| 40 | * same benfits as subclasses of <code>ReflectionInspector</code>. |
| 41 | * </li> |
| 42 | * <li> |
| 43 | * Finally, <code>InspectorSupport</code> supports strategy chaining in |
| 44 | * calling the strategy attached to a strategy by default. |
| 45 | * </li> |
| 46 | * </ul> |
| 47 | * </p> |
| 48 | * <p> |
| 49 | * The test class <code>TestReflectionInspector</code> |
| 50 | * demonstrates both, attaching a strategy and subclassing. |
| 51 | * <code> |
| 52 | * <pre> |
| 53 | * public void testReflectionInspectorWithStrategy() |
| 54 | * throws Exception |
| 55 | * { |
| 56 | * inspector = new ReflectionInspector(); |
| 57 | * inspector.setInspectee( this.getClass() ); |
| 58 | * inspector.setStrategy( new InspectorSupport() ); |
| 59 | * inspector.inspect(); |
| 60 | * assertTrue( inspector.getState().booleanValue() ); |
| 61 | * } |
| 62 | * |
| 63 | * public void testReflectionInspectorSubclassed() |
| 64 | * throws Exception |
| 65 | * { |
| 66 | * inspector = new ReflectionInspector() { |
| 67 | * public void preInspect() |
| 68 | * throws Exception |
| 69 | * { |
| 70 | * assertNull( super.getState() ); |
| 71 | * super.preInspect(); |
| 72 | * assertFalse( super.getState().booleanValue() ); |
| 73 | * } |
| 74 | |
| 75 | * public void postInspect() |
| 76 | * throws Exception |
| 77 | * { |
| 78 | * assertFalse( super.getState() ); |
| 79 | * super.postInspect(); |
| 80 | * assertTrue( super.getState().booleanValue() ); |
| 81 | * } |
| 82 | * }; |
| 83 | * inspector.setInspectee( this.getClass() ); |
| 84 | * inspector.inspect(); |
| 85 | * assertTrue( inspector.getState().booleanValue() ); |
| 86 | * } |
| 87 | * </pre> |
| 88 | * </code> |
| 89 | * </p> |
| 90 | * @author JSESoft |
| 91 | * @version 1.0 |
| 92 | */ |
| 93 | public class InspectorSupport |
| 94 | implements InspectStrategy |
| 95 | { |
| 96 | private InspectStrategy strategy; |
| 97 | |
| 98 | /** |
| 99 | * Attaches a strategy to the inspector. |
| 100 | * |
| 101 | * @param strategy InspectStrategy to attach |
| 102 | * @see #getStrategy |
| 103 | */ |
| 104 | public void setStrategy( InspectStrategy strategy ) |
| 105 | { |
| 106 | this.strategy = strategy; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Retrieves the attached strategy. |
| 111 | * |
| 112 | * @return the InspectStrategy attached |
| 113 | * @see #setStrategy |
| 114 | */ |
| 115 | public InspectStrategy getStrategy() |
| 116 | { |
| 117 | return strategy; |
| 118 | } |
| 119 | |
| 120 | public void preInspect( Class inspected ) |
| 121 | throws Exception |
| 122 | { |
| 123 | InspectStrategy straqtegy = getStrategy(); |
| 124 | if( strategy == null ) { |
| 125 | return; |
| 126 | } |
| 127 | strategy.preInspect( inspected ); |
| 128 | } |
| 129 | |
| 130 | public void preInspectAnnotation( Class inspected, Annotation annotation ) |
| 131 | throws Exception |
| 132 | { |
| 133 | InspectStrategy straqtegy = getStrategy(); |
| 134 | if( strategy == null ) { |
| 135 | return; |
| 136 | } |
| 137 | strategy.preInspectAnnotation( inspected, annotation ); |
| 138 | } |
| 139 | |
| 140 | public void preInspectAnnotations( Class inspected, Constructor constructor ) |
| 141 | throws Exception |
| 142 | { |
| 143 | InspectStrategy straqtegy = getStrategy(); |
| 144 | if( strategy == null ) { |
| 145 | return; |
| 146 | } |
| 147 | strategy.preInspectAnnotations( inspected, constructor ); |
| 148 | } |
| 149 | |
| 150 | public void preInspectAnnotations( Class inspected, Field field ) |
| 151 | throws Exception |
| 152 | { |
| 153 | InspectStrategy straqtegy = getStrategy(); |
| 154 | if( strategy == null ) { |
| 155 | return; |
| 156 | } |
| 157 | strategy.preInspectAnnotations( inspected, field ); |
| 158 | } |
| 159 | |
| 160 | public void preInspectAnnotations( Class inspected, Method method ) |
| 161 | throws Exception |
| 162 | { |
| 163 | InspectStrategy straqtegy = getStrategy(); |
| 164 | if( strategy == null ) { |
| 165 | return; |
| 166 | } |
| 167 | strategy.preInspectAnnotations( inspected, method ); |
| 168 | } |
| 169 | |
| 170 | public void preInspectConstructor( Class inspected, Constructor constructor ) |
| 171 | throws Exception |
| 172 | { |
| 173 | InspectStrategy straqtegy = getStrategy(); |
| 174 | if( strategy == null ) { |
| 175 | return; |
| 176 | } |
| 177 | strategy.preInspectConstructor( inspected, constructor ); |
| 178 | } |
| 179 | |
| 180 | public void preInspectConstructors( Class inspected ) |
| 181 | throws Exception |
| 182 | { |
| 183 | InspectStrategy straqtegy = getStrategy(); |
| 184 | if( strategy == null ) { |
| 185 | return; |
| 186 | } |
| 187 | strategy.preInspectConstructors( inspected ); |
| 188 | } |
| 189 | |
| 190 | public void preInspectField( Class inspected, Field field ) |
| 191 | throws Exception |
| 192 | { |
| 193 | InspectStrategy straqtegy = getStrategy(); |
| 194 | if( strategy == null ) { |
| 195 | return; |
| 196 | } |
| 197 | strategy.preInspectField( inspected, field ); |
| 198 | } |
| 199 | |
| 200 | public void preInspectFields( Class inspected ) |
| 201 | throws Exception |
| 202 | { |
| 203 | InspectStrategy straqtegy = getStrategy(); |
| 204 | if( strategy == null ) { |
| 205 | return; |
| 206 | } |
| 207 | strategy.preInspectFields( inspected ); |
| 208 | } |
| 209 | |
| 210 | public void preInspectMethod( Class inspected, Method method ) |
| 211 | throws Exception |
| 212 | { |
| 213 | InspectStrategy straqtegy = getStrategy(); |
| 214 | if( strategy == null ) { |
| 215 | return; |
| 216 | } |
| 217 | strategy.preInspectMethod( inspected, method ); |
| 218 | } |
| 219 | |
| 220 | public void preInspectMethods( Class inspected ) |
| 221 | throws Exception |
| 222 | { |
| 223 | InspectStrategy straqtegy = getStrategy(); |
| 224 | if( strategy == null ) { |
| 225 | return; |
| 226 | } |
| 227 | strategy.preInspectMethods( inspected ); |
| 228 | } |
| 229 | |
| 230 | public void preInspectParameterType( Class inspected, Class<? > parameterType ) |
| 231 | throws Exception |
| 232 | { |
| 233 | InspectStrategy straqtegy = getStrategy(); |
| 234 | if( strategy == null ) { |
| 235 | return; |
| 236 | } |
| 237 | strategy.preInspectParameterType( inspected, parameterType ); |
| 238 | } |
| 239 | |
| 240 | public void preInspectParameterTypes( Class inspected, Constructor constructor ) |
| 241 | throws Exception |
| 242 | { |
| 243 | InspectStrategy straqtegy = getStrategy(); |
| 244 | if( strategy == null ) { |
| 245 | return; |
| 246 | } |
| 247 | strategy.preInspectParameterTypes( inspected, constructor ); |
| 248 | } |
| 249 | |
| 250 | public void preInspectParameterTypes( Class inspected, Method method ) |
| 251 | throws Exception |
| 252 | { |
| 253 | InspectStrategy straqtegy = getStrategy(); |
| 254 | if( strategy == null ) { |
| 255 | return; |
| 256 | } |
| 257 | strategy.preInspectParameterTypes( inspected, method ); |
| 258 | } |
| 259 | |
| 260 | public void postInspect( Class inspected ) |
| 261 | throws Exception |
| 262 | { |
| 263 | InspectStrategy straqtegy = getStrategy(); |
| 264 | if( strategy == null ) { |
| 265 | return; |
| 266 | } |
| 267 | strategy.postInspect( inspected ); |
| 268 | } |
| 269 | |
| 270 | public void postInspectAnnotation( Class inspected, Annotation annotation ) |
| 271 | throws Exception |
| 272 | { |
| 273 | InspectStrategy straqtegy = getStrategy(); |
| 274 | if( strategy == null ) { |
| 275 | return; |
| 276 | } |
| 277 | strategy.postInspectAnnotation( inspected, annotation ); |
| 278 | } |
| 279 | |
| 280 | public void postInspectAnnotations( Class inspected, Constructor constructor ) |
| 281 | throws Exception |
| 282 | { |
| 283 | InspectStrategy straqtegy = getStrategy(); |
| 284 | if( strategy == null ) { |
| 285 | return; |
| 286 | } |
| 287 | strategy.postInspectAnnotations( inspected, constructor ); |
| 288 | } |
| 289 | |
| 290 | public void postInspectAnnotations( Class inspected, Field field ) |
| 291 | throws Exception |
| 292 | { |
| 293 | InspectStrategy straqtegy = getStrategy(); |
| 294 | if( strategy == null ) { |
| 295 | return; |
| 296 | } |
| 297 | strategy.postInspectAnnotations( inspected, field ); |
| 298 | } |
| 299 | |
| 300 | public void postInspectAnnotations( Class inspected, Method method ) |
| 301 | throws Exception |
| 302 | { |
| 303 | InspectStrategy straqtegy = getStrategy(); |
| 304 | if( strategy == null ) { |
| 305 | return; |
| 306 | } |
| 307 | strategy.postInspectAnnotations( inspected, method ); |
| 308 | } |
| 309 | |
| 310 | public void postInspectConstructor( Class inspected, Constructor constructor ) |
| 311 | throws Exception |
| 312 | { |
| 313 | InspectStrategy straqtegy = getStrategy(); |
| 314 | if( strategy == null ) { |
| 315 | return; |
| 316 | } |
| 317 | strategy.postInspectConstructor( inspected, constructor ); |
| 318 | } |
| 319 | |
| 320 | public void postInspectConstructors( Class inspected ) |
| 321 | throws Exception |
| 322 | { |
| 323 | InspectStrategy straqtegy = getStrategy(); |
| 324 | if( strategy == null ) { |
| 325 | return; |
| 326 | } |
| 327 | strategy.postInspectConstructors( inspected ); |
| 328 | } |
| 329 | |
| 330 | public void postInspectField( Class inspected, Field field ) |
| 331 | throws Exception |
| 332 | { |
| 333 | InspectStrategy straqtegy = getStrategy(); |
| 334 | if( strategy == null ) { |
| 335 | return; |
| 336 | } |
| 337 | strategy.postInspectField( inspected, field ); |
| 338 | } |
| 339 | |
| 340 | public void postInspectFields( Class inspected ) |
| 341 | throws Exception |
| 342 | { |
| 343 | InspectStrategy straqtegy = getStrategy(); |
| 344 | if( strategy == null ) { |
| 345 | return; |
| 346 | } |
| 347 | strategy.postInspectFields( inspected ); |
| 348 | } |
| 349 | |
| 350 | public void postInspectMethod( Class inspected, Method method ) |
| 351 | throws Exception |
| 352 | { |
| 353 | InspectStrategy straqtegy = getStrategy(); |
| 354 | if( strategy == null ) { |
| 355 | return; |
| 356 | } |
| 357 | strategy.postInspectMethod( inspected, method ); |
| 358 | } |
| 359 | |
| 360 | public void postInspectMethods( Class inspected ) |
| 361 | throws Exception |
| 362 | { |
| 363 | InspectStrategy straqtegy = getStrategy(); |
| 364 | if( strategy == null ) { |
| 365 | return; |
| 366 | } |
| 367 | strategy.postInspectMethods( inspected ); |
| 368 | } |
| 369 | |
| 370 | public void postInspectParameterType( Class inspected, Class<? > parameterType ) |
| 371 | throws Exception |
| 372 | { |
| 373 | InspectStrategy straqtegy = getStrategy(); |
| 374 | if( strategy == null ) { |
| 375 | return; |
| 376 | } |
| 377 | strategy.postInspectParameterType( inspected, parameterType ); |
| 378 | } |
| 379 | |
| 380 | public void postInspectParameterTypes( Class inspected, Constructor constructor ) |
| 381 | throws Exception |
| 382 | { |
| 383 | InspectStrategy straqtegy = getStrategy(); |
| 384 | if( strategy == null ) { |
| 385 | return; |
| 386 | } |
| 387 | strategy.postInspectParameterTypes( inspected, constructor ); |
| 388 | } |
| 389 | |
| 390 | public void postInspectParameterTypes( Class inspected, Method method ) |
| 391 | throws Exception |
| 392 | { |
| 393 | InspectStrategy straqtegy = getStrategy(); |
| 394 | if( strategy == null ) { |
| 395 | return; |
| 396 | } |
| 397 | strategy.postInspectParameterTypes( inspected, method ); |
| 398 | } |
| 399 | |
| 400 | public boolean inspect( Class inspected ) |
| 401 | throws Exception |
| 402 | { |
| 403 | InspectStrategy straqtegy = getStrategy(); |
| 404 | if( strategy == null ) { |
| 405 | return false; |
| 406 | } |
| 407 | return strategy.inspect( inspected ); |
| 408 | } |
| 409 | |
| 410 | public boolean inspectAnnotation( Class inspected, Annotation annotation ) |
| 411 | throws Exception |
| 412 | { |
| 413 | InspectStrategy straqtegy = getStrategy(); |
| 414 | if( strategy == null ) { |
| 415 | return false; |
| 416 | } |
| 417 | return strategy.inspectAnnotation( inspected, annotation ); |
| 418 | } |
| 419 | |
| 420 | public boolean inspectAnnotations( Class inspected, Field field ) |
| 421 | throws Exception |
| 422 | { |
| 423 | InspectStrategy straqtegy = getStrategy(); |
| 424 | if( strategy == null ) { |
| 425 | return false; |
| 426 | } |
| 427 | return strategy.inspectAnnotations( inspected, field ); |
| 428 | } |
| 429 | |
| 430 | public boolean inspectAnnotations( Class inspected, Method method ) |
| 431 | throws Exception |
| 432 | { |
| 433 | InspectStrategy straqtegy = getStrategy(); |
| 434 | if( strategy == null ) { |
| 435 | return false; |
| 436 | } |
| 437 | return strategy.inspectAnnotations( inspected, method ); |
| 438 | |
| 439 | } |
| 440 | |
| 441 | public boolean inspectAnnotations( Class inspected, Constructor constructor ) |
| 442 | throws Exception |
| 443 | { |
| 444 | InspectStrategy straqtegy = getStrategy(); |
| 445 | if( strategy == null ) { |
| 446 | return false; |
| 447 | } |
| 448 | return strategy.inspectAnnotations( inspected, constructor ); |
| 449 | } |
| 450 | |
| 451 | public boolean inspectConstructor( Class inspected, Constructor constructor ) |
| 452 | throws Exception |
| 453 | { |
| 454 | InspectStrategy straqtegy = getStrategy(); |
| 455 | if( strategy == null ) { |
| 456 | return false; |
| 457 | } |
| 458 | return strategy.inspectConstructor( inspected, constructor ); |
| 459 | } |
| 460 | |
| 461 | public boolean inspectConstructors( Class inspected ) |
| 462 | throws Exception |
| 463 | { |
| 464 | InspectStrategy straqtegy = getStrategy(); |
| 465 | if( strategy == null ) { |
| 466 | return false; |
| 467 | } |
| 468 | return strategy.inspectConstructors( inspected ); |
| 469 | } |
| 470 | |
| 471 | public boolean inspectField( Class inspected, Field field ) |
| 472 | throws Exception |
| 473 | { |
| 474 | InspectStrategy straqtegy = getStrategy(); |
| 475 | if( strategy == null ) { |
| 476 | return false; |
| 477 | } |
| 478 | return strategy.inspectField( inspected, field ); |
| 479 | } |
| 480 | |
| 481 | public boolean inspectFields( Class inspected ) |
| 482 | throws Exception |
| 483 | { |
| 484 | InspectStrategy straqtegy = getStrategy(); |
| 485 | if( strategy == null ) { |
| 486 | return false; |
| 487 | } |
| 488 | return strategy.inspectFields( inspected ); |
| 489 | } |
| 490 | |
| 491 | public boolean inspectMethod( Class inspected, Method method ) |
| 492 | throws Exception |
| 493 | { |
| 494 | InspectStrategy straqtegy = getStrategy(); |
| 495 | if( strategy == null ) { |
| 496 | return false; |
| 497 | } |
| 498 | return strategy.inspectMethod( inspected, method ); |
| 499 | } |
| 500 | |
| 501 | public boolean inspectMethods( Class inspected ) |
| 502 | throws Exception |
| 503 | { |
| 504 | InspectStrategy straqtegy = getStrategy(); |
| 505 | if( strategy == null ) { |
| 506 | return false; |
| 507 | } |
| 508 | return strategy.inspectMethods( inspected ); |
| 509 | |
| 510 | } |
| 511 | |
| 512 | public boolean inspectParameterType( Class inspected, Class<? > parameterType ) |
| 513 | throws Exception |
| 514 | { |
| 515 | InspectStrategy straqtegy = getStrategy(); |
| 516 | if( strategy == null ) { |
| 517 | return false; |
| 518 | } |
| 519 | return strategy.inspectParameterType( inspected, parameterType ); |
| 520 | } |
| 521 | |
| 522 | public boolean inspectParameterTypes( Class inspected, Method method ) |
| 523 | throws Exception |
| 524 | { |
| 525 | InspectStrategy straqtegy = getStrategy(); |
| 526 | if( strategy == null ) { |
| 527 | return false; |
| 528 | } |
| 529 | return strategy.inspectParameterTypes( inspected, method ); |
| 530 | } |
| 531 | |
| 532 | public boolean inspectParameterTypes( Class inspected, Constructor constructor ) |
| 533 | throws Exception |
| 534 | { |
| 535 | InspectStrategy straqtegy = getStrategy(); |
| 536 | if( strategy == null ) { |
| 537 | return false; |
| 538 | } |
| 539 | return strategy.inspectParameterTypes( inspected, constructor ); |
| 540 | |
| 541 | } |
| 542 | } |