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