001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.reflect.visitor;
019:
020: import java.lang.annotation.Annotation;
021: import java.util.Collection;
022:
023: import spoon.reflect.code.CtArrayAccess;
024: import spoon.reflect.code.CtAssert;
025: import spoon.reflect.code.CtAssignment;
026: import spoon.reflect.code.CtBinaryOperator;
027: import spoon.reflect.code.CtBlock;
028: import spoon.reflect.code.CtBreak;
029: import spoon.reflect.code.CtCase;
030: import spoon.reflect.code.CtCatch;
031: import spoon.reflect.code.CtCodeSnippetExpression;
032: import spoon.reflect.code.CtCodeSnippetStatement;
033: import spoon.reflect.code.CtConditional;
034: import spoon.reflect.code.CtContinue;
035: import spoon.reflect.code.CtDo;
036: import spoon.reflect.code.CtExpression;
037: import spoon.reflect.code.CtFieldAccess;
038: import spoon.reflect.code.CtFor;
039: import spoon.reflect.code.CtForEach;
040: import spoon.reflect.code.CtIf;
041: import spoon.reflect.code.CtInvocation;
042: import spoon.reflect.code.CtLiteral;
043: import spoon.reflect.code.CtLocalVariable;
044: import spoon.reflect.code.CtNewArray;
045: import spoon.reflect.code.CtNewClass;
046: import spoon.reflect.code.CtOperatorAssignment;
047: import spoon.reflect.code.CtReturn;
048: import spoon.reflect.code.CtStatementList;
049: import spoon.reflect.code.CtSwitch;
050: import spoon.reflect.code.CtSynchronized;
051: import spoon.reflect.code.CtThrow;
052: import spoon.reflect.code.CtTry;
053: import spoon.reflect.code.CtUnaryOperator;
054: import spoon.reflect.code.CtVariableAccess;
055: import spoon.reflect.code.CtWhile;
056: import spoon.reflect.declaration.CtAnnotation;
057: import spoon.reflect.declaration.CtAnnotationType;
058: import spoon.reflect.declaration.CtAnonymousExecutable;
059: import spoon.reflect.declaration.CtClass;
060: import spoon.reflect.declaration.CtConstructor;
061: import spoon.reflect.declaration.CtElement;
062: import spoon.reflect.declaration.CtEnum;
063: import spoon.reflect.declaration.CtField;
064: import spoon.reflect.declaration.CtInterface;
065: import spoon.reflect.declaration.CtMethod;
066: import spoon.reflect.declaration.CtPackage;
067: import spoon.reflect.declaration.CtParameter;
068: import spoon.reflect.declaration.CtTypeParameter;
069: import spoon.reflect.reference.CtArrayTypeReference;
070: import spoon.reflect.reference.CtExecutableReference;
071: import spoon.reflect.reference.CtFieldReference;
072: import spoon.reflect.reference.CtLocalVariableReference;
073: import spoon.reflect.reference.CtPackageReference;
074: import spoon.reflect.reference.CtParameterReference;
075: import spoon.reflect.reference.CtReference;
076: import spoon.reflect.reference.CtTypeParameterReference;
077: import spoon.reflect.reference.CtTypeReference;
078:
079: /**
080: * This visitor implements a deep-search scan on the metamodel.
081: */
082: public class CtScanner implements CtVisitor {
083: /**
084: * Default constructor.
085: */
086: public CtScanner() {
087: super ();
088: }
089:
090: /**
091: * This method is upcalled by the scanner when entering a scanned element.
092: * To be overriden to implement specific scanners.
093: */
094: protected void enter(CtElement e) {
095: }
096:
097: /**
098: * This method is upcalled by the scanner when entering a scanned element
099: * reference. To be overriden to implement specific scanners.
100: */
101: protected void enterReference(CtReference e) {
102: }
103:
104: /**
105: * This method is upcalled by the scanner when exiting a scanned element. To
106: * be overriden to implement specific scanners.
107: */
108: protected void exit(CtElement e) {
109: }
110:
111: /**
112: * This method is upcalled by the scanner when exiting a scanned element
113: * reference. To be overriden to implement specific scanners.
114: */
115: protected void exitReference(CtReference e) {
116: }
117:
118: /**
119: * Generically scans a collection of meta-model elements.
120: */
121: public void scan(Collection<? extends CtElement> elements) {
122: if ((elements != null)) {
123: for (CtElement e : elements) {
124: scan(e);
125: }
126: }
127:
128: }
129:
130: /**
131: * Generically scans a meta-model element.
132: */
133: public void scan(CtElement element) {
134: if ((element != null))
135: element.accept(this );
136:
137: }
138:
139: /**
140: * Generically scans a meta-model element reference.
141: */
142: public void scan(CtReference reference) {
143: if ((reference != null))
144: reference.accept(this );
145:
146: }
147:
148: /**
149: * Generically scans a collection of meta-model references.
150: */
151: public void scanReferences(
152: Collection<? extends CtReference> references) {
153: if ((references != null)) {
154: for (CtReference r : references) {
155: scan(r);
156: }
157: }
158:
159: }
160:
161: public <A extends Annotation> void visitCtAnnotation(
162: CtAnnotation<A> annotation) {
163: enter(annotation);
164: scan(annotation.getAnnotationType());
165: scan(annotation.getAnnotations());
166: for (Object o : annotation.getElementValues().values()) {
167: scan(o);
168: }
169: exit(annotation);
170: }
171:
172: /**
173: * Generically scans an object that can be an element, a reference, or a
174: * collection of those.
175: */
176: public void scan(Object o) {
177: if (o instanceof CtElement)
178: scan((CtElement) o);
179: if (o instanceof CtReference)
180: scan((CtReference) o);
181: if (o instanceof Collection<?>) {
182: for (Object obj : (Collection<?>) o) {
183: scan(obj);
184: }
185: }
186: }
187:
188: public <A extends Annotation> void visitCtAnnotationType(
189: CtAnnotationType<A> annotationType) {
190: enter(annotationType);
191: scan(annotationType.getAnnotations());
192: scan(annotationType.getNestedTypes());
193: scan(annotationType.getFields());
194: exit(annotationType);
195: }
196:
197: public void visitCtAnonymousExecutable(
198: CtAnonymousExecutable anonymousExec) {
199: enter(anonymousExec);
200: scan(anonymousExec.getAnnotations());
201: scan(anonymousExec.getBody());
202: exit(anonymousExec);
203: }
204:
205: public <T, E extends CtExpression<?>> void visitCtArrayAccess(
206: CtArrayAccess<T, E> arrayAccess) {
207: enter(arrayAccess);
208: scan(arrayAccess.getAnnotations());
209: scan(arrayAccess.getType());
210: scanReferences(arrayAccess.getTypeCasts());
211: scan(arrayAccess.getTarget());
212: scan(arrayAccess.getIndexExpression());
213: exit(arrayAccess);
214: }
215:
216: public <T> void visitCtArrayTypeReference(
217: CtArrayTypeReference<T> reference) {
218: enterReference(reference);
219: scan(reference.getDeclaringType());
220: scan(reference.getPackage());
221: scan(reference.getComponentType());
222: scanReferences(reference.getActualTypeArguments());
223: exitReference(reference);
224: }
225:
226: public <T> void visitCtAssert(CtAssert<T> asserted) {
227: enter(asserted);
228: scan(asserted.getAnnotations());
229: scan(asserted.getAssertExpression());
230: scan(asserted.getExpression());
231: exit(asserted);
232: }
233:
234: public <T, A extends T> void visitCtAssignment(
235: CtAssignment<T, A> assignement) {
236: enter(assignement);
237: scan(assignement.getAnnotations());
238: scan(assignement.getType());
239: scanReferences(assignement.getTypeCasts());
240: scan(assignement.getAssigned());
241: scan(assignement.getAssignment());
242: exit(assignement);
243: }
244:
245: public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
246: enter(operator);
247: scan(operator.getAnnotations());
248: scan(operator.getType());
249: scanReferences(operator.getTypeCasts());
250: scan(operator.getLeftHandOperand());
251: scan(operator.getRightHandOperand());
252: exit(operator);
253: }
254:
255: public <R> void visitCtBlock(CtBlock<R> block) {
256: enter(block);
257: scan(block.getAnnotations());
258: scan(block.getStatements());
259: exit(block);
260: }
261:
262: public void visitCtBreak(CtBreak breakStatement) {
263: enter(breakStatement);
264: scan(breakStatement.getAnnotations());
265: exit(breakStatement);
266: }
267:
268: public <S> void visitCtCase(CtCase<S> caseStatement) {
269: enter(caseStatement);
270: scan(caseStatement.getAnnotations());
271: scan(caseStatement.getCaseExpression());
272: scan(caseStatement.getStatements());
273: exit(caseStatement);
274: }
275:
276: public void visitCtCatch(CtCatch catchBlock) {
277: enter(catchBlock);
278: scan(catchBlock.getAnnotations());
279: scan(catchBlock.getParameter());
280: scan(catchBlock.getBody());
281: exit(catchBlock);
282: }
283:
284: public <T> void visitCtClass(CtClass<T> ctClass) {
285: enter(ctClass);
286: scan(ctClass.getAnnotations());
287: scan(ctClass.getSuperclass());
288: scanReferences(ctClass.getSuperInterfaces());
289: scanReferences(ctClass.getFormalTypeParameters());
290: scan(ctClass.getAnonymousExecutables());
291: scan(ctClass.getNestedTypes());
292: scan(ctClass.getFields());
293: scan(ctClass.getConstructors());
294: scan(ctClass.getMethods());
295: exit(ctClass);
296: }
297:
298: public <T> void visitCtConditional(CtConditional<T> conditional) {
299: enter(conditional);
300: scan(conditional.getAnnotations());
301: scan(conditional.getCondition());
302: scan(conditional.getThenExpression());
303: scan(conditional.getElseExpression());
304: exit(conditional);
305: }
306:
307: public <T> void visitCtConstructor(CtConstructor<T> c) {
308: enter(c);
309: scan(c.getAnnotations());
310: scan(c.getParameters());
311: scanReferences(c.getThrownTypes());
312: scanReferences(c.getFormalTypeParameters());
313: scan(c.getBody());
314: exit(c);
315: }
316:
317: public void visitCtContinue(CtContinue continueStatement) {
318: enter(continueStatement);
319: scan(continueStatement.getAnnotations());
320: scan(continueStatement.getLabelledStatement());
321: exit(continueStatement);
322: }
323:
324: public void visitCtDo(CtDo doLoop) {
325: enter(doLoop);
326: scan(doLoop.getAnnotations());
327: scan(doLoop.getLoopingExpression());
328: scan(doLoop.getBody());
329: exit(doLoop);
330: }
331:
332: public <T extends Enum<?>> void visitCtEnum(CtEnum<T> ctEnum) {
333: enter(ctEnum);
334: scan(ctEnum.getAnnotations());
335: scan(ctEnum.getFields());
336: scan(ctEnum.getNestedTypes());
337: exit(ctEnum);
338: }
339:
340: public <T> void visitCtExecutableReference(
341: CtExecutableReference<T> reference) {
342: enterReference(reference);
343: scan(reference.getDeclaringType());
344: scan(reference.getType());
345: scanReferences(reference.getActualTypeArguments());
346: scanReferences(reference.getParameterTypes());
347: exitReference(reference);
348: }
349:
350: public <T> void visitCtField(CtField<T> f) {
351: enter(f);
352: scan(f.getAnnotations());
353: scan(f.getType());
354: scan(f.getDefaultExpression());
355: exit(f);
356: }
357:
358: public <T> void visitCtFieldAccess(CtFieldAccess<T> fieldAccess) {
359: enter(fieldAccess);
360: scan(fieldAccess.getAnnotations());
361: scan(fieldAccess.getType());
362: scanReferences(fieldAccess.getTypeCasts());
363: scan(fieldAccess.getTarget());
364: scan(fieldAccess.getVariable());
365: exit(fieldAccess);
366: }
367:
368: public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
369: enterReference(reference);
370: scan(reference.getDeclaringType());
371: scan(reference.getType());
372: exitReference(reference);
373: }
374:
375: public void visitCtFor(CtFor forLoop) {
376: enter(forLoop);
377: scan(forLoop.getAnnotations());
378: scan(forLoop.getForInit());
379: scan(forLoop.getExpression());
380: scan(forLoop.getForUpdate());
381: scan(forLoop.getBody());
382: exit(forLoop);
383: }
384:
385: public void visitCtForEach(CtForEach foreach) {
386: enter(foreach);
387: scan(foreach.getAnnotations());
388: scan(foreach.getVariable());
389: scan(foreach.getExpression());
390: scan(foreach.getBody());
391: exit(foreach);
392: }
393:
394: public void visitCtIf(CtIf ifElement) {
395: enter(ifElement);
396: scan(ifElement.getAnnotations());
397: scan(ifElement.getCondition());
398: scan(ifElement.getThenStatement());
399: scan(ifElement.getElseStatement());
400: exit(ifElement);
401: }
402:
403: public <T> void visitCtInterface(CtInterface<T> intrface) {
404: enter(intrface);
405: scan(intrface.getAnnotations());
406: scanReferences(intrface.getSuperInterfaces());
407: scanReferences(intrface.getFormalTypeParameters());
408: scan(intrface.getNestedTypes());
409: scan(intrface.getFields());
410: scan(intrface.getMethods());
411: exit(intrface);
412: }
413:
414: public <T> void visitCtInvocation(CtInvocation<T> invocation) {
415: enter(invocation);
416: scan(invocation.getAnnotations());
417: scan(invocation.getType());
418: scanReferences(invocation.getTypeCasts());
419: scan(invocation.getTarget());
420: scan(invocation.getExecutable());
421: scan(invocation.getArguments());
422: exit(invocation);
423: }
424:
425: public <T> void visitCtLiteral(CtLiteral<T> literal) {
426: enter(literal);
427: scan(literal.getAnnotations());
428: scan(literal.getType());
429: scanReferences(literal.getTypeCasts());
430: exit(literal);
431: }
432:
433: public <T> void visitCtLocalVariable(
434: CtLocalVariable<T> localVariable) {
435: enter(localVariable);
436: scan(localVariable.getAnnotations());
437: scan(localVariable.getType());
438: scan(localVariable.getDefaultExpression());
439: exit(localVariable);
440: }
441:
442: public <T> void visitCtLocalVariableReference(
443: CtLocalVariableReference<T> reference) {
444: enterReference(reference);
445: scan(reference.getType());
446: exitReference(reference);
447: }
448:
449: public <T> void visitCtMethod(CtMethod<T> m) {
450: enter(m);
451: scan(m.getAnnotations());
452: scan(m.getType());
453: scan(m.getParameters());
454: scanReferences(m.getThrownTypes());
455: scanReferences(m.getFormalTypeParameters());
456: scan(m.getBody());
457: exit(m);
458: }
459:
460: public <T> void visitCtNewArray(CtNewArray<T> newArray) {
461: enter(newArray);
462: scan(newArray.getAnnotations());
463: scan(newArray.getType());
464: scanReferences(newArray.getTypeCasts());
465: scan(newArray.getElements());
466: scan(newArray.getDimensionExpressions());
467: exit(newArray);
468: }
469:
470: public <T> void visitCtNewClass(CtNewClass<T> newClass) {
471: enter(newClass);
472: scan(newClass.getAnnotations());
473: scan(newClass.getType());
474: scanReferences(newClass.getTypeCasts());
475: scan(newClass.getExecutable());
476: scan(newClass.getTarget());
477: scan(newClass.getArguments());
478: scan(newClass.getAnonymousClass());
479: exit(newClass);
480: }
481:
482: public <T, A extends T> void visitCtOperatorAssignement(
483: CtOperatorAssignment<T, A> assignment) {
484: enter(assignment);
485: scan(assignment.getAnnotations());
486: scan(assignment.getType());
487: scanReferences(assignment.getTypeCasts());
488: scan(assignment.getAssigned());
489: scan(assignment.getAssignment());
490: exit(assignment);
491: }
492:
493: public void visitCtPackage(CtPackage ctPackage) {
494: enter(ctPackage);
495: scan(ctPackage.getAnnotations());
496: scan(ctPackage.getPackages());
497: scan(ctPackage.getTypes());
498: exit(ctPackage);
499: }
500:
501: public void visitCtPackageReference(CtPackageReference reference) {
502: enterReference(reference);
503: exitReference(reference);
504: }
505:
506: public <T> void visitCtParameter(CtParameter<T> parameter) {
507: enter(parameter);
508: scan(parameter.getAnnotations());
509: scan(parameter.getType());
510: scan(parameter.getDefaultExpression());
511: exit(parameter);
512: }
513:
514: public <T> void visitCtParameterReference(
515: CtParameterReference<T> reference) {
516: enterReference(reference);
517: scan(reference.getType());
518: exitReference(reference);
519: }
520:
521: public <R> void visitCtReturn(CtReturn<R> returnStatement) {
522: enter(returnStatement);
523: scan(returnStatement.getAnnotations());
524: scan(returnStatement.getReturnedExpression());
525: exit(returnStatement);
526: }
527:
528: public <R> void visitCtStatementList(CtStatementList<R> statements) {
529: enter(statements);
530: scan(statements.getAnnotations());
531: scan(statements.getStatements());
532: exit(statements);
533: }
534:
535: public <S> void visitCtSwitch(CtSwitch<S> switchStatement) {
536: enter(switchStatement);
537: scan(switchStatement.getAnnotations());
538: scan(switchStatement.getSelector());
539: scan(switchStatement.getCases());
540: exit(switchStatement);
541: }
542:
543: public void visitCtSynchronized(CtSynchronized synchro) {
544: enter(synchro);
545: scan(synchro.getAnnotations());
546: scan(synchro.getExpression());
547: scan(synchro.getBlock());
548: exit(synchro);
549: }
550:
551: public void visitCtThrow(CtThrow throwStatement) {
552: enter(throwStatement);
553: scan(throwStatement.getAnnotations());
554: scan(throwStatement.getThrownExpression());
555: exit(throwStatement);
556: }
557:
558: public void visitCtTry(CtTry tryBlock) {
559: enter(tryBlock);
560: scan(tryBlock.getAnnotations());
561: scan(tryBlock.getBody());
562: scan(tryBlock.getCatchers());
563: scan(tryBlock.getFinalizer());
564: exit(tryBlock);
565: }
566:
567: public void visitCtTypeParameter(CtTypeParameter typeParameter) {
568: enter(typeParameter);
569: scan(typeParameter.getAnnotations());
570: scanReferences(typeParameter.getBounds());
571: exit(typeParameter);
572: }
573:
574: public void visitCtTypeParameterReference(
575: CtTypeParameterReference ref) {
576: enterReference(ref);
577: scan(ref.getPackage());
578: scan(ref.getDeclaringType());
579: scanReferences(ref.getActualTypeArguments());
580: scanReferences(ref.getBounds());
581: exitReference(ref);
582: }
583:
584: public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
585: enterReference(reference);
586: scan(reference.getPackage());
587: scan(reference.getDeclaringType());
588: scanReferences(reference.getActualTypeArguments());
589: exitReference(reference);
590: }
591:
592: public <T> void visitCtUnaryOperator(CtUnaryOperator<T> operator) {
593: enter(operator);
594: scan(operator.getAnnotations());
595: scan(operator.getType());
596: scanReferences(operator.getTypeCasts());
597: scan(operator.getOperand());
598: exit(operator);
599: }
600:
601: public <T> void visitCtVariableAccess(
602: CtVariableAccess<T> variableAccess) {
603: enter(variableAccess);
604: scan(variableAccess.getAnnotations());
605: scan(variableAccess.getType());
606: scanReferences(variableAccess.getTypeCasts());
607: scan(variableAccess.getVariable());
608: exit(variableAccess);
609: }
610:
611: public void visitCtWhile(CtWhile whileLoop) {
612: enter(whileLoop);
613: scan(whileLoop.getAnnotations());
614: scan(whileLoop.getLoopingExpression());
615: scan(whileLoop.getBody());
616: exit(whileLoop);
617: }
618:
619: public <T> void visitCtCodeSnippetExpression(
620: CtCodeSnippetExpression<T> expression) {
621: }
622:
623: public void visitCtCodeSnippetStatement(
624: CtCodeSnippetStatement statement) {
625: }
626:
627: }
|