Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Method.java

00001 package de.fub.bytecode.classfile;
00002 
00003 import java.io.*;
00004 
00005 /**
00006  * This class represents the method info structure, i.e. the representation 
00007  * for a method in the class. See JVM specification for details.
00008  * A method has access flags, a name, a signature and a number of attributes.
00009  *
00010  * @version $Id: Method.java,v 1.1.1.1 2002/01/24 03:44:00 pserver Exp $
00011  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00012  */
00013 public final class Method extends FieldOrMethod {
00014   /**
00015    * Empty constructor, all attributes have to be defined via `setXXX'
00016    * methods. Use at your own risk.
00017    */
00018   public Method() {}  
00019   /**
00020    * @param access_flags Access rights of method
00021    * @param name_index Points to field name in constant pool
00022    * @param signature_index Points to encoded signature
00023    * @param attributes Collection of attributes
00024    * @param constant_pool Array of constants
00025    */
00026   public Method(int access_flags, int name_index, int signature_index,
00027         Attribute[] attributes, ConstantPool constant_pool)
00028   {
00029     super(access_flags, name_index, signature_index, attributes, constant_pool);
00030   }  
00031   /**
00032    * Initialize from another object. Note that both objects use the same
00033    * references (shallow copy). Use clone() for a physical copy.
00034    */
00035   public Method(Method c) {
00036     super(c);
00037   }  
00038   /**
00039    * Construct object from file stream.
00040    * @param file Input stream
00041    * @throw IOException
00042    * @throw ClassFormatError
00043    */
00044   Method(DataInputStream file, ConstantPool constant_pool)
00045        throws IOException, ClassFormatError
00046   {
00047     super(file, constant_pool);
00048   }  
00049   /**
00050    * Called by objects that are traversing the nodes of the tree implicitely
00051    * defined by the contents of a Java class. I.e., the hierarchy of methods,
00052    * fields, attributes, etc. spawns a tree of objects.
00053    *
00054    * @param v Visitor object
00055    */
00056   public void accept(Visitor v) {
00057     v.visitMethod(this);
00058   }  
00059   /**
00060    * @return deep copy of this method
00061    */
00062   public final Method copy(ConstantPool constant_pool) {
00063     return (Method)copy_(constant_pool);
00064   }  
00065   /**
00066    * @return Code attribute of method, if any
00067    */   
00068   public final Code getCode() {
00069     for(int i=0; i < attributes_count; i++)
00070       if(attributes[i] instanceof Code)
00071     return (Code)attributes[i];
00072 
00073     return null;
00074   }  
00075   /**
00076    * @return ExceptionTable attribute of method, if any, i.e. list all
00077    * exceptions the method may throw not exception handlers!
00078    */
00079   public final ExceptionTable getExceptionTable() {
00080     for(int i=0; i < attributes_count; i++)
00081       if(attributes[i] instanceof ExceptionTable)
00082     return (ExceptionTable)attributes[i];
00083 
00084     return null;
00085   }  
00086   /**
00087    * Return string representation close to declaration format,
00088    * `public static int main(String[]) throws IOException', e.g.
00089    *
00090    * @return String representation of the method.
00091    */
00092   public final String toString() {
00093     ConstantUtf8  c;
00094     ConstantValue cv;
00095     String        name, signature, access; // Short cuts to constant pool
00096     String        exceptions;
00097     StringBuffer  buf;
00098     Attribute[]   attr;
00099 
00100     access = Utility.accessToString(access_flags);
00101 
00102     // Get name and signature from constant pool
00103     c = (ConstantUtf8)constant_pool.getConstant(signature_index, 
00104                         CONSTANT_Utf8);
00105     signature = c.getBytes();
00106 
00107     c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8);
00108     name = c.getBytes();
00109 
00110     signature = Utility.methodSignatureToString(signature, name, access);
00111 
00112     buf = new StringBuffer(signature);
00113 
00114     for(int i=0; i < attributes_count; i++) {
00115       Attribute a = attributes[i];
00116 
00117       if(!((a instanceof Code) || (a instanceof ExceptionTable)))
00118     buf.append(" <" + a.toString() + ">");
00119     }
00120 
00121     ExceptionTable e = getExceptionTable();
00122     if(e != null) {
00123       String str = e.toString();
00124       if(!str.equals(""))
00125     buf.append("\n\t\tthrows " + str);
00126     }
00127  
00128     return buf.toString();
00129   }  
00130 }

Generated at Thu Feb 7 06:50:18 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001