001 /*
002 * Copyright 2002-2008 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
027 package sun.tools.javap;
028
029 import java.util.*;
030 import java.io.*;
031
032 /**
033 * Strores field data informastion.
034 *
035 * @author Sucheta Dambalkar (Adopted code from jdis)
036 */
037
038 public class FieldData implements RuntimeConstants {
039
040 ClassData cls;
041 int access;
042 int name_index;
043 int descriptor_index;
044 int attributes_count;
045 int value_cpx=0;
046 boolean isSynthetic=false;
047 boolean isDeprecated=false;
048 Vector<AttrData> attrs;
049
050 public FieldData(ClassData cls){
051 this.cls=cls;
052 }
053
054 /**
055 * Read and store field info.
056 */
057 public void read(DataInputStream in) throws IOException {
058 access = in.readUnsignedShort();
059 name_index = in.readUnsignedShort();
060 descriptor_index = in.readUnsignedShort();
061 // Read the attributes
062 int attributes_count = in.readUnsignedShort();
063 attrs=new Vector<AttrData>(attributes_count);
064 for (int i = 0; i < attributes_count; i++) {
065 int attr_name_index=in.readUnsignedShort();
066 if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) 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("invalid ConstantValue attr length");
071 value_cpx=in.readUnsignedShort();
072 AttrData attr=new AttrData(cls);
073 attr.read(attr_name_index);
074 attrs.addElement(attr);
075 } else if (attr_name.equals("Synthetic")){
076 if (in.readInt()!=0)
077 throw new ClassFormatError("invalid Synthetic attr length");
078 isSynthetic=true;
079 AttrData attr=new AttrData(cls);
080 attr.read(attr_name_index);
081 attrs.addElement(attr);
082 } else if (attr_name.equals("Deprecated")){
083 if (in.readInt()!=0)
084 throw new ClassFormatError("invalid Synthetic attr length");
085 isDeprecated = true;
086 AttrData attr=new AttrData(cls);
087 attr.read(attr_name_index);
088 attrs.addElement(attr);
089 } else {
090 AttrData attr=new AttrData(cls);
091 attr.read(attr_name_index, in);
092 attrs.addElement(attr);
093 }
094 }
095
096 } // end read
097
098 /**
099 * Returns access of a field.
100 */
101 public String[] getAccess(){
102 Vector<String> v = new Vector<String>();
103 if ((access & ACC_PUBLIC) !=0) v.addElement("public");
104 if ((access & ACC_PRIVATE) !=0) v.addElement("private");
105 if ((access & ACC_PROTECTED) !=0) v.addElement("protected");
106 if ((access & ACC_STATIC) !=0) v.addElement("static");
107 if ((access & ACC_FINAL) !=0) v.addElement("final");
108 if ((access & ACC_VOLATILE) !=0) v.addElement("volatile");
109 if ((access & ACC_TRANSIENT) !=0) v.addElement("transient");
110 String[] accflags = new String[v.size()];
111 v.copyInto(accflags);
112 return accflags;
113 }
114
115 /**
116 * Returns name of a field.
117 */
118 public String getName(){
119 return cls.getStringValue(name_index);
120 }
121
122 /**
123 * Returns internal signature of a field
124 */
125 public String getInternalSig(){
126 return cls.getStringValue(descriptor_index);
127 }
128
129 /**
130 * Returns java type signature of a field.
131 */
132 public String getType(){
133 return new TypeSignature(getInternalSig()).getFieldType();
134 }
135
136 /**
137 * Returns true if field is synthetic.
138 */
139 public boolean isSynthetic(){
140 return isSynthetic;
141 }
142
143 /**
144 * Returns true if field is deprecated.
145 */
146 public boolean isDeprecated(){
147 return isDeprecated;
148 }
149
150 /**
151 * Returns index of constant value in cpool.
152 */
153 public int getConstantValueIndex(){
154 return (value_cpx);
155 }
156
157 /**
158 * Returns list of attributes of field.
159 */
160 public Vector<?> getAttributes(){
161 return attrs;
162 }
163 }