1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.classfile;
18
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import org.apache.bcel.Constants;
23
24 /***
25 * Abstract super class for fields and methods.
26 *
27 * @version $Id: FieldOrMethod.java 386056 2006-03-15 11:31:56Z tcurdt $
28 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
29 */
30 public abstract class FieldOrMethod extends AccessFlags implements Cloneable, Node {
31
32 protected int name_index;
33 protected int signature_index;
34 protected int attributes_count;
35 protected Attribute[] attributes;
36 protected ConstantPool constant_pool;
37
38
39 FieldOrMethod() {
40 }
41
42
43 /***
44 * Initialize from another object. Note that both objects use the same
45 * references (shallow copy). Use clone() for a physical copy.
46 */
47 protected FieldOrMethod(FieldOrMethod c) {
48 this(c.getAccessFlags(), c.getNameIndex(), c.getSignatureIndex(), c.getAttributes(), c
49 .getConstantPool());
50 }
51
52
53 /***
54 * Construct object from file stream.
55 * @param file Input stream
56 * @throws IOException
57 * @throws ClassFormatException
58 */
59 protected FieldOrMethod(DataInputStream file, ConstantPool constant_pool) throws IOException,
60 ClassFormatException {
61 this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), null,
62 constant_pool);
63 attributes_count = file.readUnsignedShort();
64 attributes = new Attribute[attributes_count];
65 for (int i = 0; i < attributes_count; i++) {
66 attributes[i] = Attribute.readAttribute(file, constant_pool);
67 }
68 }
69
70
71 /***
72 * @param access_flags Access rights of method
73 * @param name_index Points to field name in constant pool
74 * @param signature_index Points to encoded signature
75 * @param attributes Collection of attributes
76 * @param constant_pool Array of constants
77 */
78 protected FieldOrMethod(int access_flags, int name_index, int signature_index,
79 Attribute[] attributes, ConstantPool constant_pool) {
80 this.access_flags = access_flags;
81 this.name_index = name_index;
82 this.signature_index = signature_index;
83 this.constant_pool = constant_pool;
84 setAttributes(attributes);
85 }
86
87
88 /***
89 * Dump object to file stream on binary format.
90 *
91 * @param file Output file stream
92 * @throws IOException
93 */
94 public final void dump( DataOutputStream file ) throws IOException {
95 file.writeShort(access_flags);
96 file.writeShort(name_index);
97 file.writeShort(signature_index);
98 file.writeShort(attributes_count);
99 for (int i = 0; i < attributes_count; i++) {
100 attributes[i].dump(file);
101 }
102 }
103
104
105 /***
106 * @return Collection of object attributes.
107 */
108 public final Attribute[] getAttributes() {
109 return attributes;
110 }
111
112
113 /***
114 * @param attributes Collection of object attributes.
115 */
116 public final void setAttributes( Attribute[] attributes ) {
117 this.attributes = attributes;
118 attributes_count = (attributes == null) ? 0 : attributes.length;
119 }
120
121
122 /***
123 * @return Constant pool used by this object.
124 */
125 public final ConstantPool getConstantPool() {
126 return constant_pool;
127 }
128
129
130 /***
131 * @param constant_pool Constant pool to be used for this object.
132 */
133 public final void setConstantPool( ConstantPool constant_pool ) {
134 this.constant_pool = constant_pool;
135 }
136
137
138 /***
139 * @return Index in constant pool of object's name.
140 */
141 public final int getNameIndex() {
142 return name_index;
143 }
144
145
146 /***
147 * @param name_index Index in constant pool of object's name.
148 */
149 public final void setNameIndex( int name_index ) {
150 this.name_index = name_index;
151 }
152
153
154 /***
155 * @return Index in constant pool of field signature.
156 */
157 public final int getSignatureIndex() {
158 return signature_index;
159 }
160
161
162 /***
163 * @param signature_index Index in constant pool of field signature.
164 */
165 public final void setSignatureIndex( int signature_index ) {
166 this.signature_index = signature_index;
167 }
168
169
170 /***
171 * @return Name of object, i.e., method name or field name
172 */
173 public final String getName() {
174 ConstantUtf8 c;
175 c = (ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
176 return c.getBytes();
177 }
178
179
180 /***
181 * @return String representation of object's type signature (java style)
182 */
183 public final String getSignature() {
184 ConstantUtf8 c;
185 c = (ConstantUtf8) constant_pool.getConstant(signature_index, Constants.CONSTANT_Utf8);
186 return c.getBytes();
187 }
188
189
190 /***
191 * @return deep copy of this field
192 */
193 protected FieldOrMethod copy_( ConstantPool _constant_pool ) {
194 try {
195 FieldOrMethod c = (FieldOrMethod) clone();
196 c.constant_pool = _constant_pool;
197 c.attributes = new Attribute[attributes_count];
198 for (int i = 0; i < attributes_count; i++) {
199 c.attributes[i] = attributes[i].copy(_constant_pool);
200 }
201 return c;
202 } catch (CloneNotSupportedException e) {
203 return null;
204 }
205 }
206 }