1 package org.apache.bcel.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 org.apache.bcel.Constants;
57 import org.apache.bcel.generic.Type;
58 import java.io.*;
59
60 /***
61 * This class represents the method info structure, i.e., the representation
62 * for a method in the class. See JVM specification for details.
63 * A method has access flags, a name, a signature and a number of attributes.
64 *
65 * @version $Id: Method.java,v 1.5 2002/12/08 16:04:37 mdahm Exp $
66 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
67 */
68 public final class Method extends FieldOrMethod {
69 /***
70 * Empty constructor, all attributes have to be defined via `setXXX'
71 * methods. Use at your own risk.
72 */
73 public Method() {}
74
75 /***
76 * Initialize from another object. Note that both objects use the same
77 * references (shallow copy). Use clone() for a physical copy.
78 */
79 public Method(Method c) {
80 super(c);
81 }
82
83 /***
84 * Construct object from file stream.
85 * @param file Input stream
86 * @throws IOException
87 * @throws ClassFormatException
88 */
89 Method(DataInputStream file, ConstantPool constant_pool)
90 throws IOException, ClassFormatException
91 {
92 super(file, constant_pool);
93 }
94
95 /***
96 * @param access_flags Access rights of method
97 * @param name_index Points to field name in constant pool
98 * @param signature_index Points to encoded signature
99 * @param attributes Collection of attributes
100 * @param constant_pool Array of constants
101 */
102 public Method(int access_flags, int name_index, int signature_index,
103 Attribute[] attributes, ConstantPool constant_pool)
104 {
105 super(access_flags, name_index, signature_index, attributes, constant_pool);
106 }
107
108 /***
109 * Called by objects that are traversing the nodes of the tree implicitely
110 * defined by the contents of a Java class. I.e., the hierarchy of methods,
111 * fields, attributes, etc. spawns a tree of objects.
112 *
113 * @param v Visitor object
114 */
115 public void accept(Visitor v) {
116 v.visitMethod(this);
117 }
118
119 /***
120 * @return Code attribute of method, if any
121 */
122 public final Code getCode() {
123 for(int i=0; i < attributes_count; i++)
124 if(attributes[i] instanceof Code)
125 return (Code)attributes[i];
126
127 return null;
128 }
129
130 /***
131 * @return ExceptionTable attribute of method, if any, i.e., list all
132 * exceptions the method may throw not exception handlers!
133 */
134 public final ExceptionTable getExceptionTable() {
135 for(int i=0; i < attributes_count; i++)
136 if(attributes[i] instanceof ExceptionTable)
137 return (ExceptionTable)attributes[i];
138
139 return null;
140 }
141
142 /*** @return LocalVariableTable of code attribute if any, i.e. the call is forwarded
143 * to the Code atribute.
144 */
145 public final LocalVariableTable getLocalVariableTable() {
146 Code code = getCode();
147
148 if(code != null)
149 return code.getLocalVariableTable();
150 else
151 return null;
152 }
153
154 /*** @return LineNumberTable of code attribute if any, i.e. the call is forwarded
155 * to the Code atribute.
156 */
157 public final LineNumberTable getLineNumberTable() {
158 Code code = getCode();
159
160 if(code != null)
161 return code.getLineNumberTable();
162 else
163 return null;
164 }
165
166 /***
167 * Return string representation close to declaration format,
168 * `public static void main(String[] args) throws IOException', e.g.
169 *
170 * @return String representation of the method.
171 */
172 public final String toString() {
173 ConstantUtf8 c;
174 String name, signature, access; // Short cuts to constant pool
175 StringBuffer buf;
176
177 access = Utility.accessToString(access_flags);
178
179 // Get name and signature from constant pool
180 c = (ConstantUtf8)constant_pool.getConstant(signature_index,
181 Constants.CONSTANT_Utf8);
182 signature = c.getBytes();
183
184 c = (ConstantUtf8)constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
185 name = c.getBytes();
186
187 signature = Utility.methodSignatureToString(signature, name, access, true,
188 getLocalVariableTable());
189 buf = new StringBuffer(signature);
190
191 for(int i=0; i < attributes_count; i++) {
192 Attribute a = attributes[i];
193
194 if(!((a instanceof Code) || (a instanceof ExceptionTable)))
195 buf.append(" [" + a.toString() + "]");
196 }
197
198 ExceptionTable e = getExceptionTable();
199 if(e != null) {
200 String str = e.toString();
201 if(!str.equals(""))
202 buf.append("\n\t\tthrows " + str);
203 }
204
205 return buf.toString();
206 }
207
208 /***
209 * @return deep copy of this method
210 */
211 public final Method copy(ConstantPool constant_pool) {
212 return (Method)copy_(constant_pool);
213 }
214
215 /***
216 * @return return type of method
217 */
218 public Type getReturnType() {
219 return Type.getReturnType(getSignature());
220 }
221
222 /***
223 * @return array of method argument types
224 */
225 public Type[] getArgumentTypes() {
226 return Type.getArgumentTypes(getSignature());
227 }
228 }
This page was automatically generated by Maven