View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License. 
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInputStream;
21  import java.io.IOException;
22  import org.apache.bcel.Constants;
23  import org.apache.bcel.generic.Type;
24  import org.apache.bcel.util.BCELComparator;
25  
26  /**
27   * This class represents the method info structure, i.e., the representation 
28   * for a method in the class. See JVM specification for details.
29   * A method has access flags, a name, a signature and a number of attributes.
30   *
31   * @version $Id: Method.java 1152077 2011-07-29 02:29:42Z dbrosius $
32   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
33   */
34  public final class Method extends FieldOrMethod {
35  
36      private static final long serialVersionUID = -2013983967283787941L;
37      private static BCELComparator _cmp = new BCELComparator() {
38  
39          public boolean equals( Object o1, Object o2 ) {
40              Method THIS = (Method) o1;
41              Method THAT = (Method) o2;
42              return THIS.getName().equals(THAT.getName())
43                      && THIS.getSignature().equals(THAT.getSignature());
44          }
45  
46  
47          public int hashCode( Object o ) {
48              Method THIS = (Method) o;
49              return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
50          }
51      };
52  
53  
54      /**
55       * Empty constructor, all attributes have to be defined via `setXXX'
56       * methods. Use at your own risk.
57       */
58      public Method() {
59      }
60  
61  
62      /**
63       * Initialize from another object. Note that both objects use the same
64       * references (shallow copy). Use clone() for a physical copy.
65       */
66      public Method(Method c) {
67          super(c);
68      }
69  
70  
71      /**
72       * Construct object from file stream.
73       * @param file Input stream
74       * @throws IOException
75       * @throws ClassFormatException
76       */
77      Method(DataInputStream file, ConstantPool constant_pool) throws IOException,
78              ClassFormatException {
79          super(file, constant_pool);
80      }
81  
82  
83      /**
84       * @param access_flags Access rights of method
85       * @param name_index Points to field name in constant pool
86       * @param signature_index Points to encoded signature
87       * @param attributes Collection of attributes
88       * @param constant_pool Array of constants
89       */
90      public Method(int access_flags, int name_index, int signature_index, Attribute[] attributes,
91              ConstantPool constant_pool) {
92          super(access_flags, name_index, signature_index, attributes, constant_pool);
93      }
94  
95  
96      /**
97       * Called by objects that are traversing the nodes of the tree implicitely
98       * defined by the contents of a Java class. I.e., the hierarchy of methods,
99       * fields, attributes, etc. spawns a tree of objects.
100      *
101      * @param v Visitor object
102      */
103     public void accept( Visitor v ) {
104         v.visitMethod(this);
105     }
106 
107 
108     /**
109      * @return Code attribute of method, if any
110      */
111     public final Code getCode() {
112         for (int i = 0; i < attributes_count; i++) {
113             if (attributes[i] instanceof Code) {
114                 return (Code) attributes[i];
115             }
116         }
117         return null;
118     }
119 
120 
121     /**
122      * @return ExceptionTable attribute of method, if any, i.e., list all
123      * exceptions the method may throw not exception handlers!
124      */
125     public final ExceptionTable getExceptionTable() {
126         for (int i = 0; i < attributes_count; i++) {
127             if (attributes[i] instanceof ExceptionTable) {
128                 return (ExceptionTable) attributes[i];
129             }
130         }
131         return null;
132     }
133 
134 
135     /** @return LocalVariableTable of code attribute if any, i.e. the call is forwarded
136      * to the Code atribute.
137      */
138     public final LocalVariableTable getLocalVariableTable() {
139         Code code = getCode();
140         if (code == null) {
141             return null;
142         }
143         return code.getLocalVariableTable();
144     }
145 
146 
147     /** @return LineNumberTable of code attribute if any, i.e. the call is forwarded
148      * to the Code atribute.
149      */
150     public final LineNumberTable getLineNumberTable() {
151         Code code = getCode();
152         if (code == null) {
153             return null;
154         }
155         return code.getLineNumberTable();
156     }
157 
158 
159     /**
160      * Return string representation close to declaration format,
161      * `public static void main(String[] args) throws IOException', e.g.
162      *
163      * @return String representation of the method.
164      */
165     @Override
166     public final String toString() {
167         ConstantUtf8 c;
168         String name, signature, access; // Short cuts to constant pool
169         StringBuilder buf;
170         access = Utility.accessToString(access_flags);
171         // Get name and signature from constant pool
172         c = (ConstantUtf8) constant_pool.getConstant(signature_index, Constants.CONSTANT_Utf8);
173         signature = c.getBytes();
174         c = (ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
175         name = c.getBytes();
176         signature = Utility.methodSignatureToString(signature, name, access, true,
177                 getLocalVariableTable());
178         buf = new StringBuilder(signature);
179         for (int i = 0; i < attributes_count; i++) {
180             Attribute a = attributes[i];
181             if (!((a instanceof Code) || (a instanceof ExceptionTable))) {
182                 buf.append(" [").append(a.toString()).append("]");
183             }
184         }
185         ExceptionTable e = getExceptionTable();
186         if (e != null) {
187             String str = e.toString();
188             if (!str.equals("")) {
189                 buf.append("\n\t\tthrows ").append(str);
190             }
191         }
192         return buf.toString();
193     }
194 
195 
196     /**
197      * @return deep copy of this method
198      */
199     public final Method copy( ConstantPool _constant_pool ) {
200         return (Method) copy_(_constant_pool);
201     }
202 
203 
204     /**
205      * @return return type of method
206      */
207     public Type getReturnType() {
208         return Type.getReturnType(getSignature());
209     }
210 
211 
212     /**
213      * @return array of method argument types
214      */
215     public Type[] getArgumentTypes() {
216         return Type.getArgumentTypes(getSignature());
217     }
218 
219 
220     /**
221      * @return Comparison strategy object
222      */
223     public static BCELComparator getComparator() {
224         return _cmp;
225     }
226 
227 
228     /**
229      * @param comparator Comparison strategy object
230      */
231     public static void setComparator( BCELComparator comparator ) {
232         _cmp = comparator;
233     }
234 
235 
236     /**
237      * Return value as defined by given BCELComparator strategy.
238      * By default two method objects are said to be equal when
239      * their names and signatures are equal.
240      * 
241      * @see java.lang.Object#equals(java.lang.Object)
242      */
243     @Override
244     public boolean equals( Object obj ) {
245         return _cmp.equals(this, obj);
246     }
247 
248 
249     /**
250      * Return value as defined by given BCELComparator strategy.
251      * By default return the hashcode of the method's name XOR signature.
252      * 
253      * @see java.lang.Object#hashCode()
254      */
255     @Override
256     public int hashCode() {
257         return _cmp.hashCode(this);
258     }
259 }