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.ejb3unit.asm.commons;
030:
031: import org.ejb3unit.asm.Label;
032: import org.ejb3unit.asm.MethodAdapter;
033: import org.ejb3unit.asm.MethodVisitor;
034: import org.ejb3unit.asm.Opcodes;
035: import org.ejb3unit.asm.Type;
036:
037: /**
038: * A {@link MethodAdapter} that renumbers local variables in their order of
039: * appearance. This adapter allows one to easily add new local variables to a
040: * method. It may be used by inheriting from this class, but the preferred way
041: * of using it is via delegation: the next visitor in the chain can indeed add
042: * new locals when needed by calling {@link #newLocal} on this adapter (this
043: * requires a reference back to this {@link LocalVariablesSorter}).
044: *
045: * @author Chris Nokleberg
046: * @author Eugene Kuleshov
047: * @author Eric Bruneton
048: */
049: public class LocalVariablesSorter extends MethodAdapter {
050:
051: private final static Type OBJECT_TYPE = Type
052: .getObjectType("java/lang/Object");
053:
054: /**
055: * Mapping from old to new local variable indexes. A local variable at index
056: * i of size 1 is remapped to 'mapping[2*i]', while a local variable at
057: * index i of size 2 is remapped to 'mapping[2*i+1]'.
058: */
059: private int[] mapping = new int[40];
060:
061: /**
062: * Array used to store stack map local variable types after remapping.
063: */
064: private Object[] newLocals = new Object[20];
065:
066: /**
067: * Index of the first local variable, after formal parameters.
068: */
069: protected final int firstLocal;
070:
071: /**
072: * Index of the next local variable to be created by {@link #newLocal}.
073: */
074: protected int nextLocal;
075:
076: /**
077: * Indicates if at least one local variable has moved due to remapping.
078: */
079: private boolean changed;
080:
081: /**
082: * Creates a new {@link LocalVariablesSorter}.
083: *
084: * @param access access flags of the adapted method.
085: * @param desc the method's descriptor (see {@link Type Type}).
086: * @param mv the method visitor to which this adapter delegates calls.
087: */
088: public LocalVariablesSorter(final int access, final String desc,
089: final MethodVisitor mv) {
090: super (mv);
091: Type[] args = Type.getArgumentTypes(desc);
092: nextLocal = (Opcodes.ACC_STATIC & access) != 0 ? 0 : 1;
093: for (int i = 0; i < args.length; i++) {
094: nextLocal += args[i].getSize();
095: }
096: firstLocal = nextLocal;
097: }
098:
099: public void visitVarInsn(final int opcode, final int var) {
100: Type type;
101: switch (opcode) {
102: case Opcodes.LLOAD:
103: case Opcodes.LSTORE:
104: type = Type.LONG_TYPE;
105: break;
106:
107: case Opcodes.DLOAD:
108: case Opcodes.DSTORE:
109: type = Type.DOUBLE_TYPE;
110: break;
111:
112: case Opcodes.FLOAD:
113: case Opcodes.FSTORE:
114: type = Type.FLOAT_TYPE;
115: break;
116:
117: case Opcodes.ILOAD:
118: case Opcodes.ISTORE:
119: type = Type.INT_TYPE;
120: break;
121:
122: case Opcodes.ALOAD:
123: case Opcodes.ASTORE:
124: type = OBJECT_TYPE;
125: break;
126:
127: // case RET:
128: default:
129: type = Type.VOID_TYPE;
130: }
131: mv.visitVarInsn(opcode, remap(var, type));
132: }
133:
134: public void visitIincInsn(final int var, final int increment) {
135: mv.visitIincInsn(remap(var, Type.INT_TYPE), increment);
136: }
137:
138: public void visitMaxs(final int maxStack, final int maxLocals) {
139: mv.visitMaxs(maxStack, nextLocal);
140: }
141:
142: public void visitLocalVariable(final String name,
143: final String desc, final String signature,
144: final Label start, final Label end, final int index) {
145: int size = "J".equals(desc) || "D".equals(desc) ? 2 : 1;
146: int newIndex = remap(index, size);
147: mv.visitLocalVariable(name, desc, signature, start, end,
148: newIndex);
149: }
150:
151: public void visitFrame(final int type, final int nLocal,
152: final Object[] local, final int nStack, final Object[] stack) {
153: if (type != Opcodes.F_NEW) { // uncompressed frame
154: throw new IllegalStateException(
155: "ClassReader.accept() should be called with EXPAND_FRAMES flag");
156: }
157:
158: if (!changed) { // optimization for the case where mapping = identity
159: mv.visitFrame(type, nLocal, local, nStack, stack);
160: return;
161: }
162:
163: // creates a copy of newLocals
164: Object[] oldLocals = new Object[newLocals.length];
165: System.arraycopy(newLocals, 0, oldLocals, 0, oldLocals.length);
166:
167: // copies types from 'local' to 'newLocals'
168: // 'newLocals' already contains the variables added with 'newLocal'
169:
170: int index = 0; // old local variable index
171: int number = 0; // old local variable number
172: for (; number < nLocal; ++number) {
173: Object t = local[number];
174: int size = t == Opcodes.LONG || t == Opcodes.DOUBLE ? 2 : 1;
175: if (t != Opcodes.TOP) {
176: setFrameLocal(remap(index, size), t);
177: }
178: index += size;
179: }
180:
181: // removes TOP after long and double types as well as trailing TOPs
182:
183: index = 0;
184: number = 0;
185: for (int i = 0; index < newLocals.length; ++i) {
186: Object t = newLocals[index++];
187: if (t != null && t != Opcodes.TOP) {
188: newLocals[i] = t;
189: number = i + 1;
190: if (t == Opcodes.LONG || t == Opcodes.DOUBLE) {
191: index += 1;
192: }
193: } else {
194: newLocals[i] = Opcodes.TOP;
195: }
196: }
197:
198: // visits remapped frame
199: mv.visitFrame(type, number, newLocals, nStack, stack);
200:
201: // restores original value of 'newLocals'
202: newLocals = oldLocals;
203: }
204:
205: // -------------
206:
207: /**
208: * Creates a new local variable of the given type.
209: *
210: * @param type the type of the local variable to be created.
211: * @return the identifier of the newly created local variable.
212: */
213: public int newLocal(final Type type) {
214: Object t;
215: switch (type.getSort()) {
216: case Type.BOOLEAN:
217: case Type.CHAR:
218: case Type.BYTE:
219: case Type.SHORT:
220: case Type.INT:
221: t = Opcodes.INTEGER;
222: break;
223: case Type.FLOAT:
224: t = Opcodes.FLOAT;
225: break;
226: case Type.LONG:
227: t = Opcodes.LONG;
228: break;
229: case Type.DOUBLE:
230: t = Opcodes.DOUBLE;
231: break;
232: case Type.ARRAY:
233: t = type.getDescriptor();
234: break;
235: // case Type.OBJECT:
236: default:
237: t = type.getInternalName();
238: break;
239: }
240: int local = nextLocal;
241: setLocalType(local, type);
242: setFrameLocal(local, t);
243: nextLocal += type.getSize();
244: return local;
245: }
246:
247: /**
248: * Sets the current type of the given local variable. The default
249: * implementation of this method does nothing.
250: *
251: * @param local a local variable identifier, as returned by {@link #newLocal
252: * newLocal()}.
253: * @param type the type of the value being stored in the local variable
254: */
255: protected void setLocalType(final int local, final Type type) {
256: }
257:
258: private void setFrameLocal(final int local, final Object type) {
259: int l = newLocals.length;
260: if (local >= l) {
261: Object[] a = new Object[Math.max(2 * l, local + 1)];
262: System.arraycopy(newLocals, 0, a, 0, l);
263: newLocals = a;
264: }
265: newLocals[local] = type;
266: }
267:
268: private int remap(final int var, final Type type) {
269: if (var < firstLocal) {
270: return var;
271: }
272: int key = 2 * var + type.getSize() - 1;
273: int size = mapping.length;
274: if (key >= size) {
275: int[] newMapping = new int[Math.max(2 * size, key + 1)];
276: System.arraycopy(mapping, 0, newMapping, 0, size);
277: mapping = newMapping;
278: }
279: int value = mapping[key];
280: if (value == 0) {
281: value = nextLocal + 1;
282: mapping[key] = value;
283: setLocalType(nextLocal, type);
284: nextLocal += type.getSize();
285: }
286: if (value - 1 != var) {
287: changed = true;
288: }
289: return value - 1;
290: }
291:
292: private int remap(final int var, final int size) {
293: if (var < firstLocal || !changed) {
294: return var;
295: }
296: int key = 2 * var + size - 1;
297: int value = key < mapping.length ? mapping[key] : 0;
298: if (value == 0) {
299: throw new IllegalStateException("Unknown local variable "
300: + var);
301: }
302: return value - 1;
303: }
304: }
|