Source code: com/techtrader/modules/tools/bytecode/InnerClass.java
1 package com.techtrader.modules.tools.bytecode;
2
3
4 import java.io.DataOutput;
5 import java.io.DataInput;
6 import java.io.IOException;
7
8 import com.techtrader.modules.tools.bytecode.visitor.BCVisitor;
9
10
11 /**
12 * Represents an inner class.
13 * TODO: add high-level operations for manipulating the type of the
14 * inner class.
15 *
16 * @author Abe White
17 */
18 public class InnerClass
19 implements Constants
20 {
21 private int _nameIndex = 0;
22 private int _index = 0;
23 private int _ownerIndex = 0;
24 private int _access = ACCESS_PRIVATE;
25
26 private InnerClassesAttribute _owner = null;
27
28
29 /**
30 * Protected constructor. Used when reading from a .class file.
31 */
32 protected InnerClass (InnerClassesAttribute owner)
33 {
34 _owner = owner;
35 }
36
37
38 /**
39 * Protected constructor. Used when adding inner classes programmatically.
40 */
41 protected InnerClass (String name, InnerClassesAttribute owner)
42 {
43 _owner = owner;
44
45 if (name != null && name.length () > 0)
46 _nameIndex = _owner.getPool ().setUTF (0, name);
47 }
48
49
50 /**
51 * Used to invalidate an inner class when removed, so that it can
52 * no longer affect the constant pool.
53 */
54 protected void invalidate ()
55 {
56 _owner = null;
57 }
58
59
60 /**
61 * Inner classes are owned by InnerClassesAttributes.
62 */
63 public InnerClassesAttribute getOwner ()
64 {
65 return _owner;
66 }
67
68
69 /**
70 * Get the access flags on the inner class.
71 */
72 public int getAccessFlags ()
73 {
74 return _access;
75 }
76
77
78 /**
79 * Set the access flags on the inner class.
80 */
81 public void setAccessFlags (int accessFlags)
82 {
83 _access = accessFlags;
84 }
85
86
87 /**
88 * Manipulate the inner class access flags.
89 */
90 public boolean isPublic ()
91 {
92 return BCHelper.hasFlag (_access, ACCESS_PUBLIC);
93 }
94
95
96 /**
97 * Manipulate the inner class access flags.
98 */
99 public void makePublic ()
100 {
101 _access = BCHelper.setFlag (_access, ACCESS_PUBLIC, true);
102 _access = BCHelper.setFlag (_access, ACCESS_PRIVATE, false);
103 _access = BCHelper.setFlag (_access, ACCESS_PROTECTED, false);
104 }
105
106
107 /**
108 * Manipulate the inner class access flags.
109 */
110 public boolean isProtected ()
111 {
112 return BCHelper.hasFlag (_access, ACCESS_PROTECTED);
113 }
114
115
116 /**
117 * Manipulate the inner class access flags.
118 */
119 public void makeProtected ()
120 {
121 _access = BCHelper.setFlag (_access, ACCESS_PUBLIC, false);
122 _access = BCHelper.setFlag (_access, ACCESS_PRIVATE, false);
123 _access = BCHelper.setFlag (_access, ACCESS_PROTECTED, true);
124 }
125
126
127 /**
128 * Manipulate the inner class access flags.
129 */
130 public boolean isPrivate ()
131 {
132 return BCHelper.hasFlag (_access, ACCESS_PRIVATE);
133 }
134
135
136 /**
137 * Manipulate the inner class access flags.
138 */
139 public void makePrivate ()
140 {
141 _access = BCHelper.setFlag (_access, ACCESS_PUBLIC, false);
142 _access = BCHelper.setFlag (_access, ACCESS_PRIVATE, true);
143 _access = BCHelper.setFlag (_access, ACCESS_PROTECTED, false);
144 }
145
146
147 /**
148 * Manipulate the inner class access flags.
149 */
150 public boolean isFinal ()
151 {
152 return BCHelper.hasFlag (_access, ACCESS_FINAL);
153 }
154
155
156 /**
157 * Manipulate the inner class access flags.
158 */
159 public void setFinal (boolean on)
160 {
161 _access = BCHelper.setFlag (_access, ACCESS_FINAL, on);
162 }
163
164
165 /**
166 * Manipulate the inner class access flags.
167 */
168 public boolean isStatic ()
169 {
170 return BCHelper.hasFlag (_access, ACCESS_STATIC);
171 }
172
173
174 /**
175 * Manipulate the inner class access flags.
176 */
177 public void setStatic (boolean on)
178 {
179 _access = BCHelper.setFlag (_access, ACCESS_STATIC, on);
180 }
181
182
183 /**
184 * Manipulate the class access flags.
185 */
186 public boolean isInterface ()
187 {
188 return BCHelper.hasFlag (_access, ACCESS_INTERFACE);
189 }
190
191
192 /**
193 * Manipulate the class access flags.
194 */
195 public void setInterface (boolean on)
196 {
197 _access = BCHelper.setFlag (_access, ACCESS_INTERFACE, on);
198 if (on)
199 setAbstract (true);
200 }
201
202
203 /**
204 * Manipulate the class access flags.
205 */
206 public boolean isAbstract ()
207 {
208 return BCHelper.hasFlag (_access, ACCESS_ABSTRACT);
209 }
210
211
212 /**
213 * Manipulate the class access flags.
214 */
215 public void setAbstract (boolean on)
216 {
217 _access = BCHelper.setFlag (_access, ACCESS_INTERFACE, on);
218 }
219
220
221 /**
222 * Get the index into the constant pool of the ClassEntry that describeds
223 * this class.
224 */
225 public int getIndex ()
226 {
227 return _index;
228 }
229
230
231 /**
232 * Set the index into the constant pool of the ClassEntry that describeds
233 * this class.
234 */
235 public void setIndex (int index)
236 {
237 _index = index;
238 }
239
240
241 /**
242 * Get the index into the constant pool of the UTF8Entry that holds
243 * the name of the class.
244 */
245 public int getNameIndex ()
246 {
247 return _nameIndex;
248 }
249
250
251 /**
252 * Set the index into the constant pool of the UTF8Entry that holds
253 * the name of the class.
254 */
255 public void setNameIndex (int nameIndex)
256 {
257 _nameIndex = nameIndex;
258 }
259
260
261 /**
262 * Get the name of this field.
263 */
264 public String getName ()
265 {
266 return _owner.getPool ().getUTF (_nameIndex);
267 }
268
269
270 /**
271 * Set the name of this field.
272 */
273 public void setName (String name)
274 {
275 _nameIndex = _owner.getPool ().setUTF (0, name);
276 }
277
278
279 /**
280 * Get the index into the constant pool of the ClassEntry describing
281 * the outer class.
282 */
283 public int getOuterClassIndex ()
284 {
285 return _ownerIndex;
286 }
287
288
289 /**
290 * Set the index into the constant pool of the ClassEntry describing
291 * the outer class.
292 */
293 public void setOuterClassIndex (int ownerIndex)
294 {
295 _ownerIndex = ownerIndex;
296 }
297
298
299 protected void readData (DataInput in)
300 throws IOException
301 {
302 setIndex (in.readUnsignedShort ());
303 setOuterClassIndex (in.readUnsignedShort ());
304 setNameIndex (in.readUnsignedShort ());
305 setAccessFlags (in.readUnsignedShort ());
306 }
307
308
309 protected void writeData (DataOutput out)
310 throws IOException
311 {
312 out.writeShort (getIndex ());
313 out.writeShort (getOuterClassIndex ());
314 out.writeShort (getNameIndex ());
315 out.writeShort (getAccessFlags ());
316 }
317
318
319 public void acceptVisit (BCVisitor visit)
320 {
321 visit.enterInnerClass (this);
322 visit.exitInnerClass (this);
323 }
324 }