1 11 package org.eclipse.jdt.internal.debug.eval.ast.instructions; 12 13 14 import org.eclipse.jdt.core.compiler.CharOperation; 15 16 23 public class RuntimeSignature { 24 public static final char C_BOOLEAN = 'Z'; 25 public static final char C_BYTE = 'B'; 26 public static final char C_CHAR = 'C'; 27 public static final char C_DOUBLE = 'D'; 28 public static final char C_FLOAT = 'F'; 29 public static final char C_INT = 'I'; 30 public static final char C_SEMICOLON = ';'; 31 public static final char C_LONG = 'J'; 32 public static final char C_SHORT = 'S'; 33 public static final char C_VOID = 'V'; 34 public static final char C_DOT = '.'; 35 public static final char C_DOLLAR = '$'; 36 public static final char C_ARRAY = '['; 37 public static final char C_RESOLVED = 'L'; 38 public static final char C_UNRESOLVED = 'Q'; 39 public static final char C_NAME_END = ';'; 40 public static final char C_PARAM_START = '('; 41 public static final char C_PARAM_END = ')'; 42 public static final String SIG_BOOLEAN = "Z"; public static final String SIG_BYTE = "B"; public static final String SIG_CHAR = "C"; public static final String SIG_DOUBLE = "D"; public static final String SIG_FLOAT = "F"; public static final String SIG_INT = "I"; public static final String SIG_LONG = "J"; public static final String SIG_SHORT = "S"; public static final String SIG_VOID = "V"; private static final char[] NO_CHAR = new char[0]; 52 private static final char[] BOOLEAN = { 'b', 'o', 'o', 'l', 'e', 'a', 'n' }; 53 private static final char[] BYTE = { 'b', 'y', 't', 'e' }; 54 private static final char[] CHAR = { 'c', 'h', 'a', 'r' }; 55 private static final char[] DOUBLE = { 'd', 'o', 'u', 'b', 'l', 'e' }; 56 private static final char[] FLOAT = { 'f', 'l', 'o', 'a', 't' }; 57 private static final char[] INT = { 'i', 'n', 't' }; 58 private static final char[] LONG = { 'l', 'o', 'n', 'g' }; 59 private static final char[] SHORT = { 's', 'h', 'o', 'r', 't' }; 60 private static final char[] VOID = { 'v', 'o', 'i', 'd' }; 61 62 public static String toString(String signature) 63 throws IllegalArgumentException { 64 return new String (toCharArray(signature.toCharArray())); 65 } 66 67 public static char[] toCharArray(char[] signature) 68 throws IllegalArgumentException { 69 try { 70 int sigLength = signature.length; 71 72 if (sigLength == 0 || signature[0] == C_PARAM_START) { 73 return toCharArray(signature, NO_CHAR, null, true, true); 74 } 75 76 int resultLength = 0; 78 int index = -1; 79 while (signature[++index] == C_ARRAY) { 80 resultLength += 2; } 82 switch (signature[index]) { 83 case C_BOOLEAN : 84 resultLength += BOOLEAN.length; 85 break; 86 case C_BYTE : 87 resultLength += BYTE.length; 88 break; 89 case C_CHAR : 90 resultLength += CHAR.length; 91 break; 92 case C_DOUBLE : 93 resultLength += DOUBLE.length; 94 break; 95 case C_FLOAT : 96 resultLength += FLOAT.length; 97 break; 98 case C_INT : 99 resultLength += INT.length; 100 break; 101 case C_LONG : 102 resultLength += LONG.length; 103 break; 104 case C_SHORT : 105 resultLength += SHORT.length; 106 break; 107 case C_VOID : 108 resultLength += VOID.length; 109 break; 110 case C_RESOLVED : 111 case C_UNRESOLVED : 112 int end = 113 CharOperation.indexOf(C_SEMICOLON, signature, index); 114 if (end == -1) 115 throw new IllegalArgumentException (); 116 int start = index + 1; 117 resultLength += end - start; 118 break; 119 default : 120 throw new IllegalArgumentException (); 121 } 122 123 char[] result = new char[resultLength]; 124 copyType(signature, 0, result, 0, true); 125 126 133 135 return result; 136 } catch (ArrayIndexOutOfBoundsException e) { 137 throw new IllegalArgumentException (); 138 } 139 } 140 141 public static char[] toCharArray( 142 char[] methodSignature, 143 char[] methodName, 144 char[][] parameterNames, 145 boolean fullyQualifyTypeNames, 146 boolean includeReturnType) { 147 try { 148 int firstParen = 149 CharOperation.indexOf(C_PARAM_START, methodSignature); 150 if (firstParen == -1) 151 throw new IllegalArgumentException (); 152 153 int sigLength = methodSignature.length; 154 155 157 int paramCount = 0; 159 int lastParen = -1; 160 int resultLength = 0; 161 signature : for (int i = firstParen; i < sigLength; i++) { 162 switch (methodSignature[i]) { 163 case C_ARRAY : 164 resultLength += 2; continue signature; 166 case C_BOOLEAN : 167 resultLength += BOOLEAN.length; 168 break; 169 case C_BYTE : 170 resultLength += BYTE.length; 171 break; 172 case C_CHAR : 173 resultLength += CHAR.length; 174 break; 175 case C_DOUBLE : 176 resultLength += DOUBLE.length; 177 break; 178 case C_FLOAT : 179 resultLength += FLOAT.length; 180 break; 181 case C_INT : 182 resultLength += INT.length; 183 break; 184 case C_LONG : 185 resultLength += LONG.length; 186 break; 187 case C_SHORT : 188 resultLength += SHORT.length; 189 break; 190 case C_VOID : 191 resultLength += VOID.length; 192 break; 193 case C_RESOLVED : 194 case C_UNRESOLVED : 195 int end = 196 CharOperation.indexOf( 197 C_SEMICOLON, 198 methodSignature, 199 i); 200 if (end == -1) 201 throw new IllegalArgumentException (); 202 int start; 203 if (fullyQualifyTypeNames) { 204 start = i + 1; 205 } else { 206 start = 207 CharOperation.lastIndexOf( 208 C_DOT, 209 methodSignature, 210 i, 211 end) 212 + 1; 213 if (start == 0) 214 start = i + 1; 215 } 216 resultLength += end - start; 217 i = end; 218 break; 219 case C_PARAM_START : 220 resultLength++; 222 continue signature; 223 case C_PARAM_END : 224 lastParen = i; 225 if (includeReturnType) { 226 if (paramCount > 0) { 227 resultLength -= 2; 230 } 235 paramCount--; 237 continue signature; 238 } 239 if (paramCount > 0) { 240 resultLength--; 242 } else { 243 resultLength++; 245 } 246 break signature; 247 default : 248 throw new IllegalArgumentException (); 249 } 250 resultLength += 2; paramCount++; 252 } 253 254 int parameterNamesLength = 256 parameterNames == null ? 0 : parameterNames.length; 257 for (int i = 0; i < parameterNamesLength; i++) { 258 resultLength += parameterNames[i].length + 1; 259 } 261 262 int selectorLength = methodName == null ? 0 : methodName.length; 264 resultLength += selectorLength; 265 266 char[] result = new char[resultLength]; 268 269 int index = 0; 271 if (includeReturnType) { 272 long pos = 273 copyType( 274 methodSignature, 275 lastParen + 1, 276 result, 277 index, 278 fullyQualifyTypeNames); 279 index = (int) (pos >>> 32); 280 result[index++] = ' '; 281 } 282 283 if (methodName != null) { 285 System.arraycopy(methodName, 0, result, index, selectorLength); 286 index += selectorLength; 287 } 288 289 result[index++] = C_PARAM_START; 291 int sigPos = firstParen + 1; 292 for (int i = 0; i < paramCount; i++) { 293 long pos = 294 copyType( 295 methodSignature, 296 sigPos, 297 result, 298 index, 299 fullyQualifyTypeNames); 300 index = (int) (pos >>> 32); 301 sigPos = (int) pos; 302 if (parameterNames != null) { 303 result[index++] = ' '; 304 char[] parameterName = parameterNames[i]; 305 int paramLength = parameterName.length; 306 System.arraycopy( 307 parameterName, 308 0, 309 result, 310 index, 311 paramLength); 312 index += paramLength; 313 } 314 if (i != paramCount - 1) { 315 result[index++] = ','; 316 result[index++] = ' '; 317 } 318 } 319 if (sigPos >= sigLength) { 320 throw new IllegalArgumentException (); 321 } 323 result[index++] = C_PARAM_END; 324 325 return result; 326 } catch (ArrayIndexOutOfBoundsException e) { 327 throw new IllegalArgumentException (); 328 } 329 } 330 331 private static long copyType( 332 char[] signature, 333 int sigPos, 334 char[] dest, 335 int index, 336 boolean fullyQualifyTypeNames) { 337 int arrayCount = 0; 338 loop : while (true) { 339 switch (signature[sigPos++]) { 340 case C_ARRAY : 341 arrayCount++; 342 break; 343 case C_BOOLEAN : 344 int length = BOOLEAN.length; 345 System.arraycopy(BOOLEAN, 0, dest, index, length); 346 index += length; 347 break loop; 348 case C_BYTE : 349 length = BYTE.length; 350 System.arraycopy(BYTE, 0, dest, index, length); 351 index += length; 352 break loop; 353 case C_CHAR : 354 length = CHAR.length; 355 System.arraycopy(CHAR, 0, dest, index, length); 356 index += length; 357 break loop; 358 case C_DOUBLE : 359 length = DOUBLE.length; 360 System.arraycopy(DOUBLE, 0, dest, index, length); 361 index += length; 362 break loop; 363 case C_FLOAT : 364 length = FLOAT.length; 365 System.arraycopy(FLOAT, 0, dest, index, length); 366 index += length; 367 break loop; 368 case C_INT : 369 length = INT.length; 370 System.arraycopy(INT, 0, dest, index, length); 371 index += length; 372 break loop; 373 case C_LONG : 374 length = LONG.length; 375 System.arraycopy(LONG, 0, dest, index, length); 376 index += length; 377 break loop; 378 case C_SHORT : 379 length = SHORT.length; 380 System.arraycopy(SHORT, 0, dest, index, length); 381 index += length; 382 break loop; 383 case C_VOID : 384 length = VOID.length; 385 System.arraycopy(VOID, 0, dest, index, length); 386 index += length; 387 break loop; 388 case C_RESOLVED : 389 case C_UNRESOLVED : 390 int end = 391 CharOperation.indexOf(C_SEMICOLON, signature, sigPos); 392 if (end == -1) 393 throw new IllegalArgumentException (); 394 int start; 395 if (fullyQualifyTypeNames) { 396 start = sigPos; 397 } else { 398 start = 399 CharOperation.lastIndexOf( 400 C_DOT, 401 signature, 402 sigPos, 403 end) 404 + 1; 405 if (start == 0) 406 start = sigPos; 407 } 408 length = end - start; 409 System.arraycopy(signature, start, dest, index, length); 410 sigPos = end + 1; 411 index += length; 412 break loop; 413 } 414 } 415 while (arrayCount-- > 0) { 416 dest[index++] = '['; 417 dest[index++] = ']'; 418 } 419 return (((long) index) << 32) + sigPos; 420 } 421 } 422 | Popular Tags |