1 16 17 package org.cojen.classfile; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.io.DataInput ; 22 import java.io.DataOutput ; 23 import java.io.IOException ; 24 import java.lang.reflect.Modifier ; 25 import org.cojen.classfile.attribute.Annotation; 26 import org.cojen.classfile.attribute.AnnotationsAttr; 27 import org.cojen.classfile.attribute.ConstantValueAttr; 28 import org.cojen.classfile.attribute.DeprecatedAttr; 29 import org.cojen.classfile.attribute.RuntimeInvisibleAnnotationsAttr; 30 import org.cojen.classfile.attribute.RuntimeVisibleAnnotationsAttr; 31 import org.cojen.classfile.attribute.SignatureAttr; 32 import org.cojen.classfile.attribute.SyntheticAttr; 33 import org.cojen.classfile.constant.ConstantUTFInfo; 34 35 42 public class FieldInfo { 43 private ClassFile mParent; 44 private ConstantPool mCp; 45 46 private String mName; 47 private TypeDesc mType; 48 49 private Modifiers mModifiers; 50 51 private ConstantUTFInfo mNameConstant; 52 private ConstantUTFInfo mDescriptorConstant; 53 54 private List mAttributes = new ArrayList (2); 55 56 private ConstantValueAttr mConstant; 57 58 FieldInfo(ClassFile parent, 59 Modifiers modifiers, 60 String name, 61 TypeDesc type) { 62 63 mParent = parent; 64 mCp = parent.getConstantPool(); 65 mName = name; 66 mType = type; 67 68 mModifiers = modifiers; 69 mNameConstant = mCp.addConstantUTF(name); 70 mDescriptorConstant = mCp.addConstantUTF(type.getDescriptor()); 71 } 72 73 private FieldInfo(ClassFile parent, 74 int modifier, 75 ConstantUTFInfo nameConstant, 76 ConstantUTFInfo descConstant) { 77 78 mParent = parent; 79 mCp = parent.getConstantPool(); 80 mName = nameConstant.getValue(); 81 mType = TypeDesc.forDescriptor(descConstant.getValue()); 82 83 mModifiers = Modifiers.getInstance(modifier); 84 mNameConstant = nameConstant; 85 mDescriptorConstant = descConstant; 86 } 87 88 91 public ClassFile getClassFile() { 92 return mParent; 93 } 94 95 98 public String getName() { 99 return mName; 100 } 101 102 105 public TypeDesc getType() { 106 return mType; 107 } 108 109 112 public Modifiers getModifiers() { 113 return mModifiers; 114 } 115 116 public void setModifiers(Modifiers modifiers) { 117 mModifiers = modifiers; 118 } 119 120 123 public ConstantUTFInfo getNameConstant() { 124 return mNameConstant; 125 } 126 127 132 public ConstantUTFInfo getDescriptorConstant() { 133 return mDescriptorConstant; 134 } 135 136 139 public ConstantInfo getConstantValue() { 140 if (mConstant == null) { 141 return null; 142 } else { 143 return mConstant.getConstant(); 144 } 145 } 146 147 public boolean isSynthetic() { 148 for (int i = mAttributes.size(); --i >= 0; ) { 149 Object obj = mAttributes.get(i); 150 if (obj instanceof SyntheticAttr) { 151 return true; 152 } 153 } 154 return false; 155 } 156 157 public boolean isDeprecated() { 158 for (int i = mAttributes.size(); --i >= 0; ) { 159 Object obj = mAttributes.get(i); 160 if (obj instanceof DeprecatedAttr) { 161 return true; 162 } 163 } 164 return false; 165 } 166 167 171 public Annotation[] getRuntimeInvisibleAnnotations() { 172 for (int i = mAttributes.size(); --i >= 0; ) { 173 Object obj = mAttributes.get(i); 174 if (obj instanceof RuntimeInvisibleAnnotationsAttr) { 175 return ((AnnotationsAttr) obj).getAnnotations(); 176 } 177 } 178 return new Annotation[0]; 179 } 180 181 185 public Annotation[] getRuntimeVisibleAnnotations() { 186 for (int i = mAttributes.size(); --i >= 0; ) { 187 Object obj = mAttributes.get(i); 188 if (obj instanceof RuntimeVisibleAnnotationsAttr) { 189 return ((AnnotationsAttr) obj).getAnnotations(); 190 } 191 } 192 return new Annotation[0]; 193 } 194 195 198 public Annotation addRuntimeInvisibleAnnotation(TypeDesc type) { 199 AnnotationsAttr attr = null; 200 for (int i = mAttributes.size(); --i >= 0; ) { 201 Object obj = mAttributes.get(i); 202 if (obj instanceof RuntimeInvisibleAnnotationsAttr) { 203 attr = (AnnotationsAttr) obj; 204 } 205 } 206 if (attr == null) { 207 attr = new RuntimeInvisibleAnnotationsAttr(mCp); 208 addAttribute(attr); 209 } 210 Annotation ann = new Annotation(mCp); 211 ann.setType(type); 212 attr.addAnnotation(ann); 213 return ann; 214 } 215 216 219 public Annotation addRuntimeVisibleAnnotation(TypeDesc type) { 220 AnnotationsAttr attr = null; 221 for (int i = mAttributes.size(); --i >= 0; ) { 222 Object obj = mAttributes.get(i); 223 if (obj instanceof RuntimeVisibleAnnotationsAttr) { 224 attr = (AnnotationsAttr) obj; 225 } 226 } 227 if (attr == null) { 228 attr = new RuntimeVisibleAnnotationsAttr(mCp); 229 addAttribute(attr); 230 } 231 Annotation ann = new Annotation(mCp); 232 ann.setType(type); 233 attr.addAnnotation(ann); 234 return ann; 235 } 236 237 241 public SignatureAttr getSignatureAttr() { 243 for (int i = mAttributes.size(); --i >= 0; ) { 244 Object obj = mAttributes.get(i); 245 if (obj instanceof SignatureAttr) { 246 return (SignatureAttr) obj; 247 } 248 } 249 return null; 250 } 251 252 255 public void setConstantValue(int value) { 256 addAttribute(new ConstantValueAttr(mCp, mCp.addConstantInteger(value))); 257 } 258 259 262 public void setConstantValue(float value) { 263 addAttribute(new ConstantValueAttr(mCp, mCp.addConstantFloat(value))); 264 } 265 266 269 public void setConstantValue(long value) { 270 addAttribute(new ConstantValueAttr(mCp, mCp.addConstantLong(value))); 271 } 272 273 276 public void setConstantValue(double value) { 277 addAttribute(new ConstantValueAttr(mCp, mCp.addConstantDouble(value))); 278 } 279 280 283 public void setConstantValue(String value) { 284 addAttribute(new ConstantValueAttr(mCp, mCp.addConstantString(value))); 285 } 286 287 290 public void markSynthetic() { 291 addAttribute(new SyntheticAttr(mCp)); 292 } 293 294 297 public void markDeprecated() { 298 addAttribute(new DeprecatedAttr(mCp)); 299 } 300 301 public void addAttribute(Attribute attr) { 302 if (attr instanceof ConstantValueAttr) { 303 if (mConstant != null) { 304 mAttributes.remove(mConstant); 305 } 306 mConstant = (ConstantValueAttr)attr; 307 } 308 309 mAttributes.add(attr); 310 } 311 312 public Attribute[] getAttributes() { 313 Attribute[] attrs = new Attribute[mAttributes.size()]; 314 return (Attribute[])mAttributes.toArray(attrs); 315 } 316 317 320 public int getLength() { 321 int length = 8; 322 323 int size = mAttributes.size(); 324 for (int i=0; i<size; i++) { 325 length += ((Attribute)mAttributes.get(i)).getLength(); 326 } 327 328 return length; 329 } 330 331 public void writeTo(DataOutput dout) throws IOException { 332 dout.writeShort(mModifiers.getBitmask()); 333 dout.writeShort(mNameConstant.getIndex()); 334 dout.writeShort(mDescriptorConstant.getIndex()); 335 336 int size = mAttributes.size(); 337 dout.writeShort(size); 338 for (int i=0; i<size; i++) { 339 Attribute attr = (Attribute)mAttributes.get(i); 340 attr.writeTo(dout); 341 } 342 } 343 344 public String toString() { 345 String modStr = mModifiers.toString(); 346 String typeStr; 347 if (modStr.length() == 0) { 348 return mType.getFullName() + ' ' + getName(); 349 } else { 350 return modStr + ' ' + mType.getFullName() + ' ' + getName(); 351 } 352 } 353 354 static FieldInfo readFrom(ClassFile parent, 355 DataInput din, 356 AttributeFactory attrFactory) 357 throws IOException 358 { 359 ConstantPool cp = parent.getConstantPool(); 360 361 int modifier = din.readUnsignedShort(); 362 int index = din.readUnsignedShort(); 363 ConstantUTFInfo nameConstant = (ConstantUTFInfo)cp.getConstant(index); 364 index = din.readUnsignedShort(); 365 ConstantUTFInfo descConstant = (ConstantUTFInfo)cp.getConstant(index); 366 367 FieldInfo info = new FieldInfo(parent, modifier, 368 nameConstant, descConstant); 369 370 int size = din.readUnsignedShort(); 372 for (int i=0; i<size; i++) { 373 info.addAttribute(Attribute.readFrom(cp, din, attrFactory)); 374 } 375 376 return info; 377 } 378 } 379 | Popular Tags |