1 package spoon.reflect.visitor; 2 3 import java.lang.annotation.Annotation ; 4 import java.util.Collection ; 5 6 import spoon.reflect.code.CtArrayAccess; 7 import spoon.reflect.code.CtAssert; 8 import spoon.reflect.code.CtAssignment; 9 import spoon.reflect.code.CtBinaryOperator; 10 import spoon.reflect.code.CtBlock; 11 import spoon.reflect.code.CtBreak; 12 import spoon.reflect.code.CtCase; 13 import spoon.reflect.code.CtCatch; 14 import spoon.reflect.code.CtConditional; 15 import spoon.reflect.code.CtContinue; 16 import spoon.reflect.code.CtDo; 17 import spoon.reflect.code.CtExpression; 18 import spoon.reflect.code.CtFieldAccess; 19 import spoon.reflect.code.CtFor; 20 import spoon.reflect.code.CtForEach; 21 import spoon.reflect.code.CtIf; 22 import spoon.reflect.code.CtInvocation; 23 import spoon.reflect.code.CtLiteral; 24 import spoon.reflect.code.CtLocalVariable; 25 import spoon.reflect.code.CtNewArray; 26 import spoon.reflect.code.CtNewClass; 27 import spoon.reflect.code.CtOperatorAssignment; 28 import spoon.reflect.code.CtReturn; 29 import spoon.reflect.code.CtStatementList; 30 import spoon.reflect.code.CtSwitch; 31 import spoon.reflect.code.CtSynchronized; 32 import spoon.reflect.code.CtThrow; 33 import spoon.reflect.code.CtTry; 34 import spoon.reflect.code.CtUnaryOperator; 35 import spoon.reflect.code.CtVariableAccess; 36 import spoon.reflect.code.CtWhile; 37 import spoon.reflect.declaration.CtAnnotation; 38 import spoon.reflect.declaration.CtAnnotationType; 39 import spoon.reflect.declaration.CtAnonymousExecutable; 40 import spoon.reflect.declaration.CtClass; 41 import spoon.reflect.declaration.CtConstructor; 42 import spoon.reflect.declaration.CtElement; 43 import spoon.reflect.declaration.CtEnum; 44 import spoon.reflect.declaration.CtField; 45 import spoon.reflect.declaration.CtInterface; 46 import spoon.reflect.declaration.CtMethod; 47 import spoon.reflect.declaration.CtPackage; 48 import spoon.reflect.declaration.CtParameter; 49 import spoon.reflect.declaration.CtTypeParameter; 50 import spoon.reflect.reference.CtArrayTypeReference; 51 import spoon.reflect.reference.CtExecutableReference; 52 import spoon.reflect.reference.CtFieldReference; 53 import spoon.reflect.reference.CtLocalVariableReference; 54 import spoon.reflect.reference.CtPackageReference; 55 import spoon.reflect.reference.CtParameterReference; 56 import spoon.reflect.reference.CtReference; 57 import spoon.reflect.reference.CtTypeParameterReference; 58 import spoon.reflect.reference.CtTypeReference; 59 60 63 public class CtScanner implements CtVisitor { 64 67 public CtScanner() { 68 super(); 69 } 70 71 75 protected void enter(CtElement e) { 76 } 77 78 82 protected void enterReference(CtReference e) { 83 } 84 85 89 protected void exit(CtElement e) { 90 } 91 92 96 protected void exitReference(CtReference e) { 97 } 98 99 102 public void scan(Collection <? extends CtElement> elements) { 103 if ((elements != null)) { 104 for (CtElement e : elements) { 105 scan(e); 106 } 107 } 108 109 } 110 111 114 public void scan(CtElement element) { 115 if ((element != null)) 116 element.accept(this); 117 118 } 119 120 123 public void scan(CtReference reference) { 124 if ((reference != null)) 125 reference.accept(this); 126 127 } 128 129 132 public void scanReferences(Collection <? extends CtReference> references) { 133 if ((references != null)) { 134 for (CtReference r : references) { 135 scan(r); 136 } 137 } 138 139 } 140 141 public <A extends Annotation > void visitCtAnnotation( 142 CtAnnotation<A> annotation) { 143 enter(annotation); 144 scan(annotation.getAnnotationType()); 145 scan(annotation.getAnnotations()); 146 for (Object o : annotation.getElementValues().values()) { 147 scan(o); 148 } 149 exit(annotation); 150 } 151 152 156 public void scan(Object o) { 157 if (o instanceof CtElement) 158 scan((CtElement) o); 159 if (o instanceof CtReference) 160 scan((CtReference) o); 161 if (o instanceof Collection <?>) { 162 for (Object obj : (Collection ) o) { 163 scan(obj); 164 } 165 } 166 } 167 168 public <A extends Annotation > void visitCtAnnotationType( 169 CtAnnotationType<A> annotationType) { 170 enter(annotationType); 171 scan(annotationType.getNestedTypes()); 172 scan(annotationType.getAnnotations()); 173 scan(annotationType.getFields()); 174 exit(annotationType); 175 } 176 177 public void visitCtAnonymousExecutable(CtAnonymousExecutable anonymousExec) { 178 enter(anonymousExec); 179 scan(anonymousExec.getBody()); 180 scan(anonymousExec.getAnnotations()); 181 exit(anonymousExec); 182 } 183 184 public <T, E extends CtExpression<?>> void visitCtArrayAccess( 185 CtArrayAccess<T, E> arrayAccess) { 186 enter(arrayAccess); 187 scan(arrayAccess.getType()); 188 scan(arrayAccess.getAnnotations()); 189 scan(arrayAccess.getIndexExpression()); 190 scanReferences(arrayAccess.getTypeCasts()); 191 scan(arrayAccess.getTarget()); 192 exit(arrayAccess); 193 } 194 195 public <T> void visitCtArrayTypeReference(CtArrayTypeReference<T> reference) { 196 enterReference(reference); 197 scan(reference.getPackage()); 198 scanReferences(reference.getActualTypeArguments()); 199 scan(reference.getComponentType()); 200 scan(reference.getDeclaringType()); 201 exitReference(reference); 202 } 203 204 public <T> void visitCtAssert(CtAssert<T> asserted) { 205 enter(asserted); 206 scan(asserted.getAssertExpression()); 207 scan(asserted.getExpression()); 208 scan(asserted.getAnnotations()); 209 exit(asserted); 210 } 211 212 public <T, A extends T> void visitCtAssignment( 213 CtAssignment<T, A> assignement) { 214 enter(assignement); 215 scan(assignement.getType()); 216 scan(assignement.getAnnotations()); 217 scan(assignement.getAssignment()); 218 scan(assignement.getAssigned()); 219 scanReferences(assignement.getTypeCasts()); 220 exit(assignement); 221 } 222 223 public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) { 224 enter(operator); 225 scan(operator.getType()); 226 scan(operator.getAnnotations()); 227 scan(operator.getLeftHandOperand()); 228 scan(operator.getRightHandOperand()); 229 scanReferences(operator.getTypeCasts()); 230 exit(operator); 231 } 232 233 public <R> void visitCtBlock(CtBlock<R> block) { 234 enter(block); 235 scan(block.getAnnotations()); 236 scan(block.getStatements()); 237 exit(block); 238 } 239 240 public void visitCtBreak(CtBreak breakStatement) { 241 enter(breakStatement); 242 scan(breakStatement.getAnnotations()); 243 exit(breakStatement); 244 } 245 246 public <S> void visitCtCase(CtCase<S> caseStatement) { 247 enter(caseStatement); 248 scan(caseStatement.getCaseExpression()); 249 scan(caseStatement.getStatements()); 250 scan(caseStatement.getAnnotations()); 251 exit(caseStatement); 252 } 253 254 public void visitCtCatch(CtCatch catchBlock) { 255 enter(catchBlock); 256 scan(catchBlock.getBody()); 257 scan(catchBlock.getParameter()); 258 scan(catchBlock.getAnnotations()); 259 exit(catchBlock); 260 } 261 262 public <T> void visitCtClass(CtClass<T> ctClass) { 263 enter(ctClass); 264 scan(ctClass.getNestedTypes()); 265 scan(ctClass.getFields()); 266 scan(ctClass.getAnnotations()); 267 scan(ctClass.getAnonymousExecutables()); 268 scan(ctClass.getSuperclass()); 269 scan(ctClass.getConstructors()); 270 scanReferences(ctClass.getFormalTypeParameters()); 271 scanReferences(ctClass.getSuperInterfaces()); 272 scan(ctClass.getMethods()); 273 exit(ctClass); 274 } 275 276 public <T> void visitCtConditional(CtConditional<T> conditional) { 277 enter(conditional); 278 scan(conditional.getAnnotations()); 279 scan(conditional.getCondition()); 280 scan(conditional.getThenExpression()); 281 scan(conditional.getElseExpression()); 282 exit(conditional); 283 } 284 285 public <T> void visitCtConstructor(CtConstructor<T> c) { 286 enter(c); 287 scan(c.getBody()); 288 scan(c.getParameters()); 289 scan(c.getAnnotations()); 290 scanReferences(c.getThrownTypes()); 291 scanReferences(c.getFormalTypeParameters()); 292 exit(c); 293 } 294 295 public void visitCtContinue(CtContinue continueStatement) { 296 enter(continueStatement); 297 scan(continueStatement.getLabelledStatement()); 298 scan(continueStatement.getAnnotations()); 299 exit(continueStatement); 300 } 301 302 public void visitCtDo(CtDo doLoop) { 303 enter(doLoop); 304 scan(doLoop.getBody()); 305 scan(doLoop.getAnnotations()); 306 scan(doLoop.getLoopingExpression()); 307 exit(doLoop); 308 } 309 310 public <T extends Enum > void visitCtEnum(CtEnum<T> ctEnum) { 311 enter(ctEnum); 312 scan(ctEnum.getNestedTypes()); 313 scan(ctEnum.getAnnotations()); 314 scan(ctEnum.getFields()); 315 exit(ctEnum); 316 } 317 318 public <T> void visitCtExecutableReference( 319 CtExecutableReference<T> reference) { 320 enterReference(reference); 321 scanReferences(reference.getParameterTypes()); 322 scan(reference.getDeclaringType()); 323 scan(reference.getType()); 324 scanReferences(reference.getActualTypeArguments()); 325 exitReference(reference); 326 } 327 328 public <T> void visitCtField(CtField<T> f) { 329 enter(f); 330 scan(f.getDefaultExpression()); 331 scan(f.getType()); 332 scan(f.getAnnotations()); 333 exit(f); 334 } 335 336 public <T> void visitCtFieldAccess(CtFieldAccess<T> fieldAccess) { 337 enter(fieldAccess); 338 scan(fieldAccess.getVariable()); 339 scan(fieldAccess.getType()); 340 scan(fieldAccess.getAnnotations()); 341 scanReferences(fieldAccess.getTypeCasts()); 342 scan(fieldAccess.getVariable()); 343 scan(fieldAccess.getTarget()); 344 exit(fieldAccess); 345 } 346 347 public <T> void visitCtFieldReference(CtFieldReference<T> reference) { 348 enterReference(reference); 349 scan(reference.getDeclaringType()); 350 scan(reference.getType()); 351 exitReference(reference); 352 } 353 354 public void visitCtFor(CtFor forLoop) { 355 enter(forLoop); 356 scan(forLoop.getForInit()); 357 scan(forLoop.getForUpdate()); 358 scan(forLoop.getBody()); 359 scan(forLoop.getExpression()); 360 scan(forLoop.getAnnotations()); 361 exit(forLoop); 362 } 363 364 public void visitCtForEach(CtForEach foreach) { 365 enter(foreach); 366 scan(foreach.getBody()); 367 scan(foreach.getExpression()); 368 scan(foreach.getVariable()); 369 scan(foreach.getAnnotations()); 370 exit(foreach); 371 } 372 373 public void visitCtIf(CtIf ifElement) { 374 enter(ifElement); 375 scan(ifElement.getCondition()); 376 scan(ifElement.getElseStatement()); 377 scan(ifElement.getThenStatement()); 378 scan(ifElement.getAnnotations()); 379 exit(ifElement); 380 } 381 382 public <T> void visitCtInterface(CtInterface<T> intrface) { 383 enter(intrface); 384 scan(intrface.getAnnotations()); 385 scan(intrface.getNestedTypes()); 386 scan(intrface.getFields()); 387 scanReferences(intrface.getFormalTypeParameters()); 388 scanReferences(intrface.getSuperInterfaces()); 389 scan(intrface.getMethods()); 390 exit(intrface); 391 } 392 393 public <T> void visitCtInvocation(CtInvocation<T> invocation) { 394 enter(invocation); 395 scan(invocation.getType()); 396 scan(invocation.getAnnotations()); 397 scan(invocation.getExecutable()); 398 scanReferences(invocation.getTypeCasts()); 399 scan(invocation.getArguments()); 400 scan(invocation.getTarget()); 401 exit(invocation); 402 } 403 404 public <T> void visitCtLiteral(CtLiteral<T> literal) { 405 enter(literal); 406 scan(literal.getType()); 407 scan(literal.getAnnotations()); 408 scanReferences(literal.getTypeCasts()); 409 exit(literal); 410 } 411 412 public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) { 413 enter(localVariable); 414 scan(localVariable.getDefaultExpression()); 415 scan(localVariable.getType()); 416 scan(localVariable.getAnnotations()); 417 exit(localVariable); 418 } 419 420 public <T> void visitCtLocalVariableReference( 421 CtLocalVariableReference<T> reference) { 422 enterReference(reference); 423 scan(reference.getType()); 424 exitReference(reference); 425 } 426 427 public <T> void visitCtMethod(CtMethod<T> m) { 428 enter(m); 429 scan(m.getBody()); 430 scan(m.getParameters()); 431 scan(m.getType()); 432 scan(m.getAnnotations()); 433 scanReferences(m.getThrownTypes()); 434 scanReferences(m.getFormalTypeParameters()); 435 exit(m); 436 } 437 438 public <T> void visitCtNewArray(CtNewArray<T> newArray) { 439 enter(newArray); 440 scan(newArray.getDimensionExpressions()); 441 scan(newArray.getElements()); 442 scan(newArray.getType()); 443 scan(newArray.getAnnotations()); 444 scanReferences(newArray.getTypeCasts()); 445 exit(newArray); 446 } 447 448 public <T> void visitCtNewClass(CtNewClass<T> newClass) { 449 enter(newClass); 450 scan(newClass.getAnonymousClass()); 451 scan(newClass.getType()); 452 scan(newClass.getAnnotations()); 453 scan(newClass.getExecutable()); 454 scanReferences(newClass.getTypeCasts()); 455 scan(newClass.getArguments()); 456 scan(newClass.getTarget()); 457 exit(newClass); 458 } 459 460 public <T, A extends T> void visitCtOperatorAssignement( 461 CtOperatorAssignment<T, A> assignment) { 462 enter(assignment); 463 scan(assignment.getType()); 464 scan(assignment.getAnnotations()); 465 scan(assignment.getAssigned()); 466 scan(assignment.getAssignment()); 467 scanReferences(assignment.getTypeCasts()); 468 exit(assignment); 469 } 470 471 public void visitCtPackage(CtPackage ctPackage) { 472 enter(ctPackage); 473 scan(ctPackage.getPackages()); 474 scan(ctPackage.getAnnotations()); 475 scan(ctPackage.getTypes()); 476 exit(ctPackage); 477 } 478 479 public void visitCtPackageReference(CtPackageReference reference) { 480 enterReference(reference); 481 exitReference(reference); 482 } 483 484 public <T> void visitCtParameter(CtParameter<T> parameter) { 485 enter(parameter); 486 scan(parameter.getDefaultExpression()); 487 scan(parameter.getType()); 488 scan(parameter.getAnnotations()); 489 exit(parameter); 490 } 491 492 public <T> void visitCtParameterReference(CtParameterReference<T> reference) { 493 enterReference(reference); 494 scan(reference.getType()); 495 exitReference(reference); 496 } 497 498 public <R> void visitCtReturn(CtReturn<R> returnStatement) { 499 enter(returnStatement); 500 scan(returnStatement.getReturnedExpression()); 501 scan(returnStatement.getAnnotations()); 502 exit(returnStatement); 503 } 504 505 public <R> void visitCtStatementList(CtStatementList<R> statements) { 506 enter(statements); 507 scan(statements.getAnnotations()); 508 scan(statements.getStatements()); 509 exit(statements); 510 } 511 512 public <S> void visitCtSwitch(CtSwitch<S> switchStatement) { 513 enter(switchStatement); 514 scan(switchStatement.getCases()); 515 scan(switchStatement.getSelector()); 516 scan(switchStatement.getAnnotations()); 517 exit(switchStatement); 518 } 519 520 public void visitCtSynchronized(CtSynchronized synchro) { 521 enter(synchro); 522 scan(synchro.getExpression()); 523 scan(synchro.getBlock()); 524 scan(synchro.getAnnotations()); 525 exit(synchro); 526 } 527 528 public void visitCtThrow(CtThrow throwStatement) { 529 enter(throwStatement); 530 scan(throwStatement.getAnnotations()); 531 scan(throwStatement.getThrownExpression()); 532 exit(throwStatement); 533 } 534 535 public void visitCtTry(CtTry tryBlock) { 536 enter(tryBlock); 537 scan(tryBlock.getCatchers()); 538 scan(tryBlock.getFinalizer()); 539 scan(tryBlock.getAnnotations()); 540 scan(tryBlock.getBody()); 541 exit(tryBlock); 542 } 543 544 public void visitCtTypeParameter(CtTypeParameter typeParameter) { 545 enter(typeParameter); 546 scan(typeParameter.getAnnotations()); 547 scanReferences(typeParameter.getBounds()); 548 exit(typeParameter); 549 } 550 551 public void visitCtTypeParameterReference(CtTypeParameterReference ref) { 552 enterReference(ref); 553 scanReferences(ref.getBounds()); 554 scan(ref.getPackage()); 555 scanReferences(ref.getActualTypeArguments()); 556 scan(ref.getDeclaringType()); 557 exitReference(ref); 558 } 559 560 public <T> void visitCtTypeReference(CtTypeReference<T> reference) { 561 enterReference(reference); 562 scan(reference.getPackage()); 563 scanReferences(reference.getActualTypeArguments()); 564 scan(reference.getDeclaringType()); 565 exitReference(reference); 566 } 567 568 public <T> void visitCtUnaryOperator(CtUnaryOperator<T> operator) { 569 enter(operator); 570 scan(operator.getOperand()); 571 scan(operator.getType()); 572 scan(operator.getAnnotations()); 573 scanReferences(operator.getTypeCasts()); 574 exit(operator); 575 } 576 577 public <T> void visitCtVariableAccess(CtVariableAccess<T> variableAccess) { 578 enter(variableAccess); 579 scan(variableAccess.getVariable()); 580 scan(variableAccess.getType()); 581 scan(variableAccess.getAnnotations()); 582 scanReferences(variableAccess.getTypeCasts()); 583 exit(variableAccess); 584 } 585 586 public void visitCtWhile(CtWhile whileLoop) { 587 enter(whileLoop); 588 scan(whileLoop.getBody()); 589 scan(whileLoop.getLoopingExpression()); 590 scan(whileLoop.getAnnotations()); 591 exit(whileLoop); 592 } 593 594 } 595 | Popular Tags |