1 17 package org.apache.bcel.classfile; 18 19 import java.io.DataInputStream ; 20 import java.io.IOException ; 21 import org.apache.bcel.Constants; 22 import org.apache.bcel.generic.Type; 23 import org.apache.bcel.util.BCELComparator; 24 25 33 public final class Method extends FieldOrMethod { 34 35 private static BCELComparator _cmp = new BCELComparator() { 36 37 public boolean equals( Object o1, Object o2 ) { 38 Method THIS = (Method) o1; 39 Method THAT = (Method) o2; 40 return THIS.getName().equals(THAT.getName()) 41 && THIS.getSignature().equals(THAT.getSignature()); 42 } 43 44 45 public int hashCode( Object o ) { 46 Method THIS = (Method) o; 47 return THIS.getSignature().hashCode() ^ THIS.getName().hashCode(); 48 } 49 }; 50 51 52 56 public Method() { 57 } 58 59 60 64 public Method(Method c) { 65 super(c); 66 } 67 68 69 75 Method(DataInputStream file, ConstantPool constant_pool) throws IOException , 76 ClassFormatException { 77 super(file, constant_pool); 78 } 79 80 81 88 public Method(int access_flags, int name_index, int signature_index, Attribute[] attributes, 89 ConstantPool constant_pool) { 90 super(access_flags, name_index, signature_index, attributes, constant_pool); 91 } 92 93 94 101 public void accept( Visitor v ) { 102 v.visitMethod(this); 103 } 104 105 106 109 public final Code getCode() { 110 for (int i = 0; i < attributes_count; i++) { 111 if (attributes[i] instanceof Code) { 112 return (Code) attributes[i]; 113 } 114 } 115 return null; 116 } 117 118 119 123 public final ExceptionTable getExceptionTable() { 124 for (int i = 0; i < attributes_count; i++) { 125 if (attributes[i] instanceof ExceptionTable) { 126 return (ExceptionTable) attributes[i]; 127 } 128 } 129 return null; 130 } 131 132 133 136 public final LocalVariableTable getLocalVariableTable() { 137 Code code = getCode(); 138 if (code == null) { 139 return null; 140 } 141 return code.getLocalVariableTable(); 142 } 143 144 145 148 public final LineNumberTable getLineNumberTable() { 149 Code code = getCode(); 150 if (code == null) { 151 return null; 152 } 153 return code.getLineNumberTable(); 154 } 155 156 157 163 public final String toString() { 164 ConstantUtf8 c; 165 String name, signature, access; StringBuffer buf; 167 access = Utility.accessToString(access_flags); 168 c = (ConstantUtf8) constant_pool.getConstant(signature_index, Constants.CONSTANT_Utf8); 170 signature = c.getBytes(); 171 c = (ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8); 172 name = c.getBytes(); 173 signature = Utility.methodSignatureToString(signature, name, access, true, 174 getLocalVariableTable()); 175 buf = new StringBuffer (signature); 176 for (int i = 0; i < attributes_count; i++) { 177 Attribute a = attributes[i]; 178 if (!((a instanceof Code) || (a instanceof ExceptionTable))) { 179 buf.append(" [").append(a.toString()).append("]"); 180 } 181 } 182 ExceptionTable e = getExceptionTable(); 183 if (e != null) { 184 String str = e.toString(); 185 if (!str.equals("")) { 186 buf.append("\n\t\tthrows ").append(str); 187 } 188 } 189 return buf.toString(); 190 } 191 192 193 196 public final Method copy( ConstantPool _constant_pool ) { 197 return (Method) copy_(_constant_pool); 198 } 199 200 201 204 public Type getReturnType() { 205 return Type.getReturnType(getSignature()); 206 } 207 208 209 212 public Type[] getArgumentTypes() { 213 return Type.getArgumentTypes(getSignature()); 214 } 215 216 217 220 public static BCELComparator getComparator() { 221 return _cmp; 222 } 223 224 225 228 public static void setComparator( BCELComparator comparator ) { 229 _cmp = comparator; 230 } 231 232 233 240 public boolean equals( Object obj ) { 241 return _cmp.equals(this, obj); 242 } 243 244 245 251 public int hashCode() { 252 return _cmp.hashCode(this); 253 } 254 } 255 | Popular Tags |