Source code: com/chaoswg/xtc4y/classdesc/code/instructions/WideBinaryInstruction.java
1 //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/code/instructions/WideBinaryInstruction.java,v 1.2 2003/08/26 12:35:25 toggm Exp $
2 /******************************************************************************
3 * XTC4y - eXtreme Testing Collection 4 you *
4 * -------------------------------------------------------------------------- *
5 * URL: http://www.chaoswg.com/xtc4y *
6 * Author: Mike Toggweiler (2.dog@gmx.ch) *
7 * *
8 * Last Updated: $Date: 2003/08/26 12:35:25 $, by $Author: toggm $ *
9 * Version: $Revision: 1.2 $ *
10 * -------------------------------------------------------------------------- *
11 * COPYRIGHT: (c) 2003 by Mike Toggweiler *
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 *****************************************************************************/
18 package com.chaoswg.xtc4y.classdesc.code.instructions;
19
20 import java.io.DataInputStream;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import com.chaoswg.xtc4y.classdesc.ConstantPool;
25
26 /**
27 * A binary instruction consists of a command and one two bytes operand,
28 * therefore its size is 3 bytes
29 * @author Mike Toggweiler
30 **/
31 public class WideBinaryInstruction extends Instruction {
32 private short operand;
33
34 /**
35 * Opcodes of short binary instructions
36 **/
37
38 /**
39 * push a short onto the stack
40 **/
41 public static final byte SIPUSH = (byte)17;
42 /**
43 * create a new array
44 **/
45 public static final byte NEWARRAY = (byte)188;
46
47 /**
48 * Creates an WideBinaryInstruction and initializes it from a
49 * DataInputStream
50 * @param the opcode of the command
51 * @param dis the DataInputStream to read from
52 * @param cp the constant pool to resolve indices
53 **/
54 protected WideBinaryInstruction(byte command, DataInputStream dis,
55 ConstantPool cp) throws IOException {
56 super(command);
57 handleOperand((short)dis.readUnsignedShort(), cp);
58 }
59
60 /**
61 * Create a new binary instruction with a command and the short value
62 * as operand
63 * @param command
64 * @param operand
65 **/
66 public WideBinaryInstruction(byte command) {
67 super(command);
68 }
69
70 /**
71 * Handle operand. By default, the operands value is stored. Another
72 * behaviour may be implemented in an extending class
73 * @param operand to handle
74 * @param cp the constant pool to resolve indices
75 **/
76 protected void handleOperand(short operand, ConstantPool cp) {
77 this.operand = operand;
78 }
79
80 /**
81 * Write the instruction onto the DataOutputStream, using the
82 * constant pool to register variables. .
83 * @param dos the DataOutputStream to write on
84 * @param cp The constant pool to register variables
85 **/
86 public final void write(DataOutputStream dos, ConstantPool cp)
87 throws IOException {
88 super.write(dos, cp);
89 dos.writeShort(getOperand(cp));
90 }
91
92 /**
93 * @param cp The constant pool to register variables
94 * @return the short value of the operand, by default, the previously
95 * stored operands value will be returned. Another behiour may be
96 * implemented in an extending class.
97 **/
98 protected short getOperand(ConstantPool cp) {
99 return operand;
100 }
101
102 /**
103 * @return the number of bytes used in this command
104 **/
105 public final int getNumberOfUsedBytes() {
106 return 3;
107 }
108
109 /**
110 * Write code in a readable way
111 **/
112 public String toString() {
113 return super.toString() + " " + operand;
114 }
115 }