1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import com.sun.org.apache.bcel.internal.Constants;
  25 import com.sun.org.apache.bcel.internal.generic.Type;
  26 import java.io.*;
  27 
  28 /**
  29  * This class represents the method info structure, i.e., the representation
  30  * for a method in the class. See JVM specification for details.
  31  * A method has access flags, a name, a signature and a number of attributes.
  32  *
  33  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  34  */
  35 public final class Method extends FieldOrMethod {
  36   /**
  37    * Empty constructor, all attributes have to be defined via `setXXX'
  38    * methods. Use at your own risk.
  39    */
  40   public Method() {}
  41 
  42   /**
  43    * Initialize from another object. Note that both objects use the same
  44    * references (shallow copy). Use clone() for a physical copy.
  45    */
  46   public Method(Method c) {
  47     super(c);
  48   }
  49 
  50   /**
  51    * Construct object from file stream.
  52    * @param file Input stream
  53    * @throws IOException
  54    * @throws ClassFormatException
  55    */
  56   Method(DataInputStream file, ConstantPool constant_pool)
  57     throws IOException, ClassFormatException
  58   {
  59     super(file, constant_pool);
  60   }
  61 
  62   /**
  63    * @param access_flags Access rights of method
  64    * @param name_index Points to field name in constant pool
  65    * @param signature_index Points to encoded signature
  66    * @param attributes Collection of attributes
  67    * @param constant_pool Array of constants
  68    */
  69   public Method(int access_flags, int name_index, int signature_index,
  70                 Attribute[] attributes, ConstantPool constant_pool)
  71   {
  72     super(access_flags, name_index, signature_index, attributes, constant_pool);
  73   }
  74 
  75   /**
  76    * Called by objects that are traversing the nodes of the tree implicitely
  77    * defined by the contents of a Java class. I.e., the hierarchy of methods,
  78    * fields, attributes, etc. spawns a tree of objects.
  79    *
  80    * @param v Visitor object
  81    */
  82   public void accept(Visitor v) {
  83     v.visitMethod(this);
  84   }
  85 
  86   /**
  87    * @return Code attribute of method, if any
  88    */
  89   public final Code getCode() {
  90     for(int i=0; i < attributes_count; i++)
  91       if(attributes[i] instanceof Code)
  92         return (Code)attributes[i];
  93 
  94     return null;
  95   }
  96 
  97   /**
  98    * @return ExceptionTable attribute of method, if any, i.e., list all
  99    * exceptions the method may throw not exception handlers!
 100    */
 101   public final ExceptionTable getExceptionTable() {
 102     for(int i=0; i < attributes_count; i++)
 103       if(attributes[i] instanceof ExceptionTable)
 104         return (ExceptionTable)attributes[i];
 105 
 106     return null;
 107   }
 108 
 109   /** @return LocalVariableTable of code attribute if any, i.e. the call is forwarded
 110    * to the Code atribute.
 111    */
 112   public final LocalVariableTable getLocalVariableTable() {
 113     Code code = getCode();
 114 
 115     if(code != null)
 116       return code.getLocalVariableTable();
 117     else
 118       return null;
 119   }
 120 
 121   /** @return LineNumberTable of code attribute if any, i.e. the call is forwarded
 122    * to the Code atribute.
 123    */
 124   public final LineNumberTable getLineNumberTable() {
 125     Code code = getCode();
 126 
 127     if(code != null)
 128       return code.getLineNumberTable();
 129     else
 130       return null;
 131   }
 132 
 133   /**
 134    * Return string representation close to declaration format,
 135    * `public static void _main(String[] args) throws IOException', e.g.
 136    *
 137    * @return String representation of the method.
 138    */
 139   public final String toString() {
 140     ConstantUtf8  c;
 141     String        name, signature, access; // Short cuts to constant pool
 142     StringBuffer  buf;
 143 
 144     access = Utility.accessToString(access_flags);
 145 
 146     // Get name and signature from constant pool
 147     c = (ConstantUtf8)constant_pool.getConstant(signature_index,
 148                                                 Constants.CONSTANT_Utf8);
 149     signature = c.getBytes();
 150 
 151     c = (ConstantUtf8)constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
 152     name = c.getBytes();
 153 
 154     signature = Utility.methodSignatureToString(signature, name, access, true,
 155                                                 getLocalVariableTable());
 156     buf = new StringBuffer(signature);
 157 
 158     for(int i=0; i < attributes_count; i++) {
 159       Attribute a = attributes[i];
 160 
 161       if(!((a instanceof Code) || (a instanceof ExceptionTable)))
 162         buf.append(" [" + a.toString() + "]");
 163     }
 164 
 165     ExceptionTable e = getExceptionTable();
 166     if(e != null) {
 167       String str = e.toString();
 168       if(!str.equals(""))
 169         buf.append("\n\t\tthrows " + str);
 170     }
 171 
 172     return buf.toString();
 173   }
 174 
 175   /**
 176    * @return deep copy of this method
 177    */
 178   public final Method copy(ConstantPool constant_pool) {
 179     return (Method)copy_(constant_pool);
 180   }
 181 
 182   /**
 183    * @return return type of method
 184    */
 185   public Type getReturnType() {
 186     return Type.getReturnType(getSignature());
 187   }
 188 
 189   /**
 190    * @return array of method argument types
 191    */
 192   public Type[] getArgumentTypes() {
 193     return Type.getArgumentTypes(getSignature());
 194   }
 195 }