Source code: com/techtrader/modules/tools/bytecode/NewArrayInstruction.java
1 package com.techtrader.modules.tools.bytecode;
2
3
4 import java.util.Map;
5 import java.util.HashMap;
6 import java.io.IOException;
7 import java.io.DataInput;
8 import java.io.DataOutput;
9
10 import com.techtrader.modules.tools.bytecode.visitor.BCVisitor;
11
12
13 /**
14 * Represents a NEWARRAY instruction, which is used to create new arrays
15 * of primitive types.
16 *
17 * @author Abe White
18 */
19 public class NewArrayInstruction
20 extends Instruction
21 {
22 private static final int[] _arrayCodes = new int[] {
23 ARRAY_BOOLEAN, ARRAY_CHAR, ARRAY_FLOAT, ARRAY_DOUBLE,
24 ARRAY_BYTE, ARRAY_SHORT, ARRAY_INT, ARRAY_LONG
25 };
26 private static final Class[] _arrayTypes = new Class[] {
27 boolean.class, char.class, float.class, double.class,
28 byte.class, short.class, int.class, long.class
29 };
30 private static final String[] _arrayNames = new String[] {
31 "boolean", "char", "float", "double",
32 "byte", "short", "int", "long"
33 };
34
35 private int _code = 0;
36
37
38 protected NewArrayInstruction (Code owner)
39 {
40 super (owner);
41 _opcode = NEWARRAY;
42 }
43
44
45 /**
46 * Get the array code used in the lowlevel bytecode.
47 */
48 public int getArrayTypeCode ()
49 {
50 return _code;
51 }
52
53
54 /**
55 * Set the array code used in the lowlevel bytecode.
56 */
57 public void setArrayTypeCode (int code)
58 {
59 _code = code;
60 }
61
62
63 /**
64 * Get the type of array to create.
65 */
66 public Class getArrayType ()
67 {
68 for (int i = 0; i < _arrayCodes.length; i++)
69 if (_code == _arrayCodes[i])
70 return _arrayTypes[i];
71
72 return null;
73 }
74
75
76 /**
77 * Get the type of array to create.
78 */
79 public String getArrayTypeName ()
80 {
81 for (int i = 0; i < _arrayCodes.length; i++)
82 if (_code == _arrayCodes[i])
83 return _arrayTypes[i].getName ();
84
85 return null;
86 }
87
88
89 /**
90 * Set the type of array to create.
91 *
92 * @return this Instruction, for method chaining
93 */
94 public NewArrayInstruction setArrayType (Class type)
95 {
96 _code = 0;
97 for (int i = 0; i < _arrayTypes.length; i++)
98 {
99 if (_arrayTypes[i].equals (type))
100 {
101 _code = _arrayCodes[i];
102 break;
103 }
104 }
105 return this;
106 }
107
108
109 /**
110 * Set the type of array to create.
111 *
112 * @return this Instruction, for method chaining
113 */
114 public NewArrayInstruction setArrayTypeName (String type)
115 {
116 _code = 0;
117 for (int i = 0; i < _arrayNames.length; i++)
118 {
119 if (_arrayNames[i].equals (type))
120 {
121 _code = _arrayCodes[i];
122 break;
123 }
124 }
125 return this;
126 }
127
128
129 /**
130 * Two NEWARRAY instructions are equal if the array type is the same,
131 * of if the array type of either is unset.
132 */
133 public boolean equals (Object other)
134 {
135 if (this == other)
136 return true;
137 if (!(other instanceof NewArrayInstruction))
138 return false;
139 if (!super.equals (other))
140 return false;
141
142 NewArrayInstruction ins = (NewArrayInstruction) other;
143
144 return getArrayType () == null || ins.getArrayType () == null
145 || getArrayType ().equals (ins.getArrayType ());
146 }
147
148
149 public int getLength ()
150 {
151 return super.getLength () + 1;
152 }
153
154
155 protected void copy (Instruction orig)
156 {
157 super.copy (orig);
158 setArrayType (((NewArrayInstruction) orig).getArrayType ());
159 }
160
161
162 protected void readData (DataInput in)
163 throws IOException
164 {
165 _code = in.readUnsignedByte ();
166 }
167
168
169 protected void writeData (DataOutput out)
170 throws IOException
171 {
172 out.writeByte (_code);
173 }
174
175
176 public void acceptVisit (BCVisitor visit)
177 {
178 visit.enterNewArrayInstruction (this);
179 visit.exitNewArrayInstruction (this);
180 }
181 }