KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.PrintWriter JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.objectweb.asm.Attribute;
37 import org.objectweb.asm.util.attrs.ASMStackMapAttribute;
38 import org.objectweb.asm.util.attrs.ASMStackMapTableAttribute;
39 import org.objectweb.asm.util.attrs.ASMifiable;
40
41 /**
42  * An abstract visitor.
43  *
44  * @author Eric Bruneton
45  */

46 public abstract class AbstractVisitor {
47
48     /**
49      * The names of the Java Virtual Machine opcodes.
50      */

51     public final static String JavaDoc[] OPCODES;
52
53     static {
54         String JavaDoc s = "DNOPLACONST_NULLJICONST_M1IICONST_0IICONST_1IICONST_2II"
55                 + "CONST_3IICONST_4IICONST_5ILCONST_0ILCONST_1IFCONST_0IFCON"
56                 + "ST_1IFCONST_2IDCONST_0IDCONST_1GBIPUSHGSIPUSHDLDCAAFILOAD"
57                 + "FLLOADFFLOADFDLOADFALOADAAAAAAAAAAAAAAAAAAAAGIALOADGLALOA"
58                 + "DGFALOADGDALOADGAALOADGBALOADGCALOADGSALOADGISTOREGLSTORE"
59                 + "GFSTOREGDSTOREGASTOREAAAAAAAAAAAAAAAAAAAAHIASTOREHLASTORE"
60                 + "HFASTOREHDASTOREHAASTOREHBASTOREHCASTOREHSASTOREDPOPEPOP2"
61                 + "DDUPGDUP_X1GDUP_X2EDUP2HDUP2_X1HDUP2_X2ESWAPEIADDELADDEFA"
62                 + "DDEDADDEISUBELSUBEFSUBEDSUBEIMULELMULEFMULEDMULEIDIVELDIV"
63                 + "EFDIVEDDIVEIREMELREMEFREMEDREMEINEGELNEGEFNEGEDNEGEISHLEL"
64                 + "SHLEISHRELSHRFIUSHRFLUSHREIANDELANDDIORDLOREIXORELXOREIIN"
65                 + "CDI2LDI2FDI2DDL2IDL2FDL2DDF2IDF2LDF2DDD2IDD2LDD2FDI2BDI2C"
66                 + "DI2SELCMPFFCMPLFFCMPGFDCMPLFDCMPGEIFEQEIFNEEIFLTEIFGEEIFG"
67                 + "TEIFLEJIF_ICMPEQJIF_ICMPNEJIF_ICMPLTJIF_ICMPGEJIF_ICMPGTJ"
68                 + "IF_ICMPLEJIF_ACMPEQJIF_ACMPNEEGOTODJSRDRETLTABLESWITCHMLO"
69                 + "OKUPSWITCHHIRETURNHLRETURNHFRETURNHDRETURNHARETURNGRETURN"
70                 + "JGETSTATICJPUTSTATICIGETFIELDIPUTFIELDNINVOKEVIRTUALNINVO"
71                 + "KESPECIALMINVOKESTATICPINVOKEINTERFACEADNEWINEWARRAYJANEW"
72                 + "ARRAYLARRAYLENGTHGATHROWJCHECKCASTKINSTANCEOFMMONITORENTE"
73                 + "RLMONITOREXITAOMULTIANEWARRAYGIFNULLJIFNONNULL";
74         OPCODES = new String JavaDoc[200];
75         int i = 0;
76         int len = 0;
77         for (int j = 0; j < s.length(); j += len) {
78             len = s.charAt(j++) - 'A';
79             OPCODES[i++] = len == 0 ? null : s.substring(j, j + len);
80         }
81     }
82
83     // code to generate the above string
84
// public static void main (String[] args) {
85
// String[] OPCODES = {
86
// "NOP",
87
// "ACONST_NULL",
88
// "ICONST_M1",
89
// "ICONST_0",
90
// "ICONST_1",
91
// "ICONST_2",
92
// "ICONST_3",
93
// "ICONST_4",
94
// "ICONST_5",
95
// "LCONST_0",
96
// "LCONST_1",
97
// "FCONST_0",
98
// "FCONST_1",
99
// "FCONST_2",
100
// "DCONST_0",
101
// "DCONST_1",
102
// "BIPUSH",
103
// "SIPUSH",
104
// "LDC",
105
// null,
106
// null,
107
// "ILOAD",
108
// "LLOAD",
109
// "FLOAD",
110
// "DLOAD",
111
// "ALOAD",
112
// null,
113
// null,
114
// null,
115
// null,
116
// null,
117
// null,
118
// null,
119
// null,
120
// null,
121
// null,
122
// null,
123
// null,
124
// null,
125
// null,
126
// null,
127
// null,
128
// null,
129
// null,
130
// null,
131
// null,
132
// "IALOAD",
133
// "LALOAD",
134
// "FALOAD",
135
// "DALOAD",
136
// "AALOAD",
137
// "BALOAD",
138
// "CALOAD",
139
// "SALOAD",
140
// "ISTORE",
141
// "LSTORE",
142
// "FSTORE",
143
// "DSTORE",
144
// "ASTORE",
145
// null,
146
// null,
147
// null,
148
// null,
149
// null,
150
// null,
151
// null,
152
// null,
153
// null,
154
// null,
155
// null,
156
// null,
157
// null,
158
// null,
159
// null,
160
// null,
161
// null,
162
// null,
163
// null,
164
// null,
165
// "IASTORE",
166
// "LASTORE",
167
// "FASTORE",
168
// "DASTORE",
169
// "AASTORE",
170
// "BASTORE",
171
// "CASTORE",
172
// "SASTORE",
173
// "POP",
174
// "POP2",
175
// "DUP",
176
// "DUP_X1",
177
// "DUP_X2",
178
// "DUP2",
179
// "DUP2_X1",
180
// "DUP2_X2",
181
// "SWAP",
182
// "IADD",
183
// "LADD",
184
// "FADD",
185
// "DADD",
186
// "ISUB",
187
// "LSUB",
188
// "FSUB",
189
// "DSUB",
190
// "IMUL",
191
// "LMUL",
192
// "FMUL",
193
// "DMUL",
194
// "IDIV",
195
// "LDIV",
196
// "FDIV",
197
// "DDIV",
198
// "IREM",
199
// "LREM",
200
// "FREM",
201
// "DREM",
202
// "INEG",
203
// "LNEG",
204
// "FNEG",
205
// "DNEG",
206
// "ISHL",
207
// "LSHL",
208
// "ISHR",
209
// "LSHR",
210
// "IUSHR",
211
// "LUSHR",
212
// "IAND",
213
// "LAND",
214
// "IOR",
215
// "LOR",
216
// "IXOR",
217
// "LXOR",
218
// "IINC",
219
// "I2L",
220
// "I2F",
221
// "I2D",
222
// "L2I",
223
// "L2F",
224
// "L2D",
225
// "F2I",
226
// "F2L",
227
// "F2D",
228
// "D2I",
229
// "D2L",
230
// "D2F",
231
// "I2B",
232
// "I2C",
233
// "I2S",
234
// "LCMP",
235
// "FCMPL",
236
// "FCMPG",
237
// "DCMPL",
238
// "DCMPG",
239
// "IFEQ",
240
// "IFNE",
241
// "IFLT",
242
// "IFGE",
243
// "IFGT",
244
// "IFLE",
245
// "IF_ICMPEQ",
246
// "IF_ICMPNE",
247
// "IF_ICMPLT",
248
// "IF_ICMPGE",
249
// "IF_ICMPGT",
250
// "IF_ICMPLE",
251
// "IF_ACMPEQ",
252
// "IF_ACMPNE",
253
// "GOTO",
254
// "JSR",
255
// "RET",
256
// "TABLESWITCH",
257
// "LOOKUPSWITCH",
258
// "IRETURN",
259
// "LRETURN",
260
// "FRETURN",
261
// "DRETURN",
262
// "ARETURN",
263
// "RETURN",
264
// "GETSTATIC",
265
// "PUTSTATIC",
266
// "GETFIELD",
267
// "PUTFIELD",
268
// "INVOKEVIRTUAL",
269
// "INVOKESPECIAL",
270
// "INVOKESTATIC",
271
// "INVOKEINTERFACE",
272
// null,
273
// "NEW",
274
// "NEWARRAY",
275
// "ANEWARRAY",
276
// "ARRAYLENGTH",
277
// "ATHROW",
278
// "CHECKCAST",
279
// "INSTANCEOF",
280
// "MONITORENTER",
281
// "MONITOREXIT",
282
// null,
283
// "MULTIANEWARRAY",
284
// "IFNULL",
285
// "IFNONNULL"
286
// };
287
// for (int i = 0; i < OPCODES.length; ++i) {
288
// if (OPCODES[i] == null) {
289
// System.out.print('A');
290
// } else {
291
// System.out.print((char)(OPCODES[i].length() + 'A'));
292
// System.out.print(OPCODES[i]);
293
// }
294
// }
295
// System.out.println();
296
// }
297

298     /**
299      * The text to be printed. Since the code of methods is not necessarily
300      * visited in sequential order, one method after the other, but can be
301      * interlaced (some instructions from method one, then some instructions
302      * from method two, then some instructions from method one again...), it is
303      * not possible to print the visited instructions directly to a sequential
304      * stream. A class is therefore printed in a two steps process: a string
305      * tree is constructed during the visit, and printed to a sequential stream
306      * at the end of the visit. This string tree is stored in this field, as a
307      * string list that can contain other string lists, which can themselves
308      * contain other string lists, and so on.
309      */

310     public final List JavaDoc text;
311
312     /**
313      * A buffer that can be used to create strings.
314      */

315     protected final StringBuffer JavaDoc buf;
316
317     /**
318      * Constructs a new {@link AbstractVisitor}.
319      */

320     protected AbstractVisitor() {
321         this.text = new ArrayList JavaDoc();
322         this.buf = new StringBuffer JavaDoc();
323     }
324
325     /**
326      * Returns the text printed by this visitor.
327      *
328      * @return the text printed by this visitor.
329      */

330     public List JavaDoc getText() {
331         return text;
332     }
333
334     /**
335      * Appends a quoted string to a given buffer.
336      *
337      * @param buf the buffer where the string must be added.
338      * @param s the string to be added.
339      */

340     public static void appendString(final StringBuffer JavaDoc buf, final String JavaDoc s) {
341         buf.append("\"");
342         for (int i = 0; i < s.length(); ++i) {
343             char c = s.charAt(i);
344             if (c == '\n') {
345                 buf.append("\\n");
346             } else if (c == '\r') {
347                 buf.append("\\r");
348             } else if (c == '\\') {
349                 buf.append("\\\\");
350             } else if (c == '"') {
351                 buf.append("\\\"");
352             } else if (c < 0x20 || c > 0x7f) {
353                 buf.append("\\u");
354                 if (c < 0x10) {
355                     buf.append("000");
356                 } else if (c < 0x100) {
357                     buf.append("00");
358                 } else if (c < 0x1000) {
359                     buf.append("0");
360                 }
361                 buf.append(Integer.toString(c, 16));
362             } else {
363                 buf.append(c);
364             }
365         }
366         buf.append("\"");
367     }
368
369     /**
370      * Prints the given string tree.
371      *
372      * @param pw the writer to be used to print the tree.
373      * @param l a string tree, i.e., a string list that can contain other string
374      * lists, and so on recursively.
375      */

376     void printList(final PrintWriter JavaDoc pw, final List JavaDoc l) {
377         for (int i = 0; i < l.size(); ++i) {
378             Object JavaDoc o = l.get(i);
379             if (o instanceof List JavaDoc) {
380                 printList(pw, (List JavaDoc) o);
381             } else {
382                 pw.print(o.toString());
383             }
384         }
385     }
386
387     /**
388      * Returns the default {@link ASMifiable} prototypes.
389      *
390      * @return the default {@link ASMifiable} prototypes.
391      */

392     public static Attribute[] getDefaultAttributes() {
393         try {
394             return new Attribute[] {
395                 new ASMStackMapAttribute(),
396                 new ASMStackMapTableAttribute() };
397         } catch (Exception JavaDoc e) {
398             return new Attribute[0];
399         }
400     }
401 }
402
Popular Tags