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

Method.java

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

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