1. package com.sun.org.apache.bcel.internal.classfile;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import com.sun.org.apache.bcel.internal.Constants;
  56. import java.io.*;
  57. /**
  58. * This class represents the method info structure, i.e., the representation
  59. * for a method in the class. See JVM specification for details.
  60. * A method has access flags, a name, a signature and a number of attributes.
  61. *
  62. * @version $Id: Method.java,v 1.1.1.1 2001/10/29 20:00:02 jvanzyl Exp $
  63. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  64. */
  65. public final class Method extends FieldOrMethod {
  66. /**
  67. * Empty constructor, all attributes have to be defined via `setXXX'
  68. * methods. Use at your own risk.
  69. */
  70. public Method() {}
  71. /**
  72. * Initialize from another object. Note that both objects use the same
  73. * references (shallow copy). Use clone() for a physical copy.
  74. */
  75. public Method(Method c) {
  76. super(c);
  77. }
  78. /**
  79. * Construct object from file stream.
  80. * @param file Input stream
  81. * @throw IOException
  82. * @throw ClassFormatError
  83. */
  84. Method(DataInputStream file, ConstantPool constant_pool)
  85. throws IOException, ClassFormatError
  86. {
  87. super(file, constant_pool);
  88. }
  89. /**
  90. * @param access_flags Access rights of method
  91. * @param name_index Points to field name in constant pool
  92. * @param signature_index Points to encoded signature
  93. * @param attributes Collection of attributes
  94. * @param constant_pool Array of constants
  95. */
  96. public Method(int access_flags, int name_index, int signature_index,
  97. Attribute[] attributes, ConstantPool constant_pool)
  98. {
  99. super(access_flags, name_index, signature_index, attributes, constant_pool);
  100. }
  101. /**
  102. * Called by objects that are traversing the nodes of the tree implicitely
  103. * defined by the contents of a Java class. I.e., the hierarchy of methods,
  104. * fields, attributes, etc. spawns a tree of objects.
  105. *
  106. * @param v Visitor object
  107. */
  108. public void accept(Visitor v) {
  109. v.visitMethod(this);
  110. }
  111. /**
  112. * @return Code attribute of method, if any
  113. */
  114. public final Code getCode() {
  115. for(int i=0; i < attributes_count; i++)
  116. if(attributes[i] instanceof Code)
  117. return (Code)attributes[i];
  118. return null;
  119. }
  120. /**
  121. * @return ExceptionTable attribute of method, if any, i.e., list all
  122. * exceptions the method may throw not exception handlers!
  123. */
  124. public final ExceptionTable getExceptionTable() {
  125. for(int i=0; i < attributes_count; i++)
  126. if(attributes[i] instanceof ExceptionTable)
  127. return (ExceptionTable)attributes[i];
  128. return null;
  129. }
  130. /** @return LocalVariableTable of code attribute if any, i.e. the call is forwarded
  131. * to the Code atribute.
  132. */
  133. public final LocalVariableTable getLocalVariableTable() {
  134. Code code = getCode();
  135. if(code != null)
  136. return code.getLocalVariableTable();
  137. else
  138. return null;
  139. }
  140. /** @return LineNumberTable of code attribute if any, i.e. the call is forwarded
  141. * to the Code atribute.
  142. */
  143. public final LineNumberTable getLineNumberTable() {
  144. Code code = getCode();
  145. if(code != null)
  146. return code.getLineNumberTable();
  147. else
  148. return null;
  149. }
  150. /**
  151. * Return string representation close to declaration format,
  152. * `public static void main(String[] args) throws IOException', e.g.
  153. *
  154. * @return String representation of the method.
  155. */
  156. public final String toString() {
  157. ConstantUtf8 c;
  158. ConstantValue cv;
  159. String name, signature, access; // Short cuts to constant pool
  160. String exceptions;
  161. StringBuffer buf;
  162. Attribute[] attr;
  163. access = Utility.accessToString(access_flags);
  164. // Get name and signature from constant pool
  165. c = (ConstantUtf8)constant_pool.getConstant(signature_index,
  166. Constants.CONSTANT_Utf8);
  167. signature = c.getBytes();
  168. c = (ConstantUtf8)constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
  169. name = c.getBytes();
  170. signature = Utility.methodSignatureToString(signature, name, access, true,
  171. getLocalVariableTable());
  172. buf = new StringBuffer(signature);
  173. for(int i=0; i < attributes_count; i++) {
  174. Attribute a = attributes[i];
  175. if(!((a instanceof Code) || (a instanceof ExceptionTable)))
  176. buf.append(" [" + a.toString() + "]");
  177. }
  178. ExceptionTable e = getExceptionTable();
  179. if(e != null) {
  180. String str = e.toString();
  181. if(!str.equals(""))
  182. buf.append("\n\t\tthrows " + str);
  183. }
  184. return buf.toString();
  185. }
  186. /**
  187. * @return deep copy of this method
  188. */
  189. public final Method copy(ConstantPool constant_pool) {
  190. return (Method)copy_(constant_pool);
  191. }
  192. }