1 cananian 1.1.2.2 // ConstantFieldref.java, created Mon Jan 18 22:44:37 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 * The <code>CONSTANT_Fieldref</code> structure represents a field. 8 cananian 1.1.2.1 * 9 cananian 1.1.2.1 * @author C. Scott Ananian <cananian@alumni.princeton.edu> 10 cananian 1.2 * @version $Id: ConstantFieldref.java,v 1.2 2002/02/25 21:05:26 cananian Exp $ 11 cananian 1.1.2.1 * @see "The Java Virtual Machine Specification, section 4.4.2" 12 cananian 1.1.2.1 * @see Constant 13 cananian 1.1.2.1 * @see ConstantMethodref 14 cananian 1.1.2.1 * @see ConstantInterfaceMethodref 15 cananian 1.1.2.1 */ 16 cananian 1.1.2.1 public class ConstantFieldref extends Constant { 17 cananian 1.1.2.1 /** The value of the <code>class_index</code> item must be a valid 18 cananian 1.1.2.1 index into the <code>constant_pool</code> table of the 19 cananian 1.1.2.1 <code>parent</code>. The <code>constant_pool</code> entry at 20 cananian 1.1.2.1 that index must be a <code>CONSTANT_Class_info</code> structure 21 cananian 1.1.2.1 representing the class or interface type that contains the 22 cananian 1.1.2.1 declaration of the field. 23 cananian 1.1.2.1 <p> 24 cananian 1.1.2.1 The <code>class_index</code> item of a 25 cananian 1.1.2.1 <code>CONSTANT_Fieldref</code> must be a class type, not an 26 cananian 1.1.2.1 interface type. */ 27 cananian 1.1.2.1 public int class_index; 28 cananian 1.1.2.1 /** The value of the <code>name_and_type_index</code> item must be a 29 cananian 1.1.2.1 valid index into the <code>constant_pool</code> table of the 30 cananian 1.1.2.1 <code>parent</code>. The <code>constant_pool</code> entry at 31 cananian 1.1.2.1 that index must be a <code>CONSTANT_NameAndType_info</code> 32 cananian 1.1.2.1 structure. This <code>constant_pool</code> entry indicates the 33 cananian 1.1.2.1 name and descriptor of the field. */ 34 cananian 1.1.2.1 public int name_and_type_index; 35 cananian 1.1.2.1 36 cananian 1.1.2.1 /** Constructor. */ 37 cananian 1.1.2.1 ConstantFieldref(ClassFile parent, ClassDataInputStream in) 38 cananian 1.1.2.1 throws java.io.IOException { 39 cananian 1.1.2.1 super(parent); 40 cananian 1.1.2.1 class_index = in.read_u2(); 41 cananian 1.1.2.1 name_and_type_index = in.read_u2(); 42 cananian 1.1.2.1 } 43 cananian 1.1.2.1 /** Constructor. */ 44 cananian 1.1.2.1 public ConstantFieldref(ClassFile parent, 45 cananian 1.1.2.1 int class_index, int name_and_type_index) { 46 cananian 1.1.2.1 super(parent); 47 cananian 1.1.2.1 this.class_index = class_index; 48 cananian 1.1.2.1 this.name_and_type_index = name_and_type_index; 49 cananian 1.1.2.1 } 50 cananian 1.1.2.1 51 cananian 1.1.2.1 /** Write to a bytecode file. */ 52 cananian 1.1.2.1 public void write(ClassDataOutputStream out) throws java.io.IOException { 53 cananian 1.1.2.1 out.write_u1(CONSTANT_Fieldref); 54 cananian 1.1.2.1 out.write_u2(class_index); 55 cananian 1.1.2.1 out.write_u2(name_and_type_index); 56 cananian 1.1.2.1 } 57 cananian 1.1.2.1 58 cananian 1.1.2.1 // convenience. 59 cananian 1.1.2.1 public ConstantClass class_index() 60 cananian 1.1.2.1 { return (ConstantClass) parent.constant_pool[class_index]; } 61 cananian 1.1.2.1 public ConstantNameAndType name_and_type_index() 62 cananian 1.1.2.1 { return (ConstantNameAndType) parent.constant_pool[name_and_type_index]; } 63 cananian 1.1.2.1 64 cananian 1.1.2.1 /** Create a human-readable representation of this constant. */ 65 cananian 1.1.2.1 public String toString() { 66 cananian 1.1.2.1 ConstantNameAndType cnt = name_and_type_index(); 67 cananian 1.1.2.1 return "CONSTANT_Fieldref: " + 68 cananian 1.1.2.1 class_index().name() + " {" + class_index+"} " + "(" + 69 cananian 1.1.2.1 cnt.name() + " {"+cnt.name_index+"} " + 70 cananian 1.1.2.1 cnt.descriptor() + " {"+cnt.descriptor_index+"}" + ")" + 71 cananian 1.1.2.1 " {"+name_and_type_index+"}"; 72 cananian 1.1.2.1 } 73 cananian 1.2 }