1 cananian 1.1.2.2 // FieldInfo.java, created Mon Jan 18 22:44:38 1999 by cananian 2 cananian 1.1.2.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu> 3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 cananian 1.1.2.1 package harpoon.IR.RawClass; 5 cananian 1.1.2.1 6 cananian 1.1.2.1 /** 7 cananian 1.1.2.1 * Each field is described by a variable-length 8 cananian 1.1.2.1 * <code>field_info</code> structure. 9 cananian 1.1.2.1 * 10 cananian 1.1.2.1 * @author C. Scott Ananian <cananian@alumni.princeton.edu> 11 cananian 1.2 * @version $Id: FieldInfo.java,v 1.2 2002/02/25 21:05:27 cananian Exp $ 12 cananian 1.1.2.1 * @see "The Java Virtual Machine Specification, section 4.5" 13 cananian 1.1.2.1 * @see ClassFile 14 cananian 1.1.2.1 * @see harpoon.ClassFile.HField 15 cananian 1.1.2.1 */ 16 cananian 1.1.2.1 public class FieldInfo { 17 cananian 1.1.2.1 /** ClassFile in which this field information is found. */ 18 cananian 1.1.2.1 protected ClassFile parent; 19 cananian 1.1.2.1 20 cananian 1.1.2.1 /** Access permissions and properties of the field. */ 21 cananian 1.1.2.1 public AccessFlags access_flags; 22 cananian 1.1.2.1 23 cananian 1.1.2.1 /** The value of the <code>name_index</code> item must be a valid 24 cananian 1.1.2.1 index into the <code>constant_pool</code> table. The 25 cananian 1.1.2.1 <code>constant_pool</code> entry at that index must be a 26 cananian 1.1.2.1 <code>CONSTANT_Utf8_info</code> structure which must represent a 27 cananian 1.1.2.1 valid Java field name stored as a simple (not fully qualified) 28 cananian 1.1.2.1 name, that is, as a Java identifier. */ 29 cananian 1.1.2.1 public int name_index; 30 cananian 1.1.2.1 /** The value of the <code>descriptor_index</code> item must be a 31 cananian 1.1.2.1 valid index into the <code>constant_pool</code> table. The 32 cananian 1.1.2.1 <code>constant_pool</code> entry at that index must be a 33 cananian 1.1.2.1 <code>CONSTANT_Utf8_info</code> structure which must represent a 34 cananian 1.1.2.1 valid Java field descriptor. */ 35 cananian 1.1.2.1 public int descriptor_index; 36 cananian 1.1.2.1 37 cananian 1.1.2.1 /** A field can have any number of attributes associated with 38 cananian 1.1.2.1 it. <p> The only attributed defined for the 39 cananian 1.1.2.1 <code>attributes</code> table of a <code>field_info</code> 40 cananian 1.1.2.1 structure by this specification is the 41 cananian 1.1.2.1 <code>ConstantValue</code> attribute. */ 42 cananian 1.1.2.1 public Attribute attributes[]; 43 cananian 1.1.2.1 44 cananian 1.1.2.1 /** Read a single FieldInfo item from an input class bytecode file. */ 45 cananian 1.1.2.1 public void read(ClassFile p, ClassDataInputStream in) 46 cananian 1.1.2.1 throws java.io.IOException { 47 cananian 1.1.2.1 this.parent = p; 48 cananian 1.1.2.1 49 cananian 1.1.2.1 access_flags = new AccessFlags(in); 50 cananian 1.1.2.1 51 cananian 1.1.2.1 name_index = in.read_u2(); 52 cananian 1.1.2.1 descriptor_index = in.read_u2(); 53 cananian 1.1.2.1 54 cananian 1.1.2.1 int attributes_count = in.read_u2(); 55 cananian 1.1.2.1 attributes = new Attribute[attributes_count]; 56 cananian 1.1.2.1 for (int i=0; i<attributes_count; i++) 57 cananian 1.1.2.1 attributes[i] = Attribute.read(p, in); 58 cananian 1.1.2.1 } 59 cananian 1.1.2.1 60 cananian 1.1.2.1 /** Constructor. */ 61 cananian 1.1.2.1 FieldInfo(ClassFile p, ClassDataInputStream in) 62 cananian 1.1.2.1 throws java.io.IOException { 63 cananian 1.1.2.1 read(p, in); 64 cananian 1.1.2.1 } 65 cananian 1.1.2.1 /** Constructor. */ 66 cananian 1.1.2.1 public FieldInfo(ClassFile parent, AccessFlags access_flags, 67 cananian 1.1.2.1 int name_index, int descriptor_index, 68 cananian 1.1.2.1 Attribute attributes[]) { 69 cananian 1.1.2.1 this.parent = parent; 70 cananian 1.1.2.1 this.access_flags = access_flags; 71 cananian 1.1.2.1 this.name_index = name_index; 72 cananian 1.1.2.1 this.descriptor_index = descriptor_index; 73 cananian 1.1.2.1 this.attributes = attributes; 74 cananian 1.1.2.1 } 75 cananian 1.1.2.1 76 cananian 1.1.2.1 /** Writes a FieldInfo item out to a class bytecode file. */ 77 cananian 1.1.2.1 public void write(ClassDataOutputStream out) throws java.io.IOException { 78 cananian 1.1.2.1 access_flags.write(out); 79 cananian 1.1.2.1 out.write_u2(name_index); 80 cananian 1.1.2.1 out.write_u2(descriptor_index); 81 cananian 1.1.2.1 82 cananian 1.1.2.1 if (attributes.length > 0xFFFF) 83 cananian 1.1.2.1 throw new ClassDataException("Attributes list too long: " + 84 cananian 1.1.2.1 attributes.length); 85 cananian 1.1.2.1 out.write_u2(attributes.length); 86 cananian 1.1.2.1 for (int i=0; i<attributes.length; i++) 87 cananian 1.1.2.1 attributes[i].write(out); 88 cananian 1.1.2.1 } 89 cananian 1.1.2.1 90 cananian 1.1.2.1 // convenience 91 cananian 1.1.2.1 public ConstantUtf8 name_index() 92 cananian 1.1.2.1 { return (ConstantUtf8) parent.constant_pool[name_index]; } 93 cananian 1.1.2.1 public ConstantUtf8 descriptor_index() 94 cananian 1.1.2.1 { return (ConstantUtf8) parent.constant_pool[descriptor_index]; } 95 cananian 1.1.2.1 96 cananian 1.1.2.1 public String name() { return name_index().val; } 97 cananian 1.1.2.1 public String descriptor() { return descriptor_index().val; } 98 cananian 1.1.2.1 99 cananian 1.1.2.1 /** Pretty-print this field_info structure. */ 100 cananian 1.1.2.1 public void print(java.io.PrintWriter pw, int indent) { 101 cananian 1.1.2.1 int in=indent; 102 cananian 1.1.2.1 ClassFile.indent(pw, in, "Access Flags: " + access_flags); 103 cananian 1.1.2.1 ClassFile.indent(pw, in, "Name: " + name() + " {"+name_index+"}"); 104 cananian 1.1.2.1 ClassFile.indent(pw, in, "Descriptor: " + 105 cananian 1.1.2.1 descriptor() + " {"+descriptor_index+"}"); 106 cananian 1.1.2.1 ClassFile.indent(pw, in, "Attributes ["+attributes.length+"]:"); 107 cananian 1.1.2.1 for (int i=0; i<attributes.length; i++) { 108 cananian 1.1.2.1 ClassFile.indent(pw, in+1, "#"+i+":"); 109 cananian 1.1.2.1 attributes[i].print(pw, in+2); 110 cananian 1.1.2.1 } 111 cananian 1.1.2.1 } 112 cananian 1.2 }