001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.javap;
027:
028: import java.util.*;
029: import java.io.*;
030:
031: /**
032: * Strores field data informastion.
033: *
034: * @author Sucheta Dambalkar (Adopted code from jdis)
035: */
036:
037: public class FieldData implements RuntimeConstants {
038:
039: ClassData cls;
040: int access;
041: int name_index;
042: int descriptor_index;
043: int attributes_count;
044: int value_cpx = 0;
045: boolean isSynthetic = false;
046: boolean isDeprecated = false;
047: Vector attrs;
048:
049: public FieldData(ClassData cls) {
050: this .cls = cls;
051: }
052:
053: /**
054: * Read and store field info.
055: */
056: public void read(DataInputStream in) throws IOException {
057: access = in.readUnsignedShort();
058: name_index = in.readUnsignedShort();
059: descriptor_index = in.readUnsignedShort();
060: // Read the attributes
061: int attributes_count = in.readUnsignedShort();
062: attrs = new Vector(attributes_count);
063: for (int i = 0; i < attributes_count; i++) {
064: int attr_name_index = in.readUnsignedShort();
065: if (cls.getTag(attr_name_index) != CONSTANT_UTF8)
066: continue;
067: String attr_name = cls.getString(attr_name_index);
068: if (attr_name.equals("ConstantValue")) {
069: if (in.readInt() != 2)
070: throw new ClassFormatError(
071: "invalid ConstantValue attr length");
072: value_cpx = in.readUnsignedShort();
073: AttrData attr = new AttrData(cls);
074: attr.read(attr_name_index);
075: attrs.addElement(attr);
076: } else if (attr_name.equals("Synthetic")) {
077: if (in.readInt() != 0)
078: throw new ClassFormatError(
079: "invalid Synthetic attr length");
080: isSynthetic = true;
081: AttrData attr = new AttrData(cls);
082: attr.read(attr_name_index);
083: attrs.addElement(attr);
084: } else if (attr_name.equals("Deprecated")) {
085: if (in.readInt() != 0)
086: throw new ClassFormatError(
087: "invalid Synthetic attr length");
088: isDeprecated = true;
089: AttrData attr = new AttrData(cls);
090: attr.read(attr_name_index);
091: attrs.addElement(attr);
092: } else {
093: AttrData attr = new AttrData(cls);
094: attr.read(attr_name_index, in);
095: attrs.addElement(attr);
096: }
097: }
098:
099: } // end read
100:
101: /**
102: * Returns access of a field.
103: */
104: public String[] getAccess() {
105: Vector v = new Vector();
106: if ((access & ACC_PUBLIC) != 0)
107: v.addElement("public");
108: if ((access & ACC_PRIVATE) != 0)
109: v.addElement("private");
110: if ((access & ACC_PROTECTED) != 0)
111: v.addElement("protected");
112: if ((access & ACC_STATIC) != 0)
113: v.addElement("static");
114: if ((access & ACC_FINAL) != 0)
115: v.addElement("final");
116: if ((access & ACC_VOLATILE) != 0)
117: v.addElement("volatile");
118: if ((access & ACC_TRANSIENT) != 0)
119: v.addElement("transient");
120: String[] accflags = new String[v.size()];
121: v.copyInto(accflags);
122: return accflags;
123: }
124:
125: /**
126: * Returns name of a field.
127: */
128: public String getName() {
129: return cls.getStringValue(name_index);
130: }
131:
132: /**
133: * Returns internal signature of a field
134: */
135: public String getInternalSig() {
136: return cls.getStringValue(descriptor_index);
137: }
138:
139: /**
140: * Returns java type signature of a field.
141: */
142: public String getType() {
143: return new TypeSignature(getInternalSig()).getFieldType();
144: }
145:
146: /**
147: * Returns true if field is synthetic.
148: */
149: public boolean isSynthetic() {
150: return isSynthetic;
151: }
152:
153: /**
154: * Returns true if field is deprecated.
155: */
156: public boolean isDeprecated() {
157: return isDeprecated;
158: }
159:
160: /**
161: * Returns index of constant value in cpool.
162: */
163: public int getConstantValueIndex() {
164: return (value_cpx);
165: }
166:
167: /**
168: * Returns list of attributes of field.
169: */
170: public Vector getAttributes() {
171: return attrs;
172: }
173: }
|