KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > classfile > Method


1 package com.sun.org.apache.bcel.internal.classfile;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56 import com.sun.org.apache.bcel.internal.Constants;
57 import java.io.*;
58
59 /**
60  * This class represents the method info structure, i.e., the representation
61  * for a method in the class. See JVM specification for details.
62  * A method has access flags, a name, a signature and a number of attributes.
63  *
64  * @version $Id: Method.java,v 1.1.1.1 2001/10/29 20:00:02 jvanzyl Exp $
65  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
66  */

67 public final class Method extends FieldOrMethod {
68   /**
69    * Empty constructor, all attributes have to be defined via `setXXX'
70    * methods. Use at your own risk.
71    */

72   public Method() {}
73
74   /**
75    * Initialize from another object. Note that both objects use the same
76    * references (shallow copy). Use clone() for a physical copy.
77    */

78   public Method(Method c) {
79     super(c);
80   }
81
82   /**
83    * Construct object from file stream.
84    * @param file Input stream
85    * @throw IOException
86    * @throw ClassFormatError
87    */

88   Method(DataInputStream file, ConstantPool constant_pool)
89        throws IOException, ClassFormatError JavaDoc
90   {
91     super(file, constant_pool);
92   }
93
94   /**
95    * @param access_flags Access rights of method
96    * @param name_index Points to field name in constant pool
97    * @param signature_index Points to encoded signature
98    * @param attributes Collection of attributes
99    * @param constant_pool Array of constants
100    */

101   public Method(int access_flags, int name_index, int signature_index,
102         Attribute[] attributes, ConstantPool constant_pool)
103   {
104     super(access_flags, name_index, signature_index, attributes, constant_pool);
105   }
106
107   /**
108    * Called by objects that are traversing the nodes of the tree implicitely
109    * defined by the contents of a Java class. I.e., the hierarchy of methods,
110    * fields, attributes, etc. spawns a tree of objects.
111    *
112    * @param v Visitor object
113    */

114   public void accept(Visitor v) {
115     v.visitMethod(this);
116   }
117
118   /**
119    * @return Code attribute of method, if any
120    */

121   public final Code getCode() {
122     for(int i=0; i < attributes_count; i++)
123       if(attributes[i] instanceof Code)
124     return (Code)attributes[i];
125
126     return null;
127   }
128
129   /**
130    * @return ExceptionTable attribute of method, if any, i.e., list all
131    * exceptions the method may throw not exception handlers!
132    */

133   public final ExceptionTable getExceptionTable() {
134     for(int i=0; i < attributes_count; i++)
135       if(attributes[i] instanceof ExceptionTable)
136     return (ExceptionTable)attributes[i];
137
138     return null;
139   }
140
141   /** @return LocalVariableTable of code attribute if any, i.e. the call is forwarded
142    * to the Code atribute.
143    */

144   public final LocalVariableTable getLocalVariableTable() {
145     Code code = getCode();
146
147     if(code != null)
148       return code.getLocalVariableTable();
149     else
150       return null;
151   }
152
153   /** @return LineNumberTable of code attribute if any, i.e. the call is forwarded
154    * to the Code atribute.
155    */

156   public final LineNumberTable getLineNumberTable() {
157     Code code = getCode();
158
159     if(code != null)
160       return code.getLineNumberTable();
161     else
162       return null;
163   }
164
165   /**
166    * Return string representation close to declaration format,
167    * `public static void main(String[] args) throws IOException', e.g.
168    *
169    * @return String representation of the method.
170    */

171   public final String JavaDoc toString() {
172     ConstantUtf8 c;
173     ConstantValue cv;
174     String JavaDoc name, signature, access; // Short cuts to constant pool
175
String JavaDoc exceptions;
176     StringBuffer JavaDoc buf;
177     Attribute[] attr;
178
179     access = Utility.accessToString(access_flags);
180
181     // Get name and signature from constant pool
182
c = (ConstantUtf8)constant_pool.getConstant(signature_index,
183                         Constants.CONSTANT_Utf8);
184     signature = c.getBytes();
185
186     c = (ConstantUtf8)constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
187     name = c.getBytes();
188
189     signature = Utility.methodSignatureToString(signature, name, access, true,
190                         getLocalVariableTable());
191     buf = new StringBuffer JavaDoc(signature);
192
193     for(int i=0; i < attributes_count; i++) {
194       Attribute a = attributes[i];
195
196       if(!((a instanceof Code) || (a instanceof ExceptionTable)))
197     buf.append(" [" + a.toString() + "]");
198     }
199
200     ExceptionTable e = getExceptionTable();
201     if(e != null) {
202       String JavaDoc str = e.toString();
203       if(!str.equals(""))
204     buf.append("\n\t\tthrows " + str);
205     }
206  
207     return buf.toString();
208   }
209
210   /**
211    * @return deep copy of this method
212    */

213   public final Method copy(ConstantPool constant_pool) {
214     return (Method)copy_(constant_pool);
215   }
216 }
217
Popular Tags