KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > visitor > SignaturePrinter


1 package spoon.support.visitor;
2
3 import java.lang.annotation.Annotation JavaDoc;
4
5 import spoon.reflect.code.CtArrayAccess;
6 import spoon.reflect.code.CtAssert;
7 import spoon.reflect.code.CtAssignment;
8 import spoon.reflect.code.CtBinaryOperator;
9 import spoon.reflect.code.CtBlock;
10 import spoon.reflect.code.CtBreak;
11 import spoon.reflect.code.CtCase;
12 import spoon.reflect.code.CtCatch;
13 import spoon.reflect.code.CtConditional;
14 import spoon.reflect.code.CtContinue;
15 import spoon.reflect.code.CtDo;
16 import spoon.reflect.code.CtExpression;
17 import spoon.reflect.code.CtFieldAccess;
18 import spoon.reflect.code.CtFor;
19 import spoon.reflect.code.CtForEach;
20 import spoon.reflect.code.CtIf;
21 import spoon.reflect.code.CtInvocation;
22 import spoon.reflect.code.CtLiteral;
23 import spoon.reflect.code.CtLocalVariable;
24 import spoon.reflect.code.CtNewArray;
25 import spoon.reflect.code.CtNewClass;
26 import spoon.reflect.code.CtOperatorAssignment;
27 import spoon.reflect.code.CtReturn;
28 import spoon.reflect.code.CtStatement;
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.CtExecutable;
45 import spoon.reflect.declaration.CtField;
46 import spoon.reflect.declaration.CtInterface;
47 import spoon.reflect.declaration.CtMethod;
48 import spoon.reflect.declaration.CtPackage;
49 import spoon.reflect.declaration.CtParameter;
50 import spoon.reflect.declaration.CtTypeParameter;
51 import spoon.reflect.reference.CtArrayTypeReference;
52 import spoon.reflect.reference.CtExecutableReference;
53 import spoon.reflect.reference.CtFieldReference;
54 import spoon.reflect.reference.CtLocalVariableReference;
55 import spoon.reflect.reference.CtPackageReference;
56 import spoon.reflect.reference.CtParameterReference;
57 import spoon.reflect.reference.CtReference;
58 import spoon.reflect.reference.CtTypeParameterReference;
59 import spoon.reflect.reference.CtTypeReference;
60 import spoon.reflect.visitor.CtVisitor;
61
62 public class SignaturePrinter implements CtVisitor {
63     StringBuffer JavaDoc signature;
64
65     public SignaturePrinter() {
66         super();
67         reset();
68     }
69
70     public String JavaDoc getSignature() {
71         return signature.toString();
72     }
73
74     public void reset() {
75         signature = new StringBuffer JavaDoc();
76     }
77
78     public void scan(CtElement e) {
79         if (e != null)
80             e.accept(this);
81     }
82
83     public void scan(CtReference e) {
84         if (e != null)
85             e.accept(this);
86     }
87
88     protected SignaturePrinter write(String JavaDoc value) {
89         signature.append(value);
90         return this;
91     }
92
93     private SignaturePrinter clearLast() {
94         signature.deleteCharAt(signature.length() - 1);
95         return this;
96     }
97
98     public <A extends Annotation JavaDoc> void visitCtAnnotation(
99             CtAnnotation<A> annotation) {
100         write("@").write(annotation.getAnnotationType().getQualifiedName());
101     }
102
103     public <A extends Annotation JavaDoc> void visitCtAnnotationType(
104             CtAnnotationType<A> annotationType) {
105         write("@interface ");
106         write(annotationType.getQualifiedName());
107     }
108
109     public void visitCtAnonymousExecutable(CtAnonymousExecutable e) {
110         scan(e.getBody());
111     }
112
113     public <T, E extends CtExpression<?>> void visitCtArrayAccess(
114             CtArrayAccess<T, E> arrayAccess) {
115         scan(arrayAccess.getTarget());
116         write("[");
117         scan(arrayAccess.getIndexExpression());
118         write("]");
119     }
120
121     public <T> void visitCtArrayTypeReference(CtArrayTypeReference<T> reference) {
122         scan(reference.getComponentType());
123         write("[]");
124     }
125
126     public <T> void visitCtAssert(CtAssert<T> asserted) {
127         signature.append("assert ");
128         scan(asserted.getAssertExpression());
129         signature.append(":");
130         scan(asserted.getExpression());
131     }
132
133     public <T, A extends T> void visitCtAssignment(
134             CtAssignment<T, A> assignement) {
135         for (CtTypeReference ref : assignement.getTypeCasts()) {
136             write("(");
137             scan(ref);
138             write(")");
139         }
140         write("(");
141         scan(assignement.getAssigned());
142         write(" = ");
143         scan(assignement.getAssignment());
144         write(")");
145     }
146
147     public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
148         scan(operator.getLeftHandOperand());
149         write(operator.getKind().toString());
150         scan(operator.getRightHandOperand());
151     }
152
153     public <R> void visitCtBlock(CtBlock<R> block) {
154         signature.append("{\n");
155         for (CtStatement s : block.getStatements()) {
156             scan(s);
157             signature.append(";\n");
158         }
159         signature.append("}");
160     }
161
162     public void visitCtBreak(CtBreak breakStatement) {
163         write("break ");
164         if (breakStatement.getTargetLabel() != null)
165             write(breakStatement.getTargetLabel());
166     }
167
168     public <E> void visitCtCase(CtCase<E> caseStatement) {
169         write("case (");
170         scan(caseStatement.getCaseExpression());
171         write(")");
172     }
173
174     public void visitCtCatch(CtCatch catchBlock) {
175         write("catch (");
176         scan(catchBlock.getParameter().getType());
177         write(")");
178     }
179
180     public <T> void visitCtClass(CtClass<T> ctClass) {
181         write("class ").write(ctClass.getQualifiedName());
182     }
183
184     public <T> void visitCtConditional(CtConditional<T> conditional) {
185         scan(conditional.getCondition());
186         write("?");
187         scan(conditional.getThenExpression());
188         write(":");
189         scan(conditional.getElseExpression());
190     }
191
192     public <T> void visitCtConstructor(CtConstructor<T> c) {
193         write(c.getDeclaringType().getQualifiedName());
194         write("(");
195         for (CtParameter p : c.getParameters()) {
196             scan(p);
197             write(",");
198         }
199         write(")");
200     }
201
202     public void visitCtContinue(CtContinue continueStatement) {
203         signature.append("continue ");
204         scan(continueStatement.getLabelledStatement());
205     }
206
207     public void visitCtDo(CtDo doLoop) {
208         write("do ");
209         scan(doLoop.getBody());
210         write(" while (");
211         scan(doLoop.getLoopingExpression());
212         write(")");
213     }
214
215     public <T extends Enum JavaDoc> void visitCtEnum(CtEnum<T> ctEnum) {
216         write("enum ").write(ctEnum.getQualifiedName());
217     }
218
219     public <T> void visitCtExecutableReference(
220             CtExecutableReference<T> reference) {
221         write(reference.getType().getQualifiedName());
222         write(" ");
223         write(reference.getDeclaringType().getQualifiedName());
224         write(CtExecutable.EXECUTABLE_SEPARATOR);
225         write(reference.getSimpleName());
226         write("(");
227         for (CtTypeReference ref : reference.getParameterTypes()) {
228             scan(ref);
229             write(",");
230         }
231         if (!reference.getParameterTypes().isEmpty())
232             clearLast();
233         write(")");
234     }
235
236     public <T> void visitCtField(CtField<T> f) {
237         write(f.getSimpleName());
238     }
239
240     public <T> void visitCtFieldAccess(CtFieldAccess<T> fieldAccess) {
241         scan(fieldAccess.getTarget());
242     }
243
244     public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
245         write(reference.getType().getQualifiedName()).write(" ");
246         write(reference.getDeclaringType().getQualifiedName());
247         write(CtField.FIELD_SEPARATOR);
248         write(reference.getSimpleName());
249     }
250
251     public void visitCtFor(CtFor forLoop) {
252         write("for (");
253         for (CtStatement s : forLoop.getForInit()) {
254             scan(s);
255             write(",");
256         }
257         write(";");
258         scan(forLoop.getExpression());
259         write(";");
260         for (CtStatement s : forLoop.getForUpdate()) {
261             scan(s);
262             write(",");
263         }
264         write(")");
265         scan(forLoop.getBody());
266     }
267
268     public void visitCtForEach(CtForEach foreach) {
269         write("for (");
270         scan(foreach.getVariable());
271         write(":");
272         scan(foreach.getExpression());
273         write(")");
274         scan(foreach.getBody());
275     }
276
277     public void visitCtIf(CtIf ifElement) {
278         write("if (");
279         scan(ifElement.getCondition());
280         write(") then ");
281         scan(ifElement.getThenStatement());
282         write(" elseĀ ");
283         scan(ifElement.getElseStatement());
284     }
285
286     public <T> void visitCtInterface(CtInterface<T> intrface) {
287         write("interface ");
288         write(intrface.getQualifiedName());
289     }
290
291     public <T> void visitCtInvocation(CtInvocation<T> invocation) {
292         scan(invocation.getTarget());
293         write(".");
294         scan(invocation.getExecutable());
295         write("(");
296         for (CtExpression e : invocation.getArguments()) {
297             scan(e);
298             write(",");
299         }
300         write(")");
301     }
302
303     public <T> void visitCtLiteral(CtLiteral<T> literal) {
304         if (literal.getValue() != null)
305             write(literal.getValue().toString());
306     }
307
308     public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) {
309         write(localVariable.getSimpleName());
310     }
311
312     public <T> void visitCtLocalVariableReference(
313             CtLocalVariableReference<T> reference) {
314         scan(reference.getDeclaration());
315     }
316
317     public <T> void visitCtMethod(CtMethod<T> m) {
318         scan(m.getType());
319         write(" ");
320         write(m.getSimpleName());
321         write("(");
322         for (CtParameter p : m.getParameters()) {
323             scan(p);
324             write(",");
325         }
326         write(")");
327     }
328
329     public <T> void visitCtNewArray(CtNewArray<T> newArray) {
330         write("new ");
331         scan(newArray.getType());
332         for (CtExpression c : newArray.getDimensionExpressions()) {
333             write("[");
334             scan(c);
335             write("]");
336         }
337         write("{");
338         for (CtExpression e : newArray.getElements()) {
339             scan(e);
340             write(",");
341         }
342         write("}");
343     }
344
345     public <T> void visitCtNewClass(CtNewClass<T> newClass) {
346         write("new ");
347         scan(newClass.getExecutable());
348         scan(newClass.getAnonymousClass());
349     }
350
351     public <T, A extends T> void visitCtOperatorAssignement(
352             CtOperatorAssignment<T, A> assignment) {
353         scan(assignment.getAssigned());
354         write(assignment.getKind().toString());
355         scan(assignment.getAssignment());
356     }
357
358     public void visitCtPackage(CtPackage ctPackage) {
359         write(ctPackage.getQualifiedName());
360     }
361
362     public void visitCtPackageReference(CtPackageReference reference) {
363         write(reference.getSimpleName());
364     }
365
366     public <T> void visitCtParameter(CtParameter<T> parameter) {
367         scan(parameter.getType());
368     }
369
370     public <T> void visitCtParameterReference(CtParameterReference<T> reference) {
371         scan(reference.getType());
372     }
373
374     public <R> void visitCtReturn(CtReturn<R> returnStatement) {
375         write("return ");
376         scan(returnStatement.getReturnedExpression());
377     }
378
379     public <R> void visitCtStatementList(CtStatementList<R> statements) {
380         for (CtStatement s : statements.getStatements()) {
381             scan(s);
382             write(";\n");
383         }
384     }
385
386     public <E> void visitCtSwitch(CtSwitch<E> switchStatement) {
387         write("switch(");
388         scan(switchStatement.getSelector());
389         write(")");
390         for (CtCase c : switchStatement.getCases())
391             scan(c);
392     }
393
394     public void visitCtSynchronized(CtSynchronized synchro) {
395         write("synchronized (");
396         scan(synchro.getExpression());
397         write(") ");
398         scan(synchro.getBlock());
399     }
400
401     public void visitCtThrow(CtThrow throwStatement) {
402         write("throw ");
403         scan(throwStatement.getThrownExpression());
404     }
405
406     public void visitCtTry(CtTry tryBlock) {
407         write("try {\n");
408         scan(tryBlock.getBody());
409         for (CtCatch c : tryBlock.getCatchers()) {
410             scan(c);
411         }
412         scan(tryBlock.getFinalizer());
413     }
414
415     public void visitCtTypeParameter(CtTypeParameter typeParameter) {
416         write("<");
417         write(typeParameter.getName());
418         write(">");
419     }
420
421     public void visitCtTypeParameterReference(CtTypeParameterReference ref) {
422         write("<");
423         write(ref.getQualifiedName());
424         write(">");
425     }
426
427     public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
428         write(reference.getQualifiedName());
429     }
430
431     public <T> void visitCtUnaryOperator(CtUnaryOperator<T> operator) {
432         scan(operator.getOperand());
433         write(operator.getKind().toString());
434     }
435
436     public <T> void visitCtVariableAccess(CtVariableAccess<T> variableAccess) {
437         scan(variableAccess.getVariable());
438     }
439
440     public void visitCtWhile(CtWhile whileLoop) {
441         write("while (");
442         scan(whileLoop.getLoopingExpression());
443         write(")");
444         scan(whileLoop.getBody());
445     }
446
447 }
448
Popular Tags