001: /* FieldInfo Copyright (C) 1998-2002 Jochen Hoenicke.
002: *
003: * This program is free software; you can redistribute it and/or modify
004: * it under the terms of the GNU Lesser General Public License as published by
005: * the Free Software Foundation; either version 2, or (at your option)
006: * any later version.
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program; see the file COPYING.LESSER. If not, write to
015: * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
016: *
017: * $Id: FieldInfo.java,v 1.10.2.1 2002/05/28 17:34:00 hoenicke Exp $
018: */
019:
020: package jode.bytecode;
021:
022: import java.io.DataInputStream;
023: import java.io.DataOutputStream;
024: import java.io.IOException;
025: import java.lang.reflect.Modifier;
026:
027: public class FieldInfo extends BinaryInfo {
028: ClassInfo clazzInfo;
029:
030: int modifier;
031: String name;
032: String typeSig;
033:
034: Object constant;
035: boolean syntheticFlag;
036: boolean deprecatedFlag;
037:
038: public FieldInfo(ClassInfo ci) {
039: this .clazzInfo = ci;
040: }
041:
042: public FieldInfo(ClassInfo ci, String name, String typeSig,
043: int modifier) {
044: this .clazzInfo = ci;
045: this .name = name;
046: this .typeSig = typeSig;
047: this .modifier = modifier;
048: }
049:
050: protected void readAttribute(String name, int length,
051: ConstantPool cp, DataInputStream input, int howMuch)
052: throws IOException {
053: if ((howMuch & KNOWNATTRIBS) != 0
054: && name.equals("ConstantValue")) {
055: if (length != 2)
056: throw new ClassFormatException(
057: "ConstantValue attribute" + " has wrong length");
058: int index = input.readUnsignedShort();
059: constant = cp.getConstant(index);
060: } else if (name.equals("Synthetic")) {
061: syntheticFlag = true;
062: if (length != 0)
063: throw new ClassFormatException(
064: "Synthetic attribute has wrong length");
065: } else if (name.equals("Deprecated")) {
066: deprecatedFlag = true;
067: if (length != 0)
068: throw new ClassFormatException(
069: "Deprecated attribute has wrong length");
070: } else
071: super .readAttribute(name, length, cp, input, howMuch);
072: }
073:
074: public void read(ConstantPool constantPool, DataInputStream input,
075: int howMuch) throws IOException {
076: modifier = input.readUnsignedShort();
077: name = constantPool.getUTF8(input.readUnsignedShort());
078: typeSig = constantPool.getUTF8(input.readUnsignedShort());
079: readAttributes(constantPool, input, howMuch);
080: }
081:
082: public void reserveSmallConstants(GrowableConstantPool gcp) {
083: }
084:
085: public void prepareWriting(GrowableConstantPool gcp) {
086: gcp.putUTF8(name);
087: gcp.putUTF8(typeSig);
088: if (constant != null) {
089: gcp.putUTF8("ConstantValue");
090: if (typeSig.charAt(0) == 'J' || typeSig.charAt(0) == 'D')
091: gcp.putLongConstant(constant);
092: else
093: gcp.putConstant(constant);
094: }
095: if (syntheticFlag)
096: gcp.putUTF8("Synthetic");
097: if (deprecatedFlag)
098: gcp.putUTF8("Deprecated");
099: prepareAttributes(gcp);
100: }
101:
102: protected int getKnownAttributeCount() {
103: int count = 0;
104: if (constant != null)
105: count++;
106: if (syntheticFlag)
107: count++;
108: if (deprecatedFlag)
109: count++;
110: return count;
111: }
112:
113: public void writeKnownAttributes(GrowableConstantPool gcp,
114: DataOutputStream output) throws IOException {
115: if (constant != null) {
116: output.writeShort(gcp.putUTF8("ConstantValue"));
117: output.writeInt(2);
118: int index;
119: if (typeSig.charAt(0) == 'J' || 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 output) throws IOException {
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 getName() {
150: return name;
151: }
152:
153: public String 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 getConstant() {
170: clazzInfo.loadInfo(KNOWNATTRIBS);
171: return constant;
172: }
173:
174: public void setName(String newName) {
175: name = newName;
176: }
177:
178: public void setType(String 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 newConstant) {
195: constant = newConstant;
196: }
197:
198: public String toString() {
199: return "Field " + Modifier.toString(modifier) + " " + typeSig
200: + " " + name;
201: }
202: }
|