KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > FieldInfo


1 /*
2  * Copyright 2004 Brian S O'Neill
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.cojen.classfile;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.io.DataInput JavaDoc;
22 import java.io.DataOutput JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Modifier JavaDoc;
25 import org.cojen.classfile.attribute.Annotation;
26 import org.cojen.classfile.attribute.AnnotationsAttr;
27 import org.cojen.classfile.attribute.ConstantValueAttr;
28 import org.cojen.classfile.attribute.DeprecatedAttr;
29 import org.cojen.classfile.attribute.RuntimeInvisibleAnnotationsAttr;
30 import org.cojen.classfile.attribute.RuntimeVisibleAnnotationsAttr;
31 import org.cojen.classfile.attribute.SignatureAttr;
32 import org.cojen.classfile.attribute.SyntheticAttr;
33 import org.cojen.classfile.constant.ConstantUTFInfo;
34
35 /**
36  * This class corresponds to the field_info structure as defined in
37  * section 4.5 of <i>The Java Virtual Machine Specification</i>.
38  *
39  * @author Brian S O'Neill
40  * @see ClassFile
41  */

42 public class FieldInfo {
43     private ClassFile mParent;
44     private ConstantPool mCp;
45
46     private String JavaDoc mName;
47     private TypeDesc mType;
48
49     private Modifiers mModifiers;
50
51     private ConstantUTFInfo mNameConstant;
52     private ConstantUTFInfo mDescriptorConstant;
53     
54     private List JavaDoc mAttributes = new ArrayList JavaDoc(2);
55
56     private ConstantValueAttr mConstant;
57     
58     FieldInfo(ClassFile parent,
59               Modifiers modifiers,
60               String JavaDoc name,
61               TypeDesc type) {
62
63         mParent = parent;
64         mCp = parent.getConstantPool();
65         mName = name;
66         mType = type;
67
68         mModifiers = modifiers;
69         mNameConstant = mCp.addConstantUTF(name);
70         mDescriptorConstant = mCp.addConstantUTF(type.getDescriptor());
71     }
72     
73     private FieldInfo(ClassFile parent,
74                       int modifier,
75                       ConstantUTFInfo nameConstant,
76                       ConstantUTFInfo descConstant) {
77
78         mParent = parent;
79         mCp = parent.getConstantPool();
80         mName = nameConstant.getValue();
81         mType = TypeDesc.forDescriptor(descConstant.getValue());
82
83         mModifiers = Modifiers.getInstance(modifier);
84         mNameConstant = nameConstant;
85         mDescriptorConstant = descConstant;
86     }
87
88     /**
89      * Returns the parent ClassFile for this FieldInfo.
90      */

91     public ClassFile getClassFile() {
92         return mParent;
93     }
94
95     /**
96      * Returns the name of this field.
97      */

98     public String JavaDoc getName() {
99         return mName;
100     }
101
102     /**
103      * Returns the type of this field.
104      */

105     public TypeDesc getType() {
106         return mType;
107     }
108     
109     /**
110      * Returns this field's modifiers.
111      */

112     public Modifiers getModifiers() {
113         return mModifiers;
114     }
115
116     public void setModifiers(Modifiers modifiers) {
117         mModifiers = modifiers;
118     }
119
120     /**
121      * Returns a constant from the constant pool with this field's name.
122      */

123     public ConstantUTFInfo getNameConstant() {
124         return mNameConstant;
125     }
126     
127     /**
128      * Returns a constant from the constant pool with this field's type
129      * descriptor string.
130      * @see TypeDesc
131      */

132     public ConstantUTFInfo getDescriptorConstant() {
133         return mDescriptorConstant;
134     }
135
136     /**
137      * Returns the constant value for this field or null if no constant set.
138      */

139     public ConstantInfo getConstantValue() {
140         if (mConstant == null) {
141             return null;
142         } else {
143             return mConstant.getConstant();
144         }
145     }
146
147     public boolean isSynthetic() {
148         for (int i = mAttributes.size(); --i >= 0; ) {
149             Object JavaDoc obj = mAttributes.get(i);
150             if (obj instanceof SyntheticAttr) {
151                 return true;
152             }
153         }
154         return false;
155     }
156
157     public boolean isDeprecated() {
158         for (int i = mAttributes.size(); --i >= 0; ) {
159             Object JavaDoc obj = mAttributes.get(i);
160             if (obj instanceof DeprecatedAttr) {
161                 return true;
162             }
163         }
164         return false;
165     }
166
167     /**
168      * Returns all the runtime invisible annotations defined for this class
169      * file, or an empty array if none.
170      */

171     public Annotation[] getRuntimeInvisibleAnnotations() {
172         for (int i = mAttributes.size(); --i >= 0; ) {
173             Object JavaDoc obj = mAttributes.get(i);
174             if (obj instanceof RuntimeInvisibleAnnotationsAttr) {
175                 return ((AnnotationsAttr) obj).getAnnotations();
176             }
177         }
178         return new Annotation[0];
179     }
180
181     /**
182      * Returns all the runtime visible annotations defined for this class file,
183      * or an empty array if none.
184      */

185     public Annotation[] getRuntimeVisibleAnnotations() {
186         for (int i = mAttributes.size(); --i >= 0; ) {
187             Object JavaDoc obj = mAttributes.get(i);
188             if (obj instanceof RuntimeVisibleAnnotationsAttr) {
189                 return ((AnnotationsAttr) obj).getAnnotations();
190             }
191         }
192         return new Annotation[0];
193     }
194
195     /**
196      * Add a runtime invisible annotation.
197      */

198     public Annotation addRuntimeInvisibleAnnotation(TypeDesc type) {
199         AnnotationsAttr attr = null;
200         for (int i = mAttributes.size(); --i >= 0; ) {
201             Object JavaDoc obj = mAttributes.get(i);
202             if (obj instanceof RuntimeInvisibleAnnotationsAttr) {
203                 attr = (AnnotationsAttr) obj;
204             }
205         }
206         if (attr == null) {
207             attr = new RuntimeInvisibleAnnotationsAttr(mCp);
208             addAttribute(attr);
209         }
210         Annotation ann = new Annotation(mCp);
211         ann.setType(type);
212         attr.addAnnotation(ann);
213         return ann;
214     }
215
216     /**
217      * Add a runtime visible annotation.
218      */

219     public Annotation addRuntimeVisibleAnnotation(TypeDesc type) {
220         AnnotationsAttr attr = null;
221         for (int i = mAttributes.size(); --i >= 0; ) {
222             Object JavaDoc obj = mAttributes.get(i);
223             if (obj instanceof RuntimeVisibleAnnotationsAttr) {
224                 attr = (AnnotationsAttr) obj;
225             }
226         }
227         if (attr == null) {
228             attr = new RuntimeVisibleAnnotationsAttr(mCp);
229             addAttribute(attr);
230         }
231         Annotation ann = new Annotation(mCp);
232         ann.setType(type);
233         attr.addAnnotation(ann);
234         return ann;
235     }
236
237     /**
238      * Returns the signature attribute of this field, or null if none is
239      * defined.
240      */

241     // TODO: Eventually remove this method
242
public SignatureAttr getSignatureAttr() {
243         for (int i = mAttributes.size(); --i >= 0; ) {
244             Object JavaDoc obj = mAttributes.get(i);
245             if (obj instanceof SignatureAttr) {
246                 return (SignatureAttr) obj;
247             }
248         }
249         return null;
250     }
251
252     /**
253      * Set the constant value for this field as an int.
254      */

255     public void setConstantValue(int value) {
256         addAttribute(new ConstantValueAttr(mCp, mCp.addConstantInteger(value)));
257     }
258
259     /**
260      * Set the constant value for this field as a float.
261      */

262     public void setConstantValue(float value) {
263         addAttribute(new ConstantValueAttr(mCp, mCp.addConstantFloat(value)));
264     }
265
266     /**
267      * Set the constant value for this field as a long.
268      */

269     public void setConstantValue(long value) {
270         addAttribute(new ConstantValueAttr(mCp, mCp.addConstantLong(value)));
271     }
272
273     /**
274      * Set the constant value for this field as a double.
275      */

276     public void setConstantValue(double value) {
277         addAttribute(new ConstantValueAttr(mCp, mCp.addConstantDouble(value)));
278     }
279
280     /**
281      * Set the constant value for this field as a string.
282      */

283     public void setConstantValue(String JavaDoc value) {
284         addAttribute(new ConstantValueAttr(mCp, mCp.addConstantString(value)));
285     }
286     
287     /**
288      * Mark this field as being synthetic by adding a special attribute.
289      */

290     public void markSynthetic() {
291         addAttribute(new SyntheticAttr(mCp));
292     }
293
294     /**
295      * Mark this field as being deprecated by adding a special attribute.
296      */

297     public void markDeprecated() {
298         addAttribute(new DeprecatedAttr(mCp));
299     }
300
301     public void addAttribute(Attribute attr) {
302         if (attr instanceof ConstantValueAttr) {
303             if (mConstant != null) {
304                 mAttributes.remove(mConstant);
305             }
306             mConstant = (ConstantValueAttr)attr;
307         }
308
309         mAttributes.add(attr);
310     }
311
312     public Attribute[] getAttributes() {
313         Attribute[] attrs = new Attribute[mAttributes.size()];
314         return (Attribute[])mAttributes.toArray(attrs);
315     }
316     
317     /**
318      * Returns the length (in bytes) of this object in the class file.
319      */

320     public int getLength() {
321         int length = 8;
322         
323         int size = mAttributes.size();
324         for (int i=0; i<size; i++) {
325             length += ((Attribute)mAttributes.get(i)).getLength();
326         }
327         
328         return length;
329     }
330     
331     public void writeTo(DataOutput JavaDoc dout) throws IOException JavaDoc {
332         dout.writeShort(mModifiers.getBitmask());
333         dout.writeShort(mNameConstant.getIndex());
334         dout.writeShort(mDescriptorConstant.getIndex());
335         
336         int size = mAttributes.size();
337         dout.writeShort(size);
338         for (int i=0; i<size; i++) {
339             Attribute attr = (Attribute)mAttributes.get(i);
340             attr.writeTo(dout);
341         }
342     }
343
344     public String JavaDoc toString() {
345         String JavaDoc modStr = mModifiers.toString();
346         String JavaDoc typeStr;
347         if (modStr.length() == 0) {
348             return mType.getFullName() + ' ' + getName();
349         } else {
350             return modStr + ' ' + mType.getFullName() + ' ' + getName();
351         }
352     }
353
354     static FieldInfo readFrom(ClassFile parent,
355                               DataInput JavaDoc din,
356                               AttributeFactory attrFactory)
357         throws IOException JavaDoc
358     {
359         ConstantPool cp = parent.getConstantPool();
360
361         int modifier = din.readUnsignedShort();
362         int index = din.readUnsignedShort();
363         ConstantUTFInfo nameConstant = (ConstantUTFInfo)cp.getConstant(index);
364         index = din.readUnsignedShort();
365         ConstantUTFInfo descConstant = (ConstantUTFInfo)cp.getConstant(index);
366
367         FieldInfo info = new FieldInfo(parent, modifier,
368                                        nameConstant, descConstant);
369
370         // Read attributes.
371
int size = din.readUnsignedShort();
372         for (int i=0; i<size; i++) {
373             info.addAttribute(Attribute.readFrom(cp, din, attrFactory));
374         }
375
376         return info;
377     }
378 }
379
Popular Tags