KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > io > ByteCodeReader


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.io;
9
10 import org.gjt.jclasslib.bytecode.*;
11
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18     Converts code to a list of instructions as defined in the package
19     <tt>org.gjt.jclasslib.code</tt>.
20  
21     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
22     @version $Revision: 1.6 $ $Date: 2003/08/18 07:58:12 $
23 */

24 public class ByteCodeReader implements Opcodes {
25
26     private ByteCodeReader() {
27     }
28     
29     /**
30         Converts the code to a list of instructions.
31         @param code the code as an array of bytes from which to read the instructions
32         @return the <tt>java.util.List</tt> with the instructions
33         @throws IOException if an exception occurs with the code
34      */

35     public static List JavaDoc readByteCode(byte[] code) throws IOException JavaDoc {
36         return readByteCode(code, null);
37     }
38
39     /**
40         Converts the code to a list of instructions.
41         @param code the code as an array of bytes from which to read the instructions
42         @param prependInstructions an array of instructions that is prepended, may be <tt>null</tt>
43         @return the <tt>java.util.List</tt> with the instructions
44         @throws IOException if an exception occurs with the code
45      */

46     public static List JavaDoc readByteCode(byte[] code,
47                        AbstractInstruction[] prependInstructions)
48         throws IOException JavaDoc {
49
50         ByteCodeInputStream bcis = new ByteCodeInputStream(
51                                         new ByteArrayInputStream JavaDoc(code)
52                                     );
53         
54         ArrayList JavaDoc instructions = new ArrayList JavaDoc();
55         if (prependInstructions != null) {
56             for (int i = 0; i < prependInstructions.length; i++) {
57                 instructions.add(prependInstructions[i]);
58             }
59         }
60         
61         boolean wide = false;
62         AbstractInstruction currentInstruction;
63         while (bcis.getBytesRead() < code.length) {
64             currentInstruction = readNextInstruction(bcis, wide);
65             wide = (currentInstruction.getOpcode() == OPCODE_WIDE);
66             instructions.add(currentInstruction);
67         }
68         
69         return instructions;
70     }
71     
72     private static AbstractInstruction readNextInstruction(ByteCodeInputStream bcis, boolean wide)
73         throws IOException JavaDoc
74     {
75         AbstractInstruction instruction;
76
77         int opcode = bcis.readUnsignedByte();
78
79         switch (opcode) {
80             
81             case OPCODE_WIDE:
82             case OPCODE_NOP:
83             case OPCODE_ACONST_NULL:
84             case OPCODE_ICONST_M1:
85             case OPCODE_ICONST_0:
86             case OPCODE_ICONST_1:
87             case OPCODE_ICONST_2:
88             case OPCODE_ICONST_3:
89             case OPCODE_ICONST_4:
90             case OPCODE_ICONST_5:
91             case OPCODE_LCONST_0:
92             case OPCODE_LCONST_1:
93             case OPCODE_FCONST_0:
94             case OPCODE_FCONST_1:
95             case OPCODE_FCONST_2:
96             case OPCODE_DCONST_0:
97             case OPCODE_DCONST_1:
98             case OPCODE_ILOAD_0:
99             case OPCODE_ILOAD_1:
100             case OPCODE_ILOAD_2:
101             case OPCODE_ILOAD_3:
102             case OPCODE_LLOAD_0:
103             case OPCODE_LLOAD_1:
104             case OPCODE_LLOAD_2:
105             case OPCODE_LLOAD_3:
106             case OPCODE_FLOAD_0:
107             case OPCODE_FLOAD_1:
108             case OPCODE_FLOAD_2:
109             case OPCODE_FLOAD_3:
110             case OPCODE_DLOAD_0:
111             case OPCODE_DLOAD_1:
112             case OPCODE_DLOAD_2:
113             case OPCODE_DLOAD_3:
114             case OPCODE_ALOAD_0:
115             case OPCODE_ALOAD_1:
116             case OPCODE_ALOAD_2:
117             case OPCODE_ALOAD_3:
118             case OPCODE_IALOAD:
119             case OPCODE_LALOAD:
120             case OPCODE_FALOAD:
121             case OPCODE_DALOAD:
122             case OPCODE_AALOAD:
123             case OPCODE_BALOAD:
124             case OPCODE_CALOAD:
125             case OPCODE_SALOAD:
126             case OPCODE_ISTORE_0:
127             case OPCODE_ISTORE_1:
128             case OPCODE_ISTORE_2:
129             case OPCODE_ISTORE_3:
130             case OPCODE_LSTORE_0:
131             case OPCODE_LSTORE_1:
132             case OPCODE_LSTORE_2:
133             case OPCODE_LSTORE_3:
134             case OPCODE_FSTORE_0:
135             case OPCODE_FSTORE_1:
136             case OPCODE_FSTORE_2:
137             case OPCODE_FSTORE_3:
138             case OPCODE_DSTORE_0:
139             case OPCODE_DSTORE_1:
140             case OPCODE_DSTORE_2:
141             case OPCODE_DSTORE_3:
142             case OPCODE_ASTORE_0:
143             case OPCODE_ASTORE_1:
144             case OPCODE_ASTORE_2:
145             case OPCODE_ASTORE_3:
146             case OPCODE_IASTORE:
147             case OPCODE_LASTORE:
148             case OPCODE_FASTORE:
149             case OPCODE_DASTORE:
150             case OPCODE_AASTORE:
151             case OPCODE_BASTORE:
152             case OPCODE_CASTORE:
153             case OPCODE_SASTORE:
154             case OPCODE_POP:
155             case OPCODE_POP2:
156             case OPCODE_DUP:
157             case OPCODE_DUP_X1:
158             case OPCODE_DUP_X2:
159             case OPCODE_DUP2:
160             case OPCODE_DUP2_X1:
161             case OPCODE_DUP2_X2:
162             case OPCODE_SWAP:
163             case OPCODE_IADD:
164             case OPCODE_LADD:
165             case OPCODE_FADD:
166             case OPCODE_DADD:
167             case OPCODE_ISUB:
168             case OPCODE_LSUB:
169             case OPCODE_FSUB:
170             case OPCODE_DSUB:
171             case OPCODE_IMUL:
172             case OPCODE_LMUL:
173             case OPCODE_FMUL:
174             case OPCODE_DMUL:
175             case OPCODE_IDIV:
176             case OPCODE_LDIV:
177             case OPCODE_FDIV:
178             case OPCODE_DDIV:
179             case OPCODE_IREM:
180             case OPCODE_LREM:
181             case OPCODE_FREM:
182             case OPCODE_DREM:
183             case OPCODE_INEG:
184             case OPCODE_LNEG:
185             case OPCODE_FNEG:
186             case OPCODE_DNEG:
187             case OPCODE_ISHL:
188             case OPCODE_LSHL:
189             case OPCODE_ISHR:
190             case OPCODE_LSHR:
191             case OPCODE_IUSHR:
192             case OPCODE_LUSHR:
193             case OPCODE_IAND:
194             case OPCODE_LAND:
195             case OPCODE_IOR:
196             case OPCODE_LOR:
197             case OPCODE_IXOR:
198             case OPCODE_LXOR:
199             case OPCODE_I2L:
200             case OPCODE_I2F:
201             case OPCODE_I2D:
202             case OPCODE_L2I:
203             case OPCODE_L2F:
204             case OPCODE_L2D:
205             case OPCODE_F2I:
206             case OPCODE_F2L:
207             case OPCODE_F2D:
208             case OPCODE_D2I:
209             case OPCODE_D2L:
210             case OPCODE_D2F:
211             case OPCODE_I2B:
212             case OPCODE_I2C:
213             case OPCODE_I2S:
214             case OPCODE_LCMP:
215             case OPCODE_FCMPL:
216             case OPCODE_FCMPG:
217             case OPCODE_DCMPL:
218             case OPCODE_DCMPG:
219             case OPCODE_IRETURN:
220             case OPCODE_LRETURN:
221             case OPCODE_FRETURN:
222             case OPCODE_DRETURN:
223             case OPCODE_ARETURN:
224             case OPCODE_RETURN:
225             case OPCODE_XXXUNUSEDXXX:
226             case OPCODE_ARRAYLENGTH:
227             case OPCODE_ATHROW:
228             case OPCODE_MONITORENTER:
229             case OPCODE_MONITOREXIT:
230             case OPCODE_BREAKPOINT:
231             case OPCODE_IMPDEP1:
232             case OPCODE_IMPDEP2:
233                 
234                 instruction = new SimpleInstruction(opcode);
235                 break;
236
237             case OPCODE_BIPUSH:
238             case OPCODE_LDC:
239             case OPCODE_ILOAD: // subject to wide
240
case OPCODE_LLOAD: // subject to wide
241
case OPCODE_FLOAD: // subject to wide
242
case OPCODE_DLOAD: // subject to wide
243
case OPCODE_ALOAD: // subject to wide
244
case OPCODE_ISTORE: // subject to wide
245
case OPCODE_LSTORE: // subject to wide
246
case OPCODE_FSTORE: // subject to wide
247
case OPCODE_DSTORE: // subject to wide
248
case OPCODE_ASTORE: // subject to wide
249
case OPCODE_RET: // subject to wide
250
case OPCODE_NEWARRAY:
251
252                 instruction = new ImmediateByteInstruction(opcode, wide);
253                 break;
254
255             case OPCODE_LDC_W:
256             case OPCODE_LDC2_W:
257             case OPCODE_GETSTATIC:
258             case OPCODE_PUTSTATIC:
259             case OPCODE_GETFIELD:
260             case OPCODE_PUTFIELD:
261             case OPCODE_INVOKEVIRTUAL:
262             case OPCODE_INVOKESPECIAL:
263             case OPCODE_INVOKESTATIC:
264             case OPCODE_NEW:
265             case OPCODE_ANEWARRAY:
266             case OPCODE_CHECKCAST:
267             case OPCODE_INSTANCEOF:
268             case OPCODE_SIPUSH: // the only immediate short instruction that does
269
// not have an immediate constant pool reference
270

271                 instruction = new ImmediateShortInstruction(opcode);
272                 break;
273
274             case OPCODE_IFEQ:
275             case OPCODE_IFNE:
276             case OPCODE_IFLT:
277             case OPCODE_IFGE:
278             case OPCODE_IFGT:
279             case OPCODE_IFLE:
280             case OPCODE_IF_ICMPEQ:
281             case OPCODE_IF_ICMPNE:
282             case OPCODE_IF_ICMPLT:
283             case OPCODE_IF_ICMPGE:
284             case OPCODE_IF_ICMPGT:
285             case OPCODE_IF_ICMPLE:
286             case OPCODE_IF_ACMPEQ:
287             case OPCODE_IF_ACMPNE:
288             case OPCODE_GOTO:
289             case OPCODE_JSR:
290             case OPCODE_IFNULL:
291             case OPCODE_IFNONNULL:
292
293                 instruction = new BranchInstruction(opcode);
294                 break;
295
296             case OPCODE_GOTO_W:
297             case OPCODE_JSR_W:
298
299                 instruction = new ImmediateIntInstruction(opcode);
300                 break;
301                 
302             case OPCODE_IINC: // subject to wide
303

304                 instruction = new IncrementInstruction(opcode, wide);
305                 break;
306                 
307             case OPCODE_TABLESWITCH:
308
309                 instruction = new TableSwitchInstruction(opcode);
310                 break;
311                 
312             case OPCODE_LOOKUPSWITCH:
313
314                 instruction = new LookupSwitchInstruction(opcode);
315                 break;
316                 
317             case OPCODE_INVOKEINTERFACE:
318
319                 instruction = new InvokeInterfaceInstruction(opcode);
320                 break;
321                 
322             case OPCODE_MULTIANEWARRAY:
323             
324                 instruction = new MultianewarrayInstruction(opcode);
325                 break;
326                 
327             default:
328                 throw new IOException JavaDoc("invalid opcode 0x" + Integer.toHexString(opcode));
329         }
330         
331         instruction.read(bcis);
332         return instruction;
333     }
334     
335 }
336
Popular Tags