KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > bytecode > FieldInfo


1 /* FieldInfo Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: FieldInfo.java,v 1.10.2.1 2002/05/28 17:34:00 hoenicke Exp $
18  */

19
20 package jode.bytecode;
21 import java.io.DataInputStream JavaDoc;
22 import java.io.DataOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Modifier JavaDoc;
25
26 public class FieldInfo extends BinaryInfo {
27     ClassInfo clazzInfo;
28
29     int modifier;
30     String JavaDoc name;
31     String JavaDoc typeSig;
32
33     Object JavaDoc constant;
34     boolean syntheticFlag;
35     boolean deprecatedFlag;
36
37     public FieldInfo(ClassInfo ci) {
38     this.clazzInfo = ci;
39     }
40
41     public FieldInfo(ClassInfo ci, String JavaDoc name, String JavaDoc typeSig, int modifier) {
42     this.clazzInfo = ci;
43     this.name = name;
44     this.typeSig = typeSig;
45     this.modifier = modifier;
46     }
47
48     protected void readAttribute(String JavaDoc name, int length,
49                  ConstantPool cp,
50                  DataInputStream JavaDoc input,
51                  int howMuch) throws IOException JavaDoc {
52     if ((howMuch & KNOWNATTRIBS) != 0 && name.equals("ConstantValue")) {
53         if (length != 2)
54         throw new ClassFormatException("ConstantValue attribute"
55                            + " has wrong length");
56         int index = input.readUnsignedShort();
57         constant = cp.getConstant(index);
58     } else if (name.equals("Synthetic")) {
59         syntheticFlag = true;
60         if (length != 0)
61         throw new ClassFormatException
62             ("Synthetic attribute has wrong length");
63     } else if (name.equals("Deprecated")) {
64         deprecatedFlag = true;
65         if (length != 0)
66         throw new ClassFormatException
67             ("Deprecated attribute has wrong length");
68     } else
69         super.readAttribute(name, length, cp, input, howMuch);
70     }
71
72     public void read(ConstantPool constantPool,
73                      DataInputStream JavaDoc input, int howMuch) throws IOException JavaDoc {
74     modifier = input.readUnsignedShort();
75     name = constantPool.getUTF8(input.readUnsignedShort());
76     typeSig = constantPool.getUTF8(input.readUnsignedShort());
77         readAttributes(constantPool, input, howMuch);
78     }
79
80     public void reserveSmallConstants(GrowableConstantPool gcp) {
81     }
82
83     public void prepareWriting(GrowableConstantPool gcp) {
84     gcp.putUTF8(name);
85     gcp.putUTF8(typeSig);
86     if (constant != null) {
87         gcp.putUTF8("ConstantValue");
88         if (typeSig.charAt(0) == 'J' || typeSig.charAt(0) == 'D')
89         gcp.putLongConstant(constant);
90         else
91         gcp.putConstant(constant);
92     }
93     if (syntheticFlag)
94         gcp.putUTF8("Synthetic");
95     if (deprecatedFlag)
96         gcp.putUTF8("Deprecated");
97     prepareAttributes(gcp);
98     }
99
100     protected int getKnownAttributeCount() {
101     int count = 0;
102     if (constant != null)
103         count++;
104     if (syntheticFlag)
105         count++;
106     if (deprecatedFlag)
107         count++;
108     return count;
109     }
110
111     public void writeKnownAttributes(GrowableConstantPool gcp,
112                      DataOutputStream JavaDoc output)
113     throws IOException JavaDoc {
114     if (constant != null) {
115         output.writeShort(gcp.putUTF8("ConstantValue"));
116         output.writeInt(2);
117         int index;
118         if (typeSig.charAt(0) == 'J'
119         || typeSig.charAt(0) == 'D')
120         index = gcp.putLongConstant(constant);
121         else
122         index = gcp.putConstant(constant);
123         output.writeShort(index);
124     }
125     if (syntheticFlag) {
126         output.writeShort(gcp.putUTF8("Synthetic"));
127         output.writeInt(0);
128     }
129     if (deprecatedFlag) {
130         output.writeShort(gcp.putUTF8("Deprecated"));
131         output.writeInt(0);
132     }
133     }
134
135     public void write(GrowableConstantPool constantPool,
136               DataOutputStream JavaDoc output) throws IOException JavaDoc {
137     output.writeShort(modifier);
138     output.writeShort(constantPool.putUTF8(name));
139     output.writeShort(constantPool.putUTF8(typeSig));
140         writeAttributes(constantPool, output);
141     }
142
143     public void dropInfo(int howMuch) {
144     if ((howMuch & KNOWNATTRIBS) != 0)
145         constant = null;
146     super.dropInfo(howMuch);
147     }
148
149     public String JavaDoc getName() {
150         return name;
151     }
152
153     public String JavaDoc getType() {
154         return typeSig;
155     }
156
157     public int getModifiers() {
158         return modifier;
159     }
160     
161     public boolean isSynthetic() {
162     return syntheticFlag;
163     }
164
165     public boolean isDeprecated() {
166     return deprecatedFlag;
167     }
168
169     public Object JavaDoc getConstant() {
170     clazzInfo.loadInfo(KNOWNATTRIBS);
171     return constant;
172     }
173
174     public void setName(String JavaDoc newName) {
175         name = newName;
176     }
177
178     public void setType(String JavaDoc newType) {
179         typeSig = newType;
180     }
181
182     public void setModifiers(int newModifier) {
183         modifier = newModifier;
184     }
185
186     public void setSynthetic(boolean flag) {
187     syntheticFlag = flag;
188     }
189
190     public void setDeprecated(boolean flag) {
191     deprecatedFlag = flag;
192     }
193
194     public void setConstant(Object JavaDoc newConstant) {
195     constant = newConstant;
196     }
197
198     public String JavaDoc toString() {
199         return "Field "+Modifier.toString(modifier)+" "+
200             typeSig+" "+name;
201     }
202 }
203
204
Popular Tags