KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > bytecode > AbstractInstruction


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.bytecode;
9
10 import org.gjt.jclasslib.io.ByteCodeInput;
11 import org.gjt.jclasslib.io.ByteCodeOutput;
12
13 import java.io.IOException JavaDoc;
14
15 /**
16     Base class for all opcode instruction wrappers.
17  
18     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
19     @version $Revision: 1.5 $ $Date: 2003/08/18 07:58:35 $
20 */

21 public abstract class AbstractInstruction implements Opcodes {
22
23     private int offset;
24     private int opcode;
25
26     /**
27         Constructor.
28         @param opcode the opcode.
29      */

30     protected AbstractInstruction(int opcode) {
31         this.opcode = opcode;
32     }
33     
34     /**
35         Get the size in bytes of this instruction.
36         @return the size in bytes
37      */

38     public int getSize() {
39         return 1;
40     }
41
42     /**
43         Get the opcode of this instruction.
44         @return the opcode
45      */

46     public int getOpcode() {
47         return opcode;
48     }
49
50     /**
51         Set the opcode of this instruction.
52         @param opcode the opcode
53      */

54     public void setOpcode(int opcode) {
55         this.opcode = opcode;
56     }
57
58     /**
59         Get the verbose description of the opcode of this instruction.
60         @return the description
61      */

62     public String JavaDoc getOpcodeVerbose() {
63         String JavaDoc verbose = OpcodesUtil.getVerbose(opcode);
64         if (verbose == null) {
65             return "invalid opcode";
66         } else {
67             return verbose;
68         }
69     }
70     
71     /**
72         Get the offset of this instruction in its parent <tt>Code</tt> attribute.
73         @return the offset
74      */

75     public int getOffset() {
76         return offset;
77     }
78     
79     /**
80         Set the offset of this instruction in its parent <tt>Code</tt> attribute.
81         @param offset the offset
82      */

83     public void setOffset(int offset) {
84         this.offset = offset;
85     }
86     
87     /**
88         Read this instruction from the given <tt>ByteCodeInput</tt>. <p>
89      
90         Excpects <tt>ByteCodeInput</tt> to be in JVM class file format and just
91         before a instruction of this kind.
92         @param in the <tt>ByteCodeInput</tt> from which to read
93         @throws IOException if an exception occurs with the <tt>ByteCodeInput</tt>
94      */

95     public void read(ByteCodeInput in) throws IOException JavaDoc {
96         // The opcode has already been read
97
offset = in.getBytesRead() - 1;
98     }
99
100     /**
101         Write this instruction to the given <tt>ByteCodeOutput</tt>.
102         @param out the <tt>ByteCodeOutput</tt> to which to write
103         @throws IOException if an exception occurs with the <tt>ByteCodeOutput</tt>
104      */

105     public void write(ByteCodeOutput out) throws IOException JavaDoc {
106         out.writeByte(opcode);
107     }
108     
109 }
110
Popular Tags