Source Code Cross Referenced for LocalVariablesSorter.java in  » Net » Terracotta » com » tc » asm » commons » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Terracotta » com.tc.asm.commons 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 com.tc.asm.commons;
030:
031:        import com.tc.asm.Label;
032:        import com.tc.asm.MethodAdapter;
033:        import com.tc.asm.MethodVisitor;
034:        import com.tc.asm.Opcodes;
035:        import com.tc.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 newIndex = remap(index, Type.getType(desc));
146:                if (newIndex == -1) {
147:                    throw new IllegalStateException("Unknown local variable "
148:                            + index + " : " + name + " " + desc);
149:                }
150:                mv.visitLocalVariable(name, desc, signature, start, end,
151:                        newIndex);
152:            }
153:
154:            public void visitFrame(final int type, final int nLocal,
155:                    final Object[] local, final int nStack, final Object[] stack) {
156:                if (type != Opcodes.F_NEW) { // uncompressed frame
157:                    throw new IllegalStateException(
158:                            "ClassReader.accept() should be called with EXPAND_FRAMES flag");
159:                }
160:
161:                if (!changed) { // optimization for the case where mapping = identity
162:                    mv.visitFrame(type, nLocal, local, nStack, stack);
163:                    return;
164:                }
165:
166:                // creates a copy of newLocals
167:                Object[] oldLocals = new Object[newLocals.length];
168:                System.arraycopy(newLocals, 0, oldLocals, 0, oldLocals.length);
169:
170:                // copies types from 'local' to 'newLocals'
171:                // 'newLocals' already contains the variables added with 'newLocal'
172:
173:                int index = 0; // old local variable index
174:                int number = 0; // old local variable number
175:                for (; number < nLocal; ++number) {
176:                    Object t = local[number];
177:                    int size = t == Opcodes.LONG || t == Opcodes.DOUBLE ? 2 : 1;
178:                    if (t != Opcodes.TOP) {
179:                        setFrameLocal(remap(index, size), t);
180:                    }
181:                    index += size;
182:                }
183:
184:                // removes TOP after long and double types as well as trailing TOPs
185:
186:                index = 0;
187:                number = 0;
188:                for (int i = 0; index < newLocals.length; ++i) {
189:                    Object t = newLocals[index++];
190:                    if (t != null && t != Opcodes.TOP) {
191:                        newLocals[i] = t;
192:                        number = i + 1;
193:                        if (t == Opcodes.LONG || t == Opcodes.DOUBLE) {
194:                            index += 1;
195:                        }
196:                    } else {
197:                        newLocals[i] = Opcodes.TOP;
198:                    }
199:                }
200:
201:                // visits remapped frame
202:                mv.visitFrame(type, number, newLocals, nStack, stack);
203:
204:                // restores original value of 'newLocals'
205:                newLocals = oldLocals;
206:            }
207:
208:            // -------------
209:
210:            /**
211:             * Creates a new local variable of the given type.
212:             *
213:             * @param type the type of the local variable to be created.
214:             * @return the identifier of the newly created local variable.
215:             */
216:            public int newLocal(final Type type) {
217:                Object t;
218:                switch (type.getSort()) {
219:                case Type.BOOLEAN:
220:                case Type.CHAR:
221:                case Type.BYTE:
222:                case Type.SHORT:
223:                case Type.INT:
224:                    t = Opcodes.INTEGER;
225:                    break;
226:                case Type.FLOAT:
227:                    t = Opcodes.FLOAT;
228:                    break;
229:                case Type.LONG:
230:                    t = Opcodes.LONG;
231:                    break;
232:                case Type.DOUBLE:
233:                    t = Opcodes.DOUBLE;
234:                    break;
235:                case Type.ARRAY:
236:                    t = type.getDescriptor();
237:                    break;
238:                // case Type.OBJECT:
239:                default:
240:                    t = type.getInternalName();
241:                    break;
242:                }
243:                int local = nextLocal;
244:                setLocalType(local, type);
245:                setFrameLocal(local, t);
246:                nextLocal += type.getSize();
247:                return local;
248:            }
249:
250:            /**
251:             * Sets the current type of the given local variable. The default
252:             * implementation of this method does nothing.
253:             *
254:             * @param local a local variable identifier, as returned by {@link #newLocal
255:             *        newLocal()}.
256:             * @param type the type of the value being stored in the local variable
257:             */
258:            protected void setLocalType(final int local, final Type type) {
259:            }
260:
261:            private void setFrameLocal(final int local, final Object type) {
262:                int l = newLocals.length;
263:                if (local >= l) {
264:                    Object[] a = new Object[Math.max(2 * l, local + 1)];
265:                    System.arraycopy(newLocals, 0, a, 0, l);
266:                    newLocals = a;
267:                }
268:                newLocals[local] = type;
269:            }
270:
271:            private int remap(final int var, final Type type) {
272:                if (var < firstLocal) {
273:                    return var;
274:                }
275:                int key = 2 * var + type.getSize() - 1;
276:                int size = mapping.length;
277:                if (key >= size) {
278:                    int[] newMapping = new int[Math.max(2 * size, key + 1)];
279:                    System.arraycopy(mapping, 0, newMapping, 0, size);
280:                    mapping = newMapping;
281:                }
282:                int value = mapping[key];
283:                if (value == 0) {
284:                    value = nextLocal + 1;
285:                    mapping[key] = value;
286:                    setLocalType(nextLocal, type);
287:                    nextLocal += type.getSize();
288:                }
289:                if (value - 1 != var) {
290:                    changed = true;
291:                }
292:                return value - 1;
293:            }
294:
295:            private int remap(final int var, final int size) {
296:                if (var < firstLocal || !changed) {
297:                    return var;
298:                }
299:                int key = 2 * var + size - 1;
300:                int value = key < mapping.length ? mapping[key] : 0;
301:                if (value == 0) {
302:                    throw new IllegalStateException("Unknown local variable "
303:                            + var);
304:                }
305:                return value - 1;
306:            }
307:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.