1 52 53 package com.go.trove.classfile; 54 55 import java.util.*; 56 import java.io.*; 57 import java.lang.reflect.Modifier ; 58 59 68 public class FieldInfo { 69 private ClassFile mParent; 70 private ConstantPool mCp; 71 72 private String mName; 73 private TypeDescriptor mType; 74 75 private int mAccessFlags; 76 77 private ConstantUTFInfo mNameConstant; 78 private ConstantUTFInfo mDescriptorConstant; 79 80 private List mAttributes = new ArrayList(2); 81 82 private ConstantValueAttr mConstant; 83 84 FieldInfo(ClassFile parent, 85 AccessFlags flags, 86 String name, 87 TypeDescriptor type) { 88 89 mParent = parent; 90 mCp = parent.getConstantPool(); 91 mName = name; 92 mType = type; 93 94 mAccessFlags = flags.getModifier(); 95 mNameConstant = ConstantUTFInfo.make(mCp, name); 96 mDescriptorConstant = ConstantUTFInfo.make(mCp, type.toString()); 97 } 98 99 private FieldInfo(ClassFile parent, 100 int flags, 101 ConstantUTFInfo nameConstant, 102 ConstantUTFInfo descConstant) { 103 104 mParent = parent; 105 mCp = parent.getConstantPool(); 106 mName = nameConstant.getValue(); 107 mType = TypeDescriptor.parseTypeDesc(descConstant.getValue()); 108 109 mAccessFlags = flags; 110 mNameConstant = nameConstant; 111 mDescriptorConstant = descConstant; 112 } 113 114 117 public ClassFile getClassFile() { 118 return mParent; 119 } 120 121 124 public String getName() { 125 return mName; 126 } 127 128 131 public TypeDescriptor getType() { 132 return mType; 133 } 134 135 138 public AccessFlags getAccessFlags() { 139 return new AccessFlags(mAccessFlags); 140 } 141 142 145 public ConstantUTFInfo getNameConstant() { 146 return mNameConstant; 147 } 148 149 154 public ConstantUTFInfo getDescriptorConstant() { 155 return mDescriptorConstant; 156 } 157 158 161 public ConstantInfo getConstantValue() { 162 if (mConstant == null) { 163 return null; 164 } 165 else { 166 return mConstant.getConstant(); 167 } 168 } 169 170 public boolean isSynthetic() { 171 for (int i = mAttributes.size(); --i >= 0; ) { 172 Object obj = mAttributes.get(i); 173 if (obj instanceof SyntheticAttr) { 174 return true; 175 } 176 } 177 return false; 178 } 179 180 public boolean isDeprecated() { 181 for (int i = mAttributes.size(); --i >= 0; ) { 182 Object obj = mAttributes.get(i); 183 if (obj instanceof DeprecatedAttr) { 184 return true; 185 } 186 } 187 return false; 188 } 189 190 193 public void setConstantValue(int value) { 194 addAttribute(new ConstantValueAttr 195 (mCp, ConstantIntegerInfo.make(mCp, value))); 196 } 197 198 201 public void setConstantValue(float value) { 202 addAttribute(new ConstantValueAttr 203 (mCp, ConstantFloatInfo.make(mCp, value))); 204 } 205 206 209 public void setConstantValue(long value) { 210 addAttribute(new ConstantValueAttr 211 (mCp, ConstantLongInfo.make(mCp, value))); 212 } 213 214 217 public void setConstantValue(double value) { 218 addAttribute(new ConstantValueAttr 219 (mCp, ConstantDoubleInfo.make(mCp, value))); 220 } 221 222 225 public void setConstantValue(String value) { 226 addAttribute(new ConstantValueAttr 227 (mCp, ConstantStringInfo.make(mCp, value))); 228 } 229 230 233 public void markSynthetic() { 234 addAttribute(new SyntheticAttr(mCp)); 235 } 236 237 240 public void markDeprecated() { 241 addAttribute(new DeprecatedAttr(mCp)); 242 } 243 244 public void addAttribute(Attribute attr) { 245 if (attr instanceof ConstantValueAttr) { 246 if (mConstant != null) { 247 mAttributes.remove(mConstant); 248 } 249 mConstant = (ConstantValueAttr)attr; 250 } 251 252 mAttributes.add(attr); 253 } 254 255 public Attribute[] getAttributes() { 256 Attribute[] attrs = new Attribute[mAttributes.size()]; 257 return (Attribute[])mAttributes.toArray(attrs); 258 } 259 260 263 public int getLength() { 264 int length = 8; 265 266 int size = mAttributes.size(); 267 for (int i=0; i<size; i++) { 268 length += ((Attribute)mAttributes.get(i)).getLength(); 269 } 270 271 return length; 272 } 273 274 public void writeTo(DataOutput dout) throws IOException { 275 dout.writeShort(mAccessFlags); 276 dout.writeShort(mNameConstant.getIndex()); 277 dout.writeShort(mDescriptorConstant.getIndex()); 278 279 int size = mAttributes.size(); 280 dout.writeShort(size); 281 for (int i=0; i<size; i++) { 282 Attribute attr = (Attribute)mAttributes.get(i); 283 attr.writeTo(dout); 284 } 285 } 286 287 public String toString() { 288 String modStr = Modifier.toString(mAccessFlags); 289 if (modStr.length() == 0) { 290 return String.valueOf(mType) + ' ' + getName(); 291 } 292 else { 293 return modStr + ' ' + mType + ' ' + getName(); 294 } 295 } 296 297 static FieldInfo readFrom(ClassFile parent, 298 DataInput din, 299 AttributeFactory attrFactory) 300 throws IOException 301 { 302 ConstantPool cp = parent.getConstantPool(); 303 304 int flags = din.readUnsignedShort(); 305 int index = din.readUnsignedShort(); 306 ConstantUTFInfo nameConstant = (ConstantUTFInfo)cp.getConstant(index); 307 index = din.readUnsignedShort(); 308 ConstantUTFInfo descConstant = (ConstantUTFInfo)cp.getConstant(index); 309 310 FieldInfo info = new FieldInfo(parent, flags, 311 nameConstant, descConstant); 312 313 int size = din.readUnsignedShort(); 315 for (int i=0; i<size; i++) { 316 info.addAttribute(Attribute.readFrom(cp, din, attrFactory)); 317 } 318 319 return info; 320 } 321 } 322 | Popular Tags |