KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > util > TraceSignatureVisitor


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.util;
31
32 import org.objectweb.asm.Opcodes;
33 import org.objectweb.asm.signature.SignatureVisitor;
34
35 public class TraceSignatureVisitor implements SignatureVisitor {
36
37     private StringBuffer JavaDoc declaration;
38
39     private boolean isInterface;
40
41     private boolean seenFormalParameter;
42
43     private boolean seenInterfaceBound;
44
45     private boolean seenParameter;
46
47     private boolean seenInterface;
48
49     private StringBuffer JavaDoc returnType;
50
51     private StringBuffer JavaDoc exceptions;
52
53     /**
54      * Stack used to keep track of class types that have arguments. Each element
55      * of this stack is a boolean encoded in one bit. The top of the stack is
56      * the lowest order bit. Pushing false = *2, pushing true = *2+1, popping =
57      * /2.
58      */

59     private int argumentStack;
60
61     /**
62      * Stack used to keep track of array class types. Each element of this stack
63      * is a boolean encoded in one bit. The top of the stack is the lowest order
64      * bit. Pushing false = *2, pushing true = *2+1, popping = /2.
65      */

66     private int arrayStack;
67
68     private String JavaDoc separator = "";
69
70     public TraceSignatureVisitor(int access) {
71         isInterface = (access & Opcodes.ACC_INTERFACE) != 0;
72         this.declaration = new StringBuffer JavaDoc();
73     }
74
75     private TraceSignatureVisitor(StringBuffer JavaDoc buf) {
76         this.declaration = buf;
77     }
78
79     public void visitFormalTypeParameter(String JavaDoc name) {
80         declaration.append(seenFormalParameter ? ", " : "<").append(name);
81         seenFormalParameter = true;
82         seenInterfaceBound = false;
83     }
84
85     public SignatureVisitor visitClassBound() {
86         separator = " extends ";
87         startType();
88         return this;
89     }
90
91     public SignatureVisitor visitInterfaceBound() {
92         separator = seenInterfaceBound ? ", " : (isInterface
93                 ? " extends "
94                 : " implements ");
95         seenInterfaceBound = true;
96         startType();
97         return this;
98     }
99
100     public SignatureVisitor visitSuperclass() {
101         endFormals();
102         separator = " extends ";
103         startType();
104         return this;
105     }
106
107     public SignatureVisitor visitInterface() {
108         separator = seenInterface ? ", " : (isInterface
109                 ? " extends "
110                 : " implements ");
111         seenInterface = true;
112         startType();
113         return this;
114     }
115
116     public SignatureVisitor visitParameterType() {
117         endFormals();
118         if (!seenParameter) {
119             seenParameter = true;
120             declaration.append('(');
121         } else {
122             declaration.append(", ");
123         }
124         startType();
125         return this;
126     }
127
128     public SignatureVisitor visitReturnType() {
129         endFormals();
130         if (!seenParameter) {
131             declaration.append('(');
132         }
133         declaration.append(')');
134         returnType = new StringBuffer JavaDoc();
135         return new TraceSignatureVisitor(returnType);
136     }
137
138     public SignatureVisitor visitExceptionType() {
139         if (exceptions == null) {
140             exceptions = new StringBuffer JavaDoc();
141         } else {
142             exceptions.append(", ");
143         }
144         // startType();
145
return new TraceSignatureVisitor(exceptions);
146     }
147
148     public void visitBaseType(char descriptor) {
149         switch (descriptor) {
150             case 'V':
151                 declaration.append("void");
152                 break;
153             case 'B':
154                 declaration.append("byte");
155                 break;
156             case 'J':
157                 declaration.append("long");
158                 break;
159             case 'Z':
160                 declaration.append("boolean");
161                 break;
162             case 'I':
163                 declaration.append("int");
164                 break;
165             case 'S':
166                 declaration.append("short");
167                 break;
168             case 'C':
169                 declaration.append("char");
170                 break;
171             case 'F':
172                 declaration.append("float");
173                 break;
174             case 'D':
175                 declaration.append("double");
176                 break;
177             default:
178                 throw new IllegalArgumentException JavaDoc("Invalid descriptor "
179                         + descriptor);
180         }
181         endType();
182     }
183
184     public void visitTypeVariable(String JavaDoc name) {
185         declaration.append(name);
186         endType();
187     }
188
189     public SignatureVisitor visitArrayType() {
190         startType();
191         arrayStack |= 1;
192         return this;
193     }
194
195     public void visitClassType(String JavaDoc name) {
196         if (!"java/lang/Object".equals(name)) {
197             declaration.append(separator).append(name.replace('/', '.'));
198         }
199         separator = "";
200         argumentStack *= 2;
201     }
202
203     public void visitInnerClassType(String JavaDoc name) {
204         // TODO
205
}
206
207     public void visitTypeArgument() {
208         if (argumentStack % 2 == 0) {
209             ++argumentStack;
210             declaration.append("<");
211         } else {
212             declaration.append(", ");
213         }
214         declaration.append("?");
215     }
216
217     public SignatureVisitor visitTypeArgument(char tag) {
218         if (argumentStack % 2 == 0) {
219             ++argumentStack;
220             declaration.append("<");
221         } else {
222             declaration.append(", ");
223         }
224
225         if (tag == SignatureVisitor.EXTENDS) {
226             declaration.append("? extends ");
227         } else if (tag == SignatureVisitor.SUPER) {
228             declaration.append("? super ");
229         }
230
231         startType();
232         return this;
233     }
234
235     public void visitEnd() {
236         if (argumentStack % 2 == 1) {
237             declaration.append(">");
238         }
239         argumentStack /= 2;
240         endType();
241     }
242
243     public String JavaDoc getDeclaration() {
244         return declaration.toString();
245     }
246
247     public String JavaDoc getReturnType() {
248         return returnType == null ? null : returnType.toString();
249     }
250
251     public String JavaDoc getExceptions() {
252         return exceptions == null ? null : exceptions.toString();
253     }
254
255     // -----------------------------------------------
256

257     private void endFormals() {
258         if (seenFormalParameter) {
259             declaration.append(">");
260             seenFormalParameter = false;
261         }
262     }
263
264     private void startType() {
265         arrayStack *= 2;
266     }
267
268     private void endType() {
269         if (arrayStack % 2 == 1) {
270             while (arrayStack % 2 == 1) {
271                 arrayStack /= 2;
272                 declaration.append("[]");
273             }
274         } else {
275             arrayStack /= 2;
276         }
277     }
278 }
279
Popular Tags