1 cananian 1.1.2.2 // ConstantMethodref.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_Methodref</code> structure represents a method. 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: ConstantMethodref.java,v 1.2 2002/02/25 21:05:27 cananian Exp $ 11 cananian 1.1.2.1 * @see "The Java Virtual Machine Specifications, section 4.4.2" 12 cananian 1.1.2.1 * @see Constant 13 cananian 1.1.2.1 * @see ConstantFieldref 14 cananian 1.1.2.1 * @see ConstantInterfaceMethodref 15 cananian 1.1.2.1 */ 16 cananian 1.1.2.1 public class ConstantMethodref 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 method. 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_Methodref</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 method. 34 cananian 1.1.2.1 <p> 35 cananian 1.1.2.1 If the name of the method of a 36 cananian 1.1.2.1 <code>CONSTANT_Methodref_info</code> begins with a '<' 37 cananian 1.1.2.1 ('\u003c'), then the name must be one of the special internal 38 cananian 1.1.2.1 methods, either <init> or <clinit>. In this case, 39 cananian 1.1.2.1 the method must return no value. */ 40 cananian 1.1.2.1 public int name_and_type_index; 41 cananian 1.1.2.1 42 cananian 1.1.2.1 /** Constructor. */ 43 cananian 1.1.2.1 ConstantMethodref(ClassFile parent, ClassDataInputStream in) 44 cananian 1.1.2.1 throws java.io.IOException { 45 cananian 1.1.2.1 super(parent); 46 cananian 1.1.2.1 class_index = in.read_u2(); 47 cananian 1.1.2.1 name_and_type_index = in.read_u2(); 48 cananian 1.1.2.1 } 49 cananian 1.1.2.1 /** Constructor. */ 50 cananian 1.1.2.1 public ConstantMethodref(ClassFile parent, 51 cananian 1.1.2.1 int class_index, int name_and_type_index) { 52 cananian 1.1.2.1 super(parent); 53 cananian 1.1.2.1 this.class_index = class_index; 54 cananian 1.1.2.1 this.name_and_type_index = name_and_type_index; 55 cananian 1.1.2.1 } 56 cananian 1.1.2.1 57 cananian 1.1.2.1 /** Write to a bytecode file. */ 58 cananian 1.1.2.1 public void write(ClassDataOutputStream out) throws java.io.IOException { 59 cananian 1.1.2.1 out.write_u1(CONSTANT_Methodref); 60 cananian 1.1.2.1 out.write_u2(class_index); 61 cananian 1.1.2.1 out.write_u2(name_and_type_index); 62 cananian 1.1.2.1 } 63 cananian 1.1.2.1 64 cananian 1.1.2.1 // convenience. 65 cananian 1.1.2.1 public ConstantClass class_index() 66 cananian 1.1.2.1 { return (ConstantClass) parent.constant_pool[class_index]; } 67 cananian 1.1.2.1 public ConstantNameAndType name_and_type_index() 68 cananian 1.1.2.1 { return (ConstantNameAndType) parent.constant_pool[name_and_type_index]; } 69 cananian 1.1.2.1 70 cananian 1.1.2.1 /** Create a human-readable representation of this constant. */ 71 cananian 1.1.2.1 public String toString() { 72 cananian 1.1.2.1 ConstantNameAndType cnt = name_and_type_index(); 73 cananian 1.1.2.1 return "CONSTANT_Methodref: " + 74 cananian 1.1.2.1 class_index().name() + " {" + class_index+"} " + "(" + 75 cananian 1.1.2.1 cnt.name() + " {"+cnt.name_index+"} " + 76 cananian 1.1.2.1 cnt.descriptor() + " {"+cnt.descriptor_index+"}" + ")" + 77 cananian 1.1.2.1 " {"+name_and_type_index+"}"; 78 cananian 1.1.2.1 } 79 cananian 1.2 }