Source code: com/techtrader/modules/tools/bytecode/ArrayInstruction.java
1 package com.techtrader.modules.tools.bytecode;
2
3
4 import java.util.Map;
5 import java.util.HashMap;
6
7
8 /**
9 * Represents any array load or store instruction.
10 *
11 * @author Abe White
12 */
13 public abstract class ArrayInstruction
14 extends Instruction
15 {
16 protected static final Map _typeNames = new HashMap ();
17 static
18 {
19 _typeNames.put ("int", int.class);
20 _typeNames.put ("boolean", int.class);
21 _typeNames.put ("long", long.class);
22 _typeNames.put ("float", float.class);
23 _typeNames.put ("double", double.class);
24 _typeNames.put ("byte", byte.class);
25 _typeNames.put ("char", char.class);
26 _typeNames.put ("short", short.class);
27 }
28
29 protected Class _type = null;
30
31
32 protected ArrayInstruction (Code owner)
33 {
34 super (owner);
35 }
36
37
38 protected ArrayInstruction (Code owner, int opcode, Class type)
39 {
40 super (owner);
41 _opcode = opcode;
42 _type = type;
43 }
44
45
46 /**
47 * Get the type of array to load; this is one of:
48 * int, float, double, long, char, byte, short, or Object.class.
49 * If the type has not been set, this method will return null.
50 */
51 public Class getType ()
52 {
53 return _type;
54 }
55
56
57 /**
58 * Get the type of array to load; this is one of:
59 * int, float, double, long, char, byte, short, or java.lang.Object.
60 * If the type has not been set, this method will return null.
61 */
62 public String getTypeName ()
63 {
64 if (_type == null)
65 return null;
66
67 return _type.getName ();
68 }
69
70
71 /**
72 * Set the type of array to load; Object types other than Object.class
73 * will be demoted to Object.class, and primitives that have no direct
74 * support (boolean) will be converted to int.class.
75 *
76 * @return this Instruction, for method chaining
77 */
78 public ArrayInstruction setType (Class type)
79 {
80 if (Object.class.isAssignableFrom (type))
81 _type = Object.class;
82 else if (boolean.class.equals (type))
83 _type = int.class;
84 else
85 _type = type;
86
87 calculateOpCode ();
88 return this;
89 }
90
91
92 /**
93 * Set the type to load by name.
94 *
95 * @return this Instruction, for method chaining
96 *
97 * @see #setType(java.lang.Class)
98 */
99 public ArrayInstruction setTypeName (String name)
100 {
101 _type = (Class) _typeNames.get (name);
102 if (_type == null && name != null)
103 _type = Object.class;
104
105 calculateOpCode ();
106 return this;
107 }
108
109
110 /**
111 * Two array instructions are equal if the index and type they
112 * reference are equal or if either index is 0/unset.
113 */
114 public boolean equals (Object other)
115 {
116 if (this == other)
117 return true;
118 if (!(other instanceof ArrayInstruction))
119 return false;
120
121 ArrayInstruction ins = (ArrayInstruction) other;
122
123 return (_type == null || ins._type == null || _type.equals (ins._type));
124 }
125
126
127 protected void copy (Instruction orig)
128 {
129 super.copy (orig);
130 _type = (((ArrayInstruction) orig)._type);
131 }
132
133
134 /**
135 * Subclasses with variable opcodes can use this method to be
136 * notified that information possibly affecting the opcode has been
137 * changed.
138 */
139 protected abstract void calculateOpCode ();
140 }