KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > bytecode > FieldInfo


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist.bytecode;
17
18 import java.io.DataInputStream JavaDoc;
19 import java.io.DataOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.LinkedList JavaDoc;
23
24 /**
25  * <code>field_info</code> structure.
26  *
27  * @see javassist.CtField#getFieldInfo()
28  */

29 public final class FieldInfo {
30     ConstPool constPool;
31     int accessFlags;
32     int name;
33     int descriptor;
34     LinkedList JavaDoc attribute; // may be null.
35

36     private FieldInfo(ConstPool cp) {
37         constPool = cp;
38         accessFlags = 0;
39         attribute = null;
40     }
41
42     /**
43      * Constructs a <code>field_info</code> structure.
44      *
45      * @param cp a constant pool table
46      * @param fieldName field name
47      * @param desc field descriptor
48      *
49      * @see Descriptor
50      */

51     public FieldInfo(ConstPool cp, String JavaDoc fieldName, String JavaDoc desc) {
52         this(cp);
53         name = cp.addUtf8Info(fieldName);
54         descriptor = cp.addUtf8Info(desc);
55     }
56
57     FieldInfo(ConstPool cp, DataInputStream JavaDoc in) throws IOException JavaDoc {
58         this(cp);
59         read(in);
60     }
61
62     /**
63      * Copies all constant pool items to a given new constant pool
64      * and replaces the original items with the new ones.
65      * This is used for garbage collecting the items of removed fields
66      * and methods.
67      *
68      * @param cp the destination
69      */

70     void compact(ConstPool cp) {
71         name = cp.addUtf8Info(getName());
72         descriptor = cp.addUtf8Info(getDescriptor());
73         attribute = AttributeInfo.copyAll(attribute, cp);
74         constPool = cp;
75     }
76
77     void prune(ConstPool cp) {
78         int index = getConstantValue();
79         if (index == 0)
80             attribute = null;
81         else {
82             index = constPool.copy(index, cp, null);
83             attribute = new LinkedList JavaDoc();
84             attribute.add(new ConstantAttribute(cp, index));
85         }
86
87         name = cp.addUtf8Info(getName());
88         descriptor = cp.addUtf8Info(getDescriptor());
89         constPool = cp;
90     }
91
92     /**
93      * Returns the constant pool table used
94      * by this <code>field_info</code>.
95      */

96     public ConstPool getConstPool() {
97         return constPool;
98     }
99
100     /**
101      * Returns the field name.
102      */

103     public String JavaDoc getName() {
104         return constPool.getUtf8Info(name);
105     }
106
107     /**
108      * Sets the field name.
109      */

110     public void setName(String JavaDoc newName) {
111         name = constPool.addUtf8Info(newName);
112     }
113
114     /**
115      * Returns the access flags.
116      *
117      * @see AccessFlag
118      */

119     public int getAccessFlags() {
120         return accessFlags;
121     }
122
123     /**
124      * Sets the access flags.
125      *
126      * @see AccessFlag
127      */

128     public void setAccessFlags(int acc) {
129         accessFlags = acc;
130     }
131
132     /**
133      * Returns the field descriptor.
134      *
135      * @see Descriptor
136      */

137     public String JavaDoc getDescriptor() {
138         return constPool.getUtf8Info(descriptor);
139     }
140
141     /**
142      * Sets the field descriptor.
143      *
144      * @see Descriptor
145      */

146     public void setDescriptor(String JavaDoc desc) {
147         if (!desc.equals(getDescriptor()))
148             descriptor = constPool.addUtf8Info(desc);
149     }
150
151     /**
152      * Finds a ConstantValue attribute and returns the index into
153      * the <code>constant_pool</code> table.
154      *
155      * @return 0 if a ConstantValue attribute is not found.
156      */

157     public int getConstantValue() {
158         if ((accessFlags & AccessFlag.STATIC) == 0)
159             return 0;
160
161         ConstantAttribute attr
162             = (ConstantAttribute)getAttribute(ConstantAttribute.tag);
163         if (attr == null)
164             return 0;
165         else
166             return attr.getConstantValue();
167     }
168
169     /**
170      * Returns all the attributes.
171      * A new element can be added to the returned list
172      * and an existing element can be removed from the list.
173      *
174      * @return a list of <code>AttributeInfo</code> objects.
175      * @see AttributeInfo
176      */

177     public List JavaDoc getAttributes() {
178         if (attribute == null)
179             attribute = new LinkedList JavaDoc();
180
181         return attribute;
182     }
183
184     /**
185      * Returns the attribute with the specified name.
186      *
187      * @param name attribute name
188      */

189     public AttributeInfo getAttribute(String JavaDoc name) {
190         return AttributeInfo.lookup(attribute, name);
191     }
192
193     /**
194      * Appends an attribute. If there is already an attribute with
195      * the same name, the new one substitutes for it.
196      */

197     public void addAttribute(AttributeInfo info) {
198         if (attribute == null)
199             attribute = new LinkedList JavaDoc();
200
201         AttributeInfo.remove(attribute, info.getName());
202         attribute.add(info);
203     }
204
205     private void read(DataInputStream JavaDoc in) throws IOException JavaDoc {
206         accessFlags = in.readUnsignedShort();
207         name = in.readUnsignedShort();
208         descriptor = in.readUnsignedShort();
209         int n = in.readUnsignedShort();
210         attribute = new LinkedList JavaDoc();
211         for (int i = 0; i < n; ++i)
212             attribute.add(AttributeInfo.read(constPool, in));
213     }
214
215     void write(DataOutputStream JavaDoc out) throws IOException JavaDoc {
216         out.writeShort(accessFlags);
217         out.writeShort(name);
218         out.writeShort(descriptor);
219         if (attribute == null)
220             out.writeShort(0);
221         else {
222             out.writeShort(attribute.size());
223             AttributeInfo.writeAll(attribute, out);
224         }
225     }
226 }
227
Popular Tags