001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.drools.asm.util;
030:
031: import java.io.PrintWriter;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import org.drools.asm.Attribute;
036: import org.drools.asm.util.attrs.ASMStackMapAttribute;
037: import org.drools.asm.util.attrs.ASMStackMapTableAttribute;
038: import org.drools.asm.util.attrs.ASMifiable;
039:
040: /**
041: * An abstract visitor.
042: *
043: * @author Eric Bruneton
044: */
045: public abstract class AbstractVisitor {
046:
047: /**
048: * The names of the Java Virtual Machine opcodes.
049: */
050: public final static String[] OPCODES;
051: /**
052: * Types for <code>operand</code> parameter of the
053: * {@link org.drools.asm.MethodVisitor#visitIntInsn} method when
054: * <code>opcode</code> is <code>NEWARRAY</code>.
055: */
056: public final static String[] TYPES;
057:
058: static {
059: String s = "NOP,ACONST_NULL,ICONST_M1,ICONST_0,ICONST_1,ICONST_2,"
060: + "ICONST_3,ICONST_4,ICONST_5,LCONST_0,LCONST_1,FCONST_0,"
061: + "FCONST_1,FCONST_2,DCONST_0,DCONST_1,BIPUSH,SIPUSH,LDC,,,"
062: + "ILOAD,LLOAD,FLOAD,DLOAD,ALOAD,,,,,,,,,,,,,,,,,,,,,IALOAD,"
063: + "LALOAD,FALOAD,DALOAD,AALOAD,BALOAD,CALOAD,SALOAD,ISTORE,"
064: + "LSTORE,FSTORE,DSTORE,ASTORE,,,,,,,,,,,,,,,,,,,,,IASTORE,"
065: + "LASTORE,FASTORE,DASTORE,AASTORE,BASTORE,CASTORE,SASTORE,POP,"
066: + "POP2,DUP,DUP_X1,DUP_X2,DUP2,DUP2_X1,DUP2_X2,SWAP,IADD,LADD,"
067: + "FADD,DADD,ISUB,LSUB,FSUB,DSUB,IMUL,LMUL,FMUL,DMUL,IDIV,LDIV,"
068: + "FDIV,DDIV,IREM,LREM,FREM,DREM,INEG,LNEG,FNEG,DNEG,ISHL,LSHL,"
069: + "ISHR,LSHR,IUSHR,LUSHR,IAND,LAND,IOR,LOR,IXOR,LXOR,IINC,I2L,"
070: + "I2F,I2D,L2I,L2F,L2D,F2I,F2L,F2D,D2I,D2L,D2F,I2B,I2C,I2S,LCMP,"
071: + "FCMPL,FCMPG,DCMPL,DCMPG,IFEQ,IFNE,IFLT,IFGE,IFGT,IFLE,"
072: + "IF_ICMPEQ,IF_ICMPNE,IF_ICMPLT,IF_ICMPGE,IF_ICMPGT,IF_ICMPLE,"
073: + "IF_ACMPEQ,IF_ACMPNE,GOTO,JSR,RET,TABLESWITCH,LOOKUPSWITCH,"
074: + "IRETURN,LRETURN,FRETURN,DRETURN,ARETURN,RETURN,GETSTATIC,"
075: + "PUTSTATIC,GETFIELD,PUTFIELD,INVOKEVIRTUAL,INVOKESPECIAL,"
076: + "INVOKESTATIC,INVOKEINTERFACE,,NEW,NEWARRAY,ANEWARRAY,"
077: + "ARRAYLENGTH,ATHROW,CHECKCAST,INSTANCEOF,MONITORENTER,"
078: + "MONITOREXIT,,MULTIANEWARRAY,IFNULL,IFNONNULL,";
079: OPCODES = new String[200];
080: int i = 0;
081: int j = 0;
082: int l;
083: while ((l = s.indexOf(',', j)) > 0) {
084: AbstractVisitor.OPCODES[i++] = j + 1 == l ? null : s
085: .substring(j, l);
086: j = l + 1;
087: }
088:
089: s = "T_BOOLEAN,T_CHAR,T_FLOAT,T_DOUBLE,T_BYTE,T_SHORT,T_INT,T_LONG,";
090: TYPES = new String[12];
091: j = 0;
092: i = 4;
093: while ((l = s.indexOf(',', j)) > 0) {
094: AbstractVisitor.TYPES[i++] = s.substring(j, l);
095: j = l + 1;
096: }
097: }
098:
099: /**
100: * The text to be printed. Since the code of methods is not necessarily
101: * visited in sequential order, one method after the other, but can be
102: * interlaced (some instructions from method one, then some instructions
103: * from method two, then some instructions from method one again...), it is
104: * not possible to print the visited instructions directly to a sequential
105: * stream. A class is therefore printed in a two steps process: a string
106: * tree is constructed during the visit, and printed to a sequential stream
107: * at the end of the visit. This string tree is stored in this field, as a
108: * string list that can contain other string lists, which can themselves
109: * contain other string lists, and so on.
110: */
111: public final List text;
112:
113: /**
114: * A buffer that can be used to create strings.
115: */
116: protected final StringBuffer buf;
117:
118: /**
119: * Constructs a new {@link AbstractVisitor}.
120: */
121: protected AbstractVisitor() {
122: this .text = new ArrayList();
123: this .buf = new StringBuffer();
124: }
125:
126: /**
127: * Returns the text printed by this visitor.
128: *
129: * @return the text printed by this visitor.
130: */
131: public List getText() {
132: return this .text;
133: }
134:
135: /**
136: * Appends a quoted string to a given buffer.
137: *
138: * @param buf the buffer where the string must be added.
139: * @param s the string to be added.
140: */
141: public static void appendString(final StringBuffer buf,
142: final String s) {
143: buf.append("\"");
144: for (int i = 0; i < s.length(); ++i) {
145: final char c = s.charAt(i);
146: if (c == '\n') {
147: buf.append("\\n");
148: } else if (c == '\r') {
149: buf.append("\\r");
150: } else if (c == '\\') {
151: buf.append("\\\\");
152: } else if (c == '"') {
153: buf.append("\\\"");
154: } else if (c < 0x20 || c > 0x7f) {
155: buf.append("\\u");
156: if (c < 0x10) {
157: buf.append("000");
158: } else if (c < 0x100) {
159: buf.append("00");
160: } else if (c < 0x1000) {
161: buf.append("0");
162: }
163: buf.append(Integer.toString(c, 16));
164: } else {
165: buf.append(c);
166: }
167: }
168: buf.append("\"");
169: }
170:
171: /**
172: * Prints the given string tree.
173: *
174: * @param pw the writer to be used to print the tree.
175: * @param l a string tree, i.e., a string list that can contain other string
176: * lists, and so on recursively.
177: */
178: void printList(final PrintWriter pw, final List l) {
179: for (int i = 0; i < l.size(); ++i) {
180: final Object o = l.get(i);
181: if (o instanceof List) {
182: printList(pw, (List) o);
183: } else {
184: pw.print(o.toString());
185: }
186: }
187: }
188:
189: /**
190: * Returns the default {@link ASMifiable} prototypes.
191: *
192: * @return the default {@link ASMifiable} prototypes.
193: */
194: public static Attribute[] getDefaultAttributes() {
195: try {
196: return new Attribute[] { new ASMStackMapAttribute(),
197: new ASMStackMapTableAttribute() };
198: } catch (final Exception e) {
199: return new Attribute[0];
200: }
201: }
202: }
|