1 30 package com.tc.asm.commons; 31 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import com.tc.asm.Type; 36 37 44 public class Method { 45 46 49 private final String name; 50 51 54 private final String desc; 55 56 59 private final static Map DESCRIPTORS; 60 61 static { 62 DESCRIPTORS = new HashMap (); 63 DESCRIPTORS.put("void", "V"); 64 DESCRIPTORS.put("byte", "B"); 65 DESCRIPTORS.put("char", "C"); 66 DESCRIPTORS.put("double", "D"); 67 DESCRIPTORS.put("float", "F"); 68 DESCRIPTORS.put("int", "I"); 69 DESCRIPTORS.put("long", "J"); 70 DESCRIPTORS.put("short", "S"); 71 DESCRIPTORS.put("boolean", "Z"); 72 } 73 74 80 public Method(final String name, final String desc) { 81 this.name = name; 82 this.desc = desc; 83 } 84 85 92 public Method( 93 final String name, 94 final Type returnType, 95 final Type[] argumentTypes) 96 { 97 this(name, Type.getMethodDescriptor(returnType, argumentTypes)); 98 } 99 100 113 public static Method getMethod(final String method) 114 throws IllegalArgumentException 115 { 116 int space = method.indexOf(' '); 117 int start = method.indexOf('(', space) + 1; 118 int end = method.indexOf(')', start); 119 if (space == -1 || start == -1 || end == -1) { 120 throw new IllegalArgumentException (); 121 } 122 String returnType = method.substring(0, space); 124 String methodName = method.substring(space + 1, start - 1).trim(); 125 StringBuffer sb = new StringBuffer (); 126 sb.append('('); 127 int p; 128 do { 129 p = method.indexOf(',', start); 130 if (p == -1) { 131 sb.append(map(method.substring(start, end).trim())); 132 } else { 133 sb.append(map(method.substring(start, p).trim())); 134 start = p + 1; 135 } 136 } while (p != -1); 137 sb.append(')'); 138 sb.append(map(returnType)); 139 return new Method(methodName, sb.toString()); 140 } 141 142 private static String map(final String type) { 143 if (type.equals("")) { 144 return type; 145 } 146 147 StringBuffer sb = new StringBuffer (); 148 int index = 0; 149 while ((index = type.indexOf("[]", index) + 1) > 0) { 150 sb.append('['); 151 } 152 153 String t = type.substring(0, type.length() - sb.length() * 2); 154 String desc = (String ) DESCRIPTORS.get(t); 155 if (desc != null) { 156 sb.append(desc); 157 } else { 158 sb.append('L'); 159 if (t.indexOf('.') < 0) { 160 sb.append("java/lang/" + t); 161 } else { 162 sb.append(t.replace('.', '/')); 163 } 164 sb.append(';'); 165 } 166 return sb.toString(); 167 } 168 169 174 public String getName() { 175 return name; 176 } 177 178 183 public String getDescriptor() { 184 return desc; 185 } 186 187 192 public Type getReturnType() { 193 return Type.getReturnType(desc); 194 } 195 196 201 public Type[] getArgumentTypes() { 202 return Type.getArgumentTypes(desc); 203 } 204 205 public String toString() { 206 return name + desc; 207 } 208 209 public boolean equals(final Object o) { 210 if (!(o instanceof Method)) { 211 return false; 212 } 213 Method other = (Method) o; 214 return name.equals(other.name) && desc.equals(other.desc); 215 } 216 217 public int hashCode() { 218 return name.hashCode() ^ desc.hashCode(); 219 } 220 } | Popular Tags |