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.support.visitor;
019:
020: import java.lang.annotation.Annotation;
021:
022: import spoon.reflect.code.CtArrayAccess;
023: import spoon.reflect.code.CtAssert;
024: import spoon.reflect.code.CtAssignment;
025: import spoon.reflect.code.CtBinaryOperator;
026: import spoon.reflect.code.CtBlock;
027: import spoon.reflect.code.CtBreak;
028: import spoon.reflect.code.CtCase;
029: import spoon.reflect.code.CtCatch;
030: import spoon.reflect.code.CtCodeSnippetExpression;
031: import spoon.reflect.code.CtCodeSnippetStatement;
032: import spoon.reflect.code.CtConditional;
033: import spoon.reflect.code.CtContinue;
034: import spoon.reflect.code.CtDo;
035: import spoon.reflect.code.CtExpression;
036: import spoon.reflect.code.CtFieldAccess;
037: import spoon.reflect.code.CtFor;
038: import spoon.reflect.code.CtForEach;
039: import spoon.reflect.code.CtIf;
040: import spoon.reflect.code.CtInvocation;
041: import spoon.reflect.code.CtLiteral;
042: import spoon.reflect.code.CtLocalVariable;
043: import spoon.reflect.code.CtNewArray;
044: import spoon.reflect.code.CtNewClass;
045: import spoon.reflect.code.CtOperatorAssignment;
046: import spoon.reflect.code.CtReturn;
047: import spoon.reflect.code.CtStatement;
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.CtExecutable;
064: import spoon.reflect.declaration.CtField;
065: import spoon.reflect.declaration.CtInterface;
066: import spoon.reflect.declaration.CtMethod;
067: import spoon.reflect.declaration.CtPackage;
068: import spoon.reflect.declaration.CtParameter;
069: import spoon.reflect.declaration.CtTypeParameter;
070: import spoon.reflect.reference.CtArrayTypeReference;
071: import spoon.reflect.reference.CtExecutableReference;
072: import spoon.reflect.reference.CtFieldReference;
073: import spoon.reflect.reference.CtLocalVariableReference;
074: import spoon.reflect.reference.CtPackageReference;
075: import spoon.reflect.reference.CtParameterReference;
076: import spoon.reflect.reference.CtReference;
077: import spoon.reflect.reference.CtTypeParameterReference;
078: import spoon.reflect.reference.CtTypeReference;
079: import spoon.reflect.visitor.CtVisitor;
080:
081: public class SignaturePrinter implements CtVisitor {
082: StringBuffer signature;
083:
084: public SignaturePrinter() {
085: super ();
086: reset();
087: }
088:
089: public String getSignature() {
090: return signature.toString();
091: }
092:
093: public void reset() {
094: signature = new StringBuffer();
095: }
096:
097: public void scan(CtElement e) {
098: if (e != null)
099: e.accept(this );
100: }
101:
102: public void scan(CtReference e) {
103: if (e != null)
104: e.accept(this );
105: }
106:
107: protected SignaturePrinter write(String value) {
108: signature.append(value);
109: return this ;
110: }
111:
112: private SignaturePrinter clearLast() {
113: signature.deleteCharAt(signature.length() - 1);
114: return this ;
115: }
116:
117: public <A extends Annotation> void visitCtAnnotation(
118: CtAnnotation<A> annotation) {
119: write("@").write(
120: annotation.getAnnotationType().getQualifiedName());
121: }
122:
123: public <A extends Annotation> void visitCtAnnotationType(
124: CtAnnotationType<A> annotationType) {
125: write("@interface ");
126: write(annotationType.getQualifiedName());
127: }
128:
129: public void visitCtAnonymousExecutable(CtAnonymousExecutable e) {
130: scan(e.getBody());
131: }
132:
133: public <T, E extends CtExpression<?>> void visitCtArrayAccess(
134: CtArrayAccess<T, E> arrayAccess) {
135: scan(arrayAccess.getTarget());
136: write("[");
137: scan(arrayAccess.getIndexExpression());
138: write("]");
139: }
140:
141: public <T> void visitCtArrayTypeReference(
142: CtArrayTypeReference<T> reference) {
143: scan(reference.getComponentType());
144: write("[]");
145: }
146:
147: public <T> void visitCtAssert(CtAssert<T> asserted) {
148: signature.append("assert ");
149: scan(asserted.getAssertExpression());
150: signature.append(":");
151: scan(asserted.getExpression());
152: }
153:
154: public <T, A extends T> void visitCtAssignment(
155: CtAssignment<T, A> assignement) {
156: for (CtTypeReference<?> ref : assignement.getTypeCasts()) {
157: write("(");
158: scan(ref);
159: write(")");
160: }
161: write("(");
162: scan(assignement.getAssigned());
163: write(" = ");
164: scan(assignement.getAssignment());
165: write(")");
166: }
167:
168: public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
169: scan(operator.getLeftHandOperand());
170: write(operator.getKind().toString());
171: scan(operator.getRightHandOperand());
172: }
173:
174: public <R> void visitCtBlock(CtBlock<R> block) {
175: signature.append("{\n");
176: for (CtStatement s : block.getStatements()) {
177: scan(s);
178: signature.append(";\n");
179: }
180: signature.append("}");
181: }
182:
183: public void visitCtBreak(CtBreak breakStatement) {
184: write("break ");
185: if (breakStatement.getTargetLabel() != null)
186: write(breakStatement.getTargetLabel());
187: }
188:
189: public <E> void visitCtCase(CtCase<E> caseStatement) {
190: write("case (");
191: scan(caseStatement.getCaseExpression());
192: write(")");
193: }
194:
195: public void visitCtCatch(CtCatch catchBlock) {
196: write("catch (");
197: scan(catchBlock.getParameter().getType());
198: write(")");
199: }
200:
201: public <T> void visitCtClass(CtClass<T> ctClass) {
202: write("class ").write(ctClass.getQualifiedName());
203: }
204:
205: public <T> void visitCtConditional(CtConditional<T> conditional) {
206: scan(conditional.getCondition());
207: write("?");
208: scan(conditional.getThenExpression());
209: write(":");
210: scan(conditional.getElseExpression());
211: }
212:
213: public <T> void visitCtConstructor(CtConstructor<T> c) {
214: write(c.getDeclaringType().getQualifiedName());
215: write("(");
216: for (CtParameter<?> p : c.getParameters()) {
217: scan(p.getType());
218: write(",");
219: }
220: if (!c.getParameters().isEmpty())
221: clearLast();
222: write(")");
223: }
224:
225: public void visitCtContinue(CtContinue continueStatement) {
226: signature.append("continue ");
227: scan(continueStatement.getLabelledStatement());
228: }
229:
230: public void visitCtDo(CtDo doLoop) {
231: write("do ");
232: scan(doLoop.getBody());
233: write(" while (");
234: scan(doLoop.getLoopingExpression());
235: write(")");
236: }
237:
238: public <T extends Enum<?>> void visitCtEnum(CtEnum<T> ctEnum) {
239: write("enum ").write(ctEnum.getQualifiedName());
240: }
241:
242: public <T> void visitCtExecutableReference(
243: CtExecutableReference<T> reference) {
244: write(reference.getType().getQualifiedName());
245: write(" ");
246: write(reference.getDeclaringType().getQualifiedName());
247: write(CtExecutable.EXECUTABLE_SEPARATOR);
248: write(reference.getSimpleName());
249: write("(");
250: for (CtTypeReference<?> ref : reference.getParameterTypes()) {
251: scan(ref);
252: write(",");
253: }
254: if (!reference.getParameterTypes().isEmpty())
255: clearLast();
256: write(")");
257: }
258:
259: public <T> void visitCtField(CtField<T> f) {
260: scan(f.getType());
261: write(" ").write(f.getSimpleName());
262: }
263:
264: public <T> void visitCtFieldAccess(CtFieldAccess<T> fieldAccess) {
265: scan(fieldAccess.getTarget());
266: }
267:
268: public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
269: // TODO: Fix this null pointer catch
270: try {
271: write(reference.getType().getQualifiedName()).write(" ");
272: write(reference.getDeclaringType().getQualifiedName());
273: write(CtField.FIELD_SEPARATOR);
274: write(reference.getSimpleName());
275: } catch (NullPointerException npe) {
276: System.err
277: .println("Null Pointer Exception in SingnaturePrinter.visitCtFieldReference()");
278: }
279: }
280:
281: public void visitCtFor(CtFor forLoop) {
282: write("for (");
283: for (CtStatement s : forLoop.getForInit()) {
284: scan(s);
285: write(",");
286: }
287: if (!forLoop.getForInit().isEmpty())
288: clearLast();
289: write(";");
290: scan(forLoop.getExpression());
291: write(";");
292: for (CtStatement s : forLoop.getForUpdate()) {
293: scan(s);
294: write(",");
295: }
296: if (!forLoop.getForUpdate().isEmpty())
297: clearLast();
298: write(")");
299: scan(forLoop.getBody());
300: }
301:
302: public void visitCtForEach(CtForEach foreach) {
303: write("for (");
304: scan(foreach.getVariable());
305: write(":");
306: scan(foreach.getExpression());
307: write(")");
308: scan(foreach.getBody());
309: }
310:
311: public void visitCtIf(CtIf ifElement) {
312: write("if (");
313: scan(ifElement.getCondition());
314: write(") then ");
315: scan(ifElement.getThenStatement());
316: write(" elseĀ ");
317: scan(ifElement.getElseStatement());
318: }
319:
320: public <T> void visitCtInterface(CtInterface<T> intrface) {
321: write("interface ");
322: write(intrface.getQualifiedName());
323: }
324:
325: public <T> void visitCtInvocation(CtInvocation<T> invocation) {
326: scan(invocation.getTarget());
327: write(".");
328: scan(invocation.getExecutable());
329: write("(");
330: for (CtExpression<?> e : invocation.getArguments()) {
331: scan(e);
332: write(",");
333: }
334: if (!invocation.getArguments().isEmpty())
335: clearLast();
336: write(")");
337: }
338:
339: public <T> void visitCtLiteral(CtLiteral<T> literal) {
340: if (literal.getValue() != null)
341: write(literal.getValue().toString());
342: }
343:
344: public <T> void visitCtLocalVariable(
345: CtLocalVariable<T> localVariable) {
346: write(localVariable.getSimpleName());
347: }
348:
349: public <T> void visitCtLocalVariableReference(
350: CtLocalVariableReference<T> reference) {
351: scan(reference.getDeclaration());
352: }
353:
354: public <T> void visitCtMethod(CtMethod<T> m) {
355: scan(m.getType());
356: write(" ");
357: write(m.getSimpleName());
358: write("(");
359: for (CtParameter<?> p : m.getParameters()) {
360: scan(p.getType());
361: write(",");
362: }
363: if (!m.getParameters().isEmpty())
364: clearLast();
365: write(")");
366: }
367:
368: public <T> void visitCtNewArray(CtNewArray<T> newArray) {
369: write("new ");
370: scan(newArray.getType());
371: for (CtExpression<?> c : newArray.getDimensionExpressions()) {
372: write("[");
373: scan(c);
374: write("]");
375: }
376: write("{");
377: for (CtExpression<?> e : newArray.getElements()) {
378: scan(e);
379: write(",");
380: }
381: if (!newArray.getElements().isEmpty())
382: clearLast();
383: write("}");
384: }
385:
386: public <T> void visitCtNewClass(CtNewClass<T> newClass) {
387: write("new ");
388: scan(newClass.getExecutable());
389: scan(newClass.getAnonymousClass());
390: }
391:
392: public <T> void visitCtCodeSnippetExpression(
393: CtCodeSnippetExpression<T> expression) {
394: write(expression.getValue());
395: }
396:
397: public void visitCtCodeSnippetStatement(
398: CtCodeSnippetStatement statement) {
399: write(statement.getValue());
400: }
401:
402: public <T, A extends T> void visitCtOperatorAssignement(
403: CtOperatorAssignment<T, A> assignment) {
404: scan(assignment.getAssigned());
405: write(assignment.getKind().toString());
406: scan(assignment.getAssignment());
407: }
408:
409: public void visitCtPackage(CtPackage ctPackage) {
410: write(ctPackage.getQualifiedName());
411: }
412:
413: public void visitCtPackageReference(CtPackageReference reference) {
414: write(reference.getSimpleName());
415: }
416:
417: public <T> void visitCtParameter(CtParameter<T> parameter) {
418: write(parameter.getSimpleName());
419: }
420:
421: public <T> void visitCtParameterReference(
422: CtParameterReference<T> reference) {
423: write(reference.getSimpleName());
424: }
425:
426: public <R> void visitCtReturn(CtReturn<R> returnStatement) {
427: write("return ");
428: scan(returnStatement.getReturnedExpression());
429: }
430:
431: public <R> void visitCtStatementList(CtStatementList<R> statements) {
432: for (CtStatement s : statements.getStatements()) {
433: scan(s);
434: write(";\n");
435: }
436: }
437:
438: public <E> void visitCtSwitch(CtSwitch<E> switchStatement) {
439: write("switch(");
440: scan(switchStatement.getSelector());
441: write(")");
442: for (CtCase<?> c : switchStatement.getCases())
443: scan(c);
444: }
445:
446: public void visitCtSynchronized(CtSynchronized synchro) {
447: write("synchronized (");
448: scan(synchro.getExpression());
449: write(") ");
450: scan(synchro.getBlock());
451: }
452:
453: public void visitCtThrow(CtThrow throwStatement) {
454: write("throw ");
455: scan(throwStatement.getThrownExpression());
456: }
457:
458: public void visitCtTry(CtTry tryBlock) {
459: write("try {\n");
460: scan(tryBlock.getBody());
461: for (CtCatch c : tryBlock.getCatchers()) {
462: scan(c);
463: }
464: scan(tryBlock.getFinalizer());
465: }
466:
467: public void visitCtTypeParameter(CtTypeParameter typeParameter) {
468: write("<");
469: write(typeParameter.getName());
470: write(">");
471: }
472:
473: public void visitCtTypeParameterReference(
474: CtTypeParameterReference ref) {
475: write(ref.getQualifiedName());
476: }
477:
478: public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
479: write(reference.getQualifiedName());
480: }
481:
482: public <T> void visitCtUnaryOperator(CtUnaryOperator<T> operator) {
483: scan(operator.getOperand());
484: write(operator.getKind().toString());
485: }
486:
487: public <T> void visitCtVariableAccess(
488: CtVariableAccess<T> variableAccess) {
489: scan(variableAccess.getVariable());
490: }
491:
492: public void visitCtWhile(CtWhile whileLoop) {
493: write("while (");
494: scan(whileLoop.getLoopingExpression());
495: write(")");
496: scan(whileLoop.getBody());
497: }
498:
499: }
|