1 |
779 |
jeremybenn |
/***
|
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;
|
33 |
|
|
import java.util.ArrayList;
|
34 |
|
|
import java.util.List;
|
35 |
|
|
|
36 |
|
|
import org.objectweb.asm.Attribute;
|
37 |
|
|
import org.objectweb.asm.util.attrs.ASMStackMapAttribute;
|
38 |
|
|
import org.objectweb.asm.util.attrs.ASMStackMapTableAttribute;
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* An abstract visitor.
|
42 |
|
|
*
|
43 |
|
|
* @author Eric Bruneton
|
44 |
|
|
*/
|
45 |
|
|
public abstract class AbstractVisitor {
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* The names of the Java Virtual Machine opcodes.
|
49 |
|
|
*/
|
50 |
|
|
public final static String[] OPCODES;
|
51 |
|
|
/**
|
52 |
|
|
* Types for <code>operand</code> parameter of the
|
53 |
|
|
* {@link org.objectweb.asm.MethodVisitor#visitIntInsn} method when
|
54 |
|
|
* <code>opcode</code> is <code>NEWARRAY</code>.
|
55 |
|
|
*/
|
56 |
|
|
public final static String[] TYPES;
|
57 |
|
|
|
58 |
|
|
static {
|
59 |
|
|
String s = "NOP,ACONST_NULL,ICONST_M1,ICONST_0,ICONST_1,ICONST_2,"
|
60 |
|
|
+ "ICONST_3,ICONST_4,ICONST_5,LCONST_0,LCONST_1,FCONST_0,"
|
61 |
|
|
+ "FCONST_1,FCONST_2,DCONST_0,DCONST_1,BIPUSH,SIPUSH,LDC,,,"
|
62 |
|
|
+ "ILOAD,LLOAD,FLOAD,DLOAD,ALOAD,,,,,,,,,,,,,,,,,,,,,IALOAD,"
|
63 |
|
|
+ "LALOAD,FALOAD,DALOAD,AALOAD,BALOAD,CALOAD,SALOAD,ISTORE,"
|
64 |
|
|
+ "LSTORE,FSTORE,DSTORE,ASTORE,,,,,,,,,,,,,,,,,,,,,IASTORE,"
|
65 |
|
|
+ "LASTORE,FASTORE,DASTORE,AASTORE,BASTORE,CASTORE,SASTORE,POP,"
|
66 |
|
|
+ "POP2,DUP,DUP_X1,DUP_X2,DUP2,DUP2_X1,DUP2_X2,SWAP,IADD,LADD,"
|
67 |
|
|
+ "FADD,DADD,ISUB,LSUB,FSUB,DSUB,IMUL,LMUL,FMUL,DMUL,IDIV,LDIV,"
|
68 |
|
|
+ "FDIV,DDIV,IREM,LREM,FREM,DREM,INEG,LNEG,FNEG,DNEG,ISHL,LSHL,"
|
69 |
|
|
+ "ISHR,LSHR,IUSHR,LUSHR,IAND,LAND,IOR,LOR,IXOR,LXOR,IINC,I2L,"
|
70 |
|
|
+ "I2F,I2D,L2I,L2F,L2D,F2I,F2L,F2D,D2I,D2L,D2F,I2B,I2C,I2S,LCMP,"
|
71 |
|
|
+ "FCMPL,FCMPG,DCMPL,DCMPG,IFEQ,IFNE,IFLT,IFGE,IFGT,IFLE,"
|
72 |
|
|
+ "IF_ICMPEQ,IF_ICMPNE,IF_ICMPLT,IF_ICMPGE,IF_ICMPGT,IF_ICMPLE,"
|
73 |
|
|
+ "IF_ACMPEQ,IF_ACMPNE,GOTO,JSR,RET,TABLESWITCH,LOOKUPSWITCH,"
|
74 |
|
|
+ "IRETURN,LRETURN,FRETURN,DRETURN,ARETURN,RETURN,GETSTATIC,"
|
75 |
|
|
+ "PUTSTATIC,GETFIELD,PUTFIELD,INVOKEVIRTUAL,INVOKESPECIAL,"
|
76 |
|
|
+ "INVOKESTATIC,INVOKEINTERFACE,,NEW,NEWARRAY,ANEWARRAY,"
|
77 |
|
|
+ "ARRAYLENGTH,ATHROW,CHECKCAST,INSTANCEOF,MONITORENTER,"
|
78 |
|
|
+ "MONITOREXIT,,MULTIANEWARRAY,IFNULL,IFNONNULL,";
|
79 |
|
|
OPCODES = new String[200];
|
80 |
|
|
int i = 0;
|
81 |
|
|
int j = 0;
|
82 |
|
|
int l;
|
83 |
|
|
while ((l = s.indexOf(',', j)) > 0) {
|
84 |
|
|
OPCODES[i++] = j + 1 == l ? null : s.substring(j, l);
|
85 |
|
|
j = l + 1;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
s = "T_BOOLEAN,T_CHAR,T_FLOAT,T_DOUBLE,T_BYTE,T_SHORT,T_INT,T_LONG,";
|
89 |
|
|
TYPES = new String[12];
|
90 |
|
|
j = 0;
|
91 |
|
|
i = 4;
|
92 |
|
|
while ((l = s.indexOf(',', j)) > 0) {
|
93 |
|
|
TYPES[i++] = s.substring(j, l);
|
94 |
|
|
j = l + 1;
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
/**
|
99 |
|
|
* The text to be printed. Since the code of methods is not necessarily
|
100 |
|
|
* visited in sequential order, one method after the other, but can be
|
101 |
|
|
* interlaced (some instructions from method one, then some instructions
|
102 |
|
|
* from method two, then some instructions from method one again...), it is
|
103 |
|
|
* not possible to print the visited instructions directly to a sequential
|
104 |
|
|
* stream. A class is therefore printed in a two steps process: a string
|
105 |
|
|
* tree is constructed during the visit, and printed to a sequential stream
|
106 |
|
|
* at the end of the visit. This string tree is stored in this field, as a
|
107 |
|
|
* string list that can contain other string lists, which can themselves
|
108 |
|
|
* contain other string lists, and so on.
|
109 |
|
|
*/
|
110 |
|
|
public final List text;
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* A buffer that can be used to create strings.
|
114 |
|
|
*/
|
115 |
|
|
protected final StringBuffer buf;
|
116 |
|
|
|
117 |
|
|
/**
|
118 |
|
|
* Constructs a new {@link AbstractVisitor}.
|
119 |
|
|
*/
|
120 |
|
|
protected AbstractVisitor() {
|
121 |
|
|
this.text = new ArrayList();
|
122 |
|
|
this.buf = new StringBuffer();
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* Returns the text printed by this visitor.
|
127 |
|
|
*
|
128 |
|
|
* @return the text printed by this visitor.
|
129 |
|
|
*/
|
130 |
|
|
public List getText() {
|
131 |
|
|
return text;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* Appends a quoted string to a given buffer.
|
136 |
|
|
*
|
137 |
|
|
* @param buf the buffer where the string must be added.
|
138 |
|
|
* @param s the string to be added.
|
139 |
|
|
*/
|
140 |
|
|
public static void appendString(final StringBuffer buf, final String s) {
|
141 |
|
|
buf.append("\"");
|
142 |
|
|
for (int i = 0; i < s.length(); ++i) {
|
143 |
|
|
char c = s.charAt(i);
|
144 |
|
|
if (c == '\n') {
|
145 |
|
|
buf.append("\\n");
|
146 |
|
|
} else if (c == '\r') {
|
147 |
|
|
buf.append("\\r");
|
148 |
|
|
} else if (c == '\\') {
|
149 |
|
|
buf.append("\\\\");
|
150 |
|
|
} else if (c == '"') {
|
151 |
|
|
buf.append("\\\"");
|
152 |
|
|
} else if (c < 0x20 || c > 0x7f) {
|
153 |
|
|
buf.append("\\u");
|
154 |
|
|
if (c < 0x10) {
|
155 |
|
|
buf.append("000");
|
156 |
|
|
} else if (c < 0x100) {
|
157 |
|
|
buf.append("00");
|
158 |
|
|
} else if (c < 0x1000) {
|
159 |
|
|
buf.append("0");
|
160 |
|
|
}
|
161 |
|
|
buf.append(Integer.toString(c, 16));
|
162 |
|
|
} else {
|
163 |
|
|
buf.append(c);
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
buf.append("\"");
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/**
|
170 |
|
|
* Prints the given string tree.
|
171 |
|
|
*
|
172 |
|
|
* @param pw the writer to be used to print the tree.
|
173 |
|
|
* @param l a string tree, i.e., a string list that can contain other string
|
174 |
|
|
* lists, and so on recursively.
|
175 |
|
|
*/
|
176 |
|
|
void printList(final PrintWriter pw, final List l) {
|
177 |
|
|
for (int i = 0; i < l.size(); ++i) {
|
178 |
|
|
Object o = l.get(i);
|
179 |
|
|
if (o instanceof List) {
|
180 |
|
|
printList(pw, (List) o);
|
181 |
|
|
} else {
|
182 |
|
|
pw.print(o.toString());
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
/**
|
188 |
|
|
* Returns the default {@link ASMifiable} prototypes.
|
189 |
|
|
*
|
190 |
|
|
* @return the default {@link ASMifiable} prototypes.
|
191 |
|
|
*/
|
192 |
|
|
public static Attribute[] getDefaultAttributes() {
|
193 |
|
|
try {
|
194 |
|
|
return new Attribute[] {
|
195 |
|
|
new ASMStackMapAttribute(),
|
196 |
|
|
new ASMStackMapTableAttribute() };
|
197 |
|
|
} catch (Exception e) {
|
198 |
|
|
return new Attribute[0];
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
}
|