KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > commons > LocalVariablesSorter


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.commons;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.objectweb.asm.Label;
36 import org.objectweb.asm.MethodAdapter;
37 import org.objectweb.asm.MethodVisitor;
38 import org.objectweb.asm.Opcodes;
39 import org.objectweb.asm.Type;
40
41 /**
42  * A {@link MethodAdapter} that renumbers local variables in their order of
43  * appearance. This adapter allows one to easily add new local variables to a
44  * method. <tt>computeMaxs</tt> <i>must be set to</i> <tt>true</tt> <i>in</i>
45  * {@link org.objectweb.asm.ClassWriter#ClassWriter(boolean) ClassWriter}
46  * <i>when this adapter is used</i>.
47  *
48  * @author Chris Nokleberg
49  */

50 public class LocalVariablesSorter extends MethodAdapter {
51
52     private Map JavaDoc locals = new HashMap JavaDoc();
53
54     protected final int firstLocal;
55
56     private int nextLocal;
57
58     public LocalVariablesSorter(
59         final int access,
60         final String JavaDoc desc,
61         final MethodVisitor mv)
62     {
63         super(mv);
64         Type[] args = Type.getArgumentTypes(desc);
65         nextLocal = ((Opcodes.ACC_STATIC & access) != 0) ? 0 : 1;
66         for (int i = 0; i < args.length; i++) {
67             nextLocal += args[i].getSize();
68         }
69         firstLocal = nextLocal;
70     }
71
72     public void visitVarInsn(final int opcode, final int var) {
73         int size;
74         switch (opcode) {
75             case Opcodes.LLOAD:
76             case Opcodes.LSTORE:
77             case Opcodes.DLOAD:
78             case Opcodes.DSTORE:
79                 size = 2;
80                 break;
81             default:
82                 size = 1;
83         }
84         mv.visitVarInsn(opcode, remap(var, size));
85     }
86
87     public void visitIincInsn(final int var, final int increment) {
88         mv.visitIincInsn(remap(var, 1), increment);
89     }
90
91     public void visitMaxs(final int maxStack, final int maxLocals) {
92         mv.visitMaxs(0, 0);
93     }
94
95     public void visitLocalVariable(
96         final String JavaDoc name,
97         final String JavaDoc desc,
98         final String JavaDoc signature,
99         final Label start,
100         final Label end,
101         final int index)
102     {
103         mv.visitLocalVariable(name, desc, signature, start, end, remap(index));
104     }
105
106     // -------------
107

108     protected int newLocal(final int size) {
109         int var = nextLocal;
110         nextLocal += size;
111         return var;
112     }
113
114     private int remap(final int var, final int size) {
115         if (var < firstLocal) {
116             return var;
117         }
118         Integer JavaDoc key = new Integer JavaDoc(size == 2 ? ~var : var);
119         Integer JavaDoc value = (Integer JavaDoc) locals.get(key);
120         if (value == null) {
121             value = new Integer JavaDoc(newLocal(size));
122             locals.put(key, value);
123         }
124         return value.intValue();
125     }
126
127     private int remap(final int var) {
128         if (var < firstLocal) {
129             return var;
130         }
131         Integer JavaDoc key = new Integer JavaDoc(var);
132         Integer JavaDoc value = (Integer JavaDoc) locals.get(key);
133         if (value == null) {
134             key = new Integer JavaDoc(~var);
135             value = (Integer JavaDoc) locals.get(key);
136             if (value == null) {
137                 throw new IllegalStateException JavaDoc("Unknown local variable " + var);
138             }
139         }
140         return value.intValue();
141     }
142 }
143
Popular Tags