KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > classfile > FieldInfo


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.classfile;
54
55 import java.util.*;
56 import java.io.*;
57 import java.lang.reflect.Modifier JavaDoc;
58
59 /******************************************************************************
60  * This class corresponds to the field_info structure as defined in
61  * section 4.5 of <i>The Java Virtual Machine Specification</i>.
62  *
63  * @author Brian S O'Neill
64  * @version
65  * <!--$$Revision:--> 24 <!-- $-->, <!--$$JustDate:--> 00/11/27 <!-- $-->
66  * @see ClassFile
67  */

68 public class FieldInfo {
69     private ClassFile mParent;
70     private ConstantPool mCp;
71
72     private String JavaDoc mName;
73     private TypeDescriptor mType;
74
75     private int mAccessFlags;
76
77     private ConstantUTFInfo mNameConstant;
78     private ConstantUTFInfo mDescriptorConstant;
79     
80     private List mAttributes = new ArrayList(2);
81
82     private ConstantValueAttr mConstant;
83     
84     FieldInfo(ClassFile parent,
85               AccessFlags flags,
86               String JavaDoc name,
87               TypeDescriptor type) {
88
89         mParent = parent;
90         mCp = parent.getConstantPool();
91         mName = name;
92         mType = type;
93
94         mAccessFlags = flags.getModifier();
95         mNameConstant = ConstantUTFInfo.make(mCp, name);
96         mDescriptorConstant = ConstantUTFInfo.make(mCp, type.toString());
97     }
98     
99     private FieldInfo(ClassFile parent,
100                       int flags,
101                       ConstantUTFInfo nameConstant,
102                       ConstantUTFInfo descConstant) {
103
104         mParent = parent;
105         mCp = parent.getConstantPool();
106         mName = nameConstant.getValue();
107         mType = TypeDescriptor.parseTypeDesc(descConstant.getValue());
108
109         mAccessFlags = flags;
110         mNameConstant = nameConstant;
111         mDescriptorConstant = descConstant;
112     }
113
114     /**
115      * Returns the parent ClassFile for this FieldInfo.
116      */

117     public ClassFile getClassFile() {
118         return mParent;
119     }
120
121     /**
122      * Returns the name of this field.
123      */

124     public String JavaDoc getName() {
125         return mName;
126     }
127
128     /**
129      * Returns the type of this field.
130      */

131     public TypeDescriptor getType() {
132         return mType;
133     }
134     
135     /**
136      * Returns a copy of this field's access flags.
137      */

138     public AccessFlags getAccessFlags() {
139         return new AccessFlags(mAccessFlags);
140     }
141
142     /**
143      * Returns a constant from the constant pool with this field's name.
144      */

145     public ConstantUTFInfo getNameConstant() {
146         return mNameConstant;
147     }
148     
149     /**
150      * Returns a constant from the constant pool with this field's type
151      * descriptor string.
152      * @see TypeDescriptor
153      */

154     public ConstantUTFInfo getDescriptorConstant() {
155         return mDescriptorConstant;
156     }
157
158     /**
159      * Returns the constant value for this field or null if no constant set.
160      */

161     public ConstantInfo getConstantValue() {
162         if (mConstant == null) {
163             return null;
164         }
165         else {
166             return mConstant.getConstant();
167         }
168     }
169
170     public boolean isSynthetic() {
171         for (int i = mAttributes.size(); --i >= 0; ) {
172             Object JavaDoc obj = mAttributes.get(i);
173             if (obj instanceof SyntheticAttr) {
174                 return true;
175             }
176         }
177         return false;
178     }
179
180     public boolean isDeprecated() {
181         for (int i = mAttributes.size(); --i >= 0; ) {
182             Object JavaDoc obj = mAttributes.get(i);
183             if (obj instanceof DeprecatedAttr) {
184                 return true;
185             }
186         }
187         return false;
188     }
189
190     /**
191      * Set the constant value for this field as an int.
192      */

193     public void setConstantValue(int value) {
194         addAttribute(new ConstantValueAttr
195                      (mCp, ConstantIntegerInfo.make(mCp, value)));
196     }
197
198     /**
199      * Set the constant value for this field as a float.
200      */

201     public void setConstantValue(float value) {
202         addAttribute(new ConstantValueAttr
203                      (mCp, ConstantFloatInfo.make(mCp, value)));
204     }
205
206     /**
207      * Set the constant value for this field as a long.
208      */

209     public void setConstantValue(long value) {
210         addAttribute(new ConstantValueAttr
211                      (mCp, ConstantLongInfo.make(mCp, value)));
212     }
213
214     /**
215      * Set the constant value for this field as a double.
216      */

217     public void setConstantValue(double value) {
218         addAttribute(new ConstantValueAttr
219                      (mCp, ConstantDoubleInfo.make(mCp, value)));
220     }
221
222     /**
223      * Set the constant value for this field as a string.
224      */

225     public void setConstantValue(String JavaDoc value) {
226         addAttribute(new ConstantValueAttr
227                      (mCp, ConstantStringInfo.make(mCp, value)));
228     }
229     
230     /**
231      * Mark this field as being synthetic by adding a special attribute.
232      */

233     public void markSynthetic() {
234         addAttribute(new SyntheticAttr(mCp));
235     }
236
237     /**
238      * Mark this field as being deprecated by adding a special attribute.
239      */

240     public void markDeprecated() {
241         addAttribute(new DeprecatedAttr(mCp));
242     }
243
244     public void addAttribute(Attribute attr) {
245         if (attr instanceof ConstantValueAttr) {
246             if (mConstant != null) {
247                 mAttributes.remove(mConstant);
248             }
249             mConstant = (ConstantValueAttr)attr;
250         }
251
252         mAttributes.add(attr);
253     }
254
255     public Attribute[] getAttributes() {
256         Attribute[] attrs = new Attribute[mAttributes.size()];
257         return (Attribute[])mAttributes.toArray(attrs);
258     }
259     
260     /**
261      * Returns the length (in bytes) of this object in the class file.
262      */

263     public int getLength() {
264         int length = 8;
265         
266         int size = mAttributes.size();
267         for (int i=0; i<size; i++) {
268             length += ((Attribute)mAttributes.get(i)).getLength();
269         }
270         
271         return length;
272     }
273     
274     public void writeTo(DataOutput dout) throws IOException {
275         dout.writeShort(mAccessFlags);
276         dout.writeShort(mNameConstant.getIndex());
277         dout.writeShort(mDescriptorConstant.getIndex());
278         
279         int size = mAttributes.size();
280         dout.writeShort(size);
281         for (int i=0; i<size; i++) {
282             Attribute attr = (Attribute)mAttributes.get(i);
283             attr.writeTo(dout);
284         }
285     }
286
287     public String JavaDoc toString() {
288         String JavaDoc modStr = Modifier.toString(mAccessFlags);
289         if (modStr.length() == 0) {
290             return String.valueOf(mType) + ' ' + getName();
291         }
292         else {
293             return modStr + ' ' + mType + ' ' + getName();
294         }
295     }
296
297     static FieldInfo readFrom(ClassFile parent,
298                               DataInput din,
299                               AttributeFactory attrFactory)
300         throws IOException
301     {
302         ConstantPool cp = parent.getConstantPool();
303
304         int flags = din.readUnsignedShort();
305         int index = din.readUnsignedShort();
306         ConstantUTFInfo nameConstant = (ConstantUTFInfo)cp.getConstant(index);
307         index = din.readUnsignedShort();
308         ConstantUTFInfo descConstant = (ConstantUTFInfo)cp.getConstant(index);
309
310         FieldInfo info = new FieldInfo(parent, flags,
311                                        nameConstant, descConstant);
312
313         // Read attributes.
314
int size = din.readUnsignedShort();
315         for (int i=0; i<size; i++) {
316             info.addAttribute(Attribute.readFrom(cp, din, attrFactory));
317         }
318
319         return info;
320     }
321 }
322
Popular Tags