434 // TO DO: Replace wierd characters in s by C-style escapes.
435 return "'"+s.replaceAll("['\\\\]", "\\\\$0")+"'";
436 }
437
438 private static void checkSafeBytecodeName(String s)
439 throws IllegalArgumentException {
440 if (!isSafeBytecodeName(s)) {
441 throw new IllegalArgumentException(s);
442 }
443 }
444
445 /**
446 * Report whether a simple name is safe as a bytecode name.
447 * Such names are acceptable in class files as class, method, and field names.
448 * Additionally, they are free of "dangerous" characters, even if those
449 * characters are legal in some (or all) names in class files.
450 * @param s the proposed bytecode name
451 * @return true if the name is non-empty and all of its characters are safe
452 */
453 public static boolean isSafeBytecodeName(String s) {
454 if (s.length() == 0) return false;
455 // check occurrences of each DANGEROUS char
456 for (char xc : DANGEROUS_CHARS_A) {
457 if (xc == ESCAPE_C) continue; // not really that dangerous
458 if (s.indexOf(xc) >= 0) return false;
459 }
460 return true;
461 }
462
463 /**
464 * Report whether a character is safe in a bytecode name.
465 * This is true of any unicode character except the following
466 * <em>dangerous characters</em>: {@code ".;:$[]<>/"}.
467 * @param c the proposed character
468 * @return true if the character is safe to use in classfiles
469 */
470 public static boolean isSafeBytecodeChar(char c) {
471 return DANGEROUS_CHARS.indexOf(c) < DANGEROUS_CHAR_FIRST_INDEX;
472 }
473
474 private static boolean looksMangled(String s) {
475 return s.charAt(0) == ESCAPE_C;
476 }
477
478 private static String mangle(String s) {
479 if (s.length() == 0)
480 return NULL_ESCAPE;
481
482 // build this lazily, when we first need an escape:
483 StringBuilder sb = null;
484
485 for (int i = 0, slen = s.length(); i < slen; i++) {
486 char c = s.charAt(i);
487
488 boolean needEscape = false;
489 if (c == ESCAPE_C) {
490 if (i+1 < slen) {
491 char c1 = s.charAt(i+1);
492 if ((i == 0 && c1 == NULL_ESCAPE_C)
493 || c1 != originalOfReplacement(c1)) {
494 // an accidental escape
495 needEscape = true;
496 }
497 }
498 } else {
499 needEscape = isDangerous(c);
|
434 // TO DO: Replace wierd characters in s by C-style escapes.
435 return "'"+s.replaceAll("['\\\\]", "\\\\$0")+"'";
436 }
437
438 private static void checkSafeBytecodeName(String s)
439 throws IllegalArgumentException {
440 if (!isSafeBytecodeName(s)) {
441 throw new IllegalArgumentException(s);
442 }
443 }
444
445 /**
446 * Report whether a simple name is safe as a bytecode name.
447 * Such names are acceptable in class files as class, method, and field names.
448 * Additionally, they are free of "dangerous" characters, even if those
449 * characters are legal in some (or all) names in class files.
450 * @param s the proposed bytecode name
451 * @return true if the name is non-empty and all of its characters are safe
452 */
453 public static boolean isSafeBytecodeName(String s) {
454 if (s.isEmpty()) return false;
455 // check occurrences of each DANGEROUS char
456 for (char xc : DANGEROUS_CHARS_A) {
457 if (xc == ESCAPE_C) continue; // not really that dangerous
458 if (s.indexOf(xc) >= 0) return false;
459 }
460 return true;
461 }
462
463 /**
464 * Report whether a character is safe in a bytecode name.
465 * This is true of any unicode character except the following
466 * <em>dangerous characters</em>: {@code ".;:$[]<>/"}.
467 * @param c the proposed character
468 * @return true if the character is safe to use in classfiles
469 */
470 public static boolean isSafeBytecodeChar(char c) {
471 return DANGEROUS_CHARS.indexOf(c) < DANGEROUS_CHAR_FIRST_INDEX;
472 }
473
474 private static boolean looksMangled(String s) {
475 return s.charAt(0) == ESCAPE_C;
476 }
477
478 private static String mangle(String s) {
479 if (s.isEmpty())
480 return NULL_ESCAPE;
481
482 // build this lazily, when we first need an escape:
483 StringBuilder sb = null;
484
485 for (int i = 0, slen = s.length(); i < slen; i++) {
486 char c = s.charAt(i);
487
488 boolean needEscape = false;
489 if (c == ESCAPE_C) {
490 if (i+1 < slen) {
491 char c1 = s.charAt(i+1);
492 if ((i == 0 && c1 == NULL_ESCAPE_C)
493 || c1 != originalOfReplacement(c1)) {
494 // an accidental escape
495 needEscape = true;
496 }
497 }
498 } else {
499 needEscape = isDangerous(c);
|